How to get body.scrollHeight in script injected of Chrome Extension? - google-chrome-extension

Here are the list of my project's files.
index.html - main extension file
manifest.json
background.html
background.js
content.js
manifest.json
manifest.json
...
"content_scripts": [
{
"all_frames": false,
"js": [
"content.js"
],
"matches": [
"https://www.****.com/vsearch/*"
],
"run_at": "document_end"
}
]
content.js
...
console.log(document.body.scrollHeight);
I need to get scrollHeight of special site in content.js.
But when I load this extension, sometimes I got scrollHeight of index.html.
By watching content.js using Chrome Dev Tool, content.js injected correctly in special site.
How can I fix this issue?

Related

Chrome extension manifest v3 exclude_glob and exclude_matches not working

I've got 2 scripts that I want to load depending on what site ppl open in browser ...
login.js should only be loaded on admin.someDomain.com, admin.someOtherDomain.com and localhost
dc_ctre_content.js should be loaded on all other pages opened in browser.
Now, login.js is correctly added, but dc_ctre_content.js is also added to admin.someDomain.com, admin.someOtherDomain.com and localhost domains and it shouldn't be!
Since I'm developing locally my local address is "http://localhost:5090/something" maybe that's the problem.
from manifest.json v3:
"content_scripts": [
{
"matches": ["*://admin.someDomain.com/*","*://admin.someOtherDomain.com/*","*://localhost/*"],
"js": ["js/login.js"],
"run_at": "document_end"
},
{
"exclude_globs": ["*://admin.someDomain.com/*","*://admin.someOtherDomain.com/*","*://localhost/*"],
"matches": ["https://*/*","http://*/*"],
"js": ["js/dc_ctre_content.js"],
"run_at": "document_end"
}
],
I've also tried with exclude_matches but no joy.
Any help appreciated on how to not load dc_ctre_content.js script on excluded domains.
Regards
this fixed it
"exclude_globs": ["http://localhost:*/*"]
I had to put :* after host name for this to work properly.

How to run a content script inside a chrome-extension?

We have a separate html page that can be access inside chrome-extension:// and we want to run the content script inside that extension page, but it seems like the content script doesn't run inside it.
Even updated the manifest.json file to add chrome-extension domain but to no avail?
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*", "file://*/*.pdf", "chrome-extension://*/*"],
"js": ["contentPage.js", "polyfills.js", "runtime.js", "main.js"],
"css": ["styles.css"],
"run_at": "document_idle"
}
],
Note that we should only be running it inside our own content script, so i will also change the chrome-extension://*/* to chrome-extension://{chromeId}/*.
What should be the approach for this one?

Renaming content_script stops it from running in Chrome Extension

In my manifest.json I have:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/punycode.js", "js/content-main.js"]
}
],
However if I rename the content-main.js to content-main1.js update the manifest.json to reflect the change ("js/content-main1.js") and reload the extension the script no longer runs.
How is this possible? Thanks
The Reload Extensions extension that I was using wasn't reflecting the changes in the directory.

manifest.json matches <all_urls> not working after publishing?

From my extension's manifest.json file I am using this:
"content_scripts": [ {
"all_frames": true,
"js": [ "jquery-2.2.3.min.js", "content_script.js" ],
"matches": [ "<all_urls>" ],
"run_at": "document_start"
} ],
When I install my extension locally the matches: is set to all_urls and everything works as expected. My content_script is injected for locally opened file:///c:...whatever.html files which is what I want.
When I publish my app to the Chrome Store and install my extension it does not work anymore. Upon closer inspection I see the Google store has somehow tried to escape my all_urls and my manifest.json is now:
"content_scripts": [ {
"all_frames": true,
"js": [ "jquery-2.2.3.min.js", "content_script.js" ],
"matches": [ "\u003Call_urls>" ],
"run_at": "document_start"
} ],
Why? this completely breaks my extension. How does someone specify all_urls then? I have no idea what I'm doing wrong or why this is happening and appreciate any help someone can provide.

Chrome extension content script not injected to iframes

Our manifest.json contains:
"content_scripts": [
{
"js": [ "content-script.js" ],
"run_at": "document_idle",
"matches": ["https://*.example.com/*"]
}
],
Content script gets correctly injected to our pages at example.com unless somebody embeds our page to iframe on his page. Is there a way for our extension to inject content script to all iframes pointing to example.com if we want to keep "matches": ["https://*.example.com/*"]? We know it is possible with "matches": ["https://*/*"], "all_frames": true but we don't want to ask for wider permissions.

Resources