Chrome extension manifest v3 exclude_glob and exclude_matches not working - google-chrome-extension

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.

Related

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.

How to get body.scrollHeight in script injected of 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?

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.

Chrome Content script injection in MHTML files

It seems the Chrome does not inject content script into local files that are of type MHTML. They might do this for security reason. They don't allow you to download files that have MHTML extension either. So that makes me suspicious.
My content script gets injected properly if the local file type is HTML.
Here is my manifest:
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*", "file://*/*", "<all_urls>" ],
"run_at": "document_start",
"js": [
"js/contentscript.js"
]
}
],
"permissions": [ "tabs", "http://*/*", "https://*/*"],
In the extension management page I also checked:
[x] Allow Access to file URLs
And finally the error I get:
test1.mhtml:1 Blocked script execution in 'file:///Users/test/Downloads/test1.mhtml'
because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
Is there anyway to work around this and get my script injected in .mhtl file?
Update:
Here is a simple test extension that shows script injection and a test mhtml file. Make sure you check this check box in extension management page. [x] Allow Access to file URLs
Update 2:
Found it. It is a chrome bug https://code.google.com/p/chromium/issues/detail?id=452901
Update 3:
so it looks like that it works but chrome debugger just does not show the content script files in the UI when the file type is MHTML.
The content script is added via the standard declaration, for example:
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"run_at": "document_end",
"js": ["content.js"]
}
],
Notes:
it won't be shown in Chrome devtools -> Sources -> Content scripts panel which seems a bug.
MHTML has some restrictions so certain features may not work, use console.log everywhere.

Resources