Cant detect if PWA is installed on first page view? - google-chrome-extension

I have a desktop Chrome extension that I normally want to run on every domain. In manifest.json:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],
However I don't want it to run for installed PWAs. In my content script I have this check:
const isInstalled =
window.matchMedia('(display-mode: standalone)').matches ||
window.matchMedia('(display-mode: fullscreen)').matches ||
window.matchMedia('(display-mode: minimal-ui)').matches;
if (isInstalled) {
// Don't run
} else {
// Run
}
Technically it's still "running" but this type of check is OK for me. However there are 2 problems with this:
Firstly the check doesn't work the very first time the PWA is installed and opened, even if it does work after that.
Secondly, after the first view it does work for most PWAs that I tested (keep.google.com, www.soundslice.com, music.youtube.com) but for app.starbucks.com it doesn't work at all.

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.

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.

chome.tabs.executeScript from external file? What permissions do I need to change in manifest.json?

I'd like to run chrome.tabs.executeScript to run a file stored on a remote server (for testing purposes, that server is localhost). Here is the code I have so far:
chrome.tabs.executeScript(tabId, {file: 'http://localhost/js/myTestFile.js'}, function() {
//Do some stuff on completion
});
I know that by default, programmatically injected content scripts cannot be sourced from a remote location, but that you can "whitelist" certain sources in the manifest to change that. Here is my manifest at the moment:
//Extensions Permissions
"permissions": [
"activeTab",
"notifications",
],
//External access permissions
"externally_connectable": {
"matches": [
"http://localhost/*",
]
}
//Directories available to the extension
"web_accessible_resources": [
"js/*",
"html/*",
"img/*"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/lib/require-2.1.8.min.js", "js/extension/contentManager.js"],
"all_frames": true
}
],
In what way can I modify the manifest to allow a remote JS file to be injected as a content script?
You need to ask for "tabs" permissions to inject script into a page. Note that you can inject code or a link to the script. There may be an issue injecting a remote script, but you can pull down the JS and inject that directly into the page.
To include a remote script into the background page, you need to list the domain in content_security_policy and it must be https unless it is on localhost.

Resources