Extension manifest v3 content_scripts registered scripts are not run
Categories
(WebExtensions :: General, defect)
Tracking
(Not tracked)
People
(Reporter: czerny.jakub, Unassigned)
References
Details
Attachments
(3 files)
Description
Web extension content scripts registered using content_scripts
key in manifest version 3 are not executed. Scripts registered the same way using content_scripts
key in the manifest version 2 are run.
Weirdly enough once an extension (an unpacked extension loaded using "Load Temporary Add-on..." button at page about:debugging#/runtime/this-firefox) is loaded using manifest version 2 and later on re-loaded with manifest version being bumped to version 3, content scripts are run until Firefox is restarted.
Version
109.0 (64-bit), build id 20230112150232
Firefox beta 110.0b2 (64-bit), build id 20230117185909
Nightly 111, build id 20230116211903
Steps to reproduce
-
Create a demo extension
1.1. Create a directorydemo-extension
1.2. Create a filedemo-extension/manifest.json
containing{ "manifest_version": 3, "name": "script demo", "version": "1.0", "content_scripts": [{ "matches": ["<all_urls>"], "js": ["script.js"] }] }
1.3. Create a file
demo-extension/script.js
containingalert('foo')
-
Open
about:debugging#/runtime/this-firefox
in Firefox 110 -
Click "Load Temporary Add-on..." button and select the
demo-extension
directory -
Open DevTools (F12)
-
Open DevTools settings (F1)
-
Make sure "Enable browser chrome and add-on debugging toolboxes" is checked
-
In another tab open https://www.mozilla.org
Expected state
- Page https://www.mozilla.org is shown with an alert message.
- In DevTools of this page, tab "Debugger", left sidebar "Source" there is "Main Thread" > "script demo" > "script.js" item
Actual state
- Page https://www.mozilla.org is loaded with no alert message
- Sources sidebar of the Debugger tab of DevTools of the page doesn't show any item corresponding to the demo extension
It works fine in Chrome 108.
Comment 1•2 years ago
|
||
I couldn't manage to reproduce the issue on my end, I tried on both Win11 and Win10, on the Firefox Nightly and Firefox Release.
Every Time I accessed mozilla.org the alert message was shown and script.js was displayed in the debugger. (https://imgur.com/a/37CqI1B)
Could you please try if the issue occurs in safe mode. Here is a link that can help you do that: https://support.mozilla.org/en-US/kb/troubleshoot-firefox-issues-using-safe-mode
Thanks.
Reporter | ||
Comment 2•2 years ago
|
||
Hello Hani,
the problem seems to be the the prompt for granting the host permission - screenshot. I was not aware of that - the little green highlighting dot was not enough for me. IMO the UX of host permissions is sub-optimal and can be improved by a bubble popup appearing every time a host permission is required for an extension. Similarly to popup bubble that used to be there to notify on page requesting notification permission / is there for location location permission. This can be applied at least temporary for couple of releases so that users can accommodate to this new feature.
Nevertheless I still can reproduce the problem using steps to reproduce from comment 0 since it doesn't mention granting of the host permission. In all of my Firefox installations the host permission level "Only when clicked" seems to be the default. For the extension script to run automatically when a page is loaded - as steps to reproduce suggest - I need to change the permission level to "Always allon on www.mozilla.com" in extensions popup panel or grant permission "Access your data for all websites" to the extension on page about:addons
. Is there perhaps some configuration that is able to set the default level for all host permissions that you have set?
Comment 3•2 years ago
|
||
Comment 4•2 years ago
|
||
(In reply to Hani Yacoub, Desktop QA from comment #1)
I couldn't manage to reproduce the issue on my end, I tried on both Win11 and Win10, on the Firefox Nightly and Firefox Release.
Every Time I accessed mozilla.org the alert message was shown and script.js was displayed in the debugger. (https://imgur.com/a/37CqI1B)Could you please try if the issue occurs in safe mode. Here is a link that can help you do that: https://support.mozilla.org/en-US/kb/troubleshoot-firefox-issues-using-safe-mode
Thanks.
You may be using an older version of Firefox, or you clicked on the extension icon.
See a Minimal, Reproducible Example:
manifest.json
{
"author": "Guilherme Nascimento",
"name": "content_scripts MCVE",
"description": "content_scripts MCVE",
"version": "0.0.1",
"manifest_version": 3,
"icons": { "16": "icon.png" },
"action": { "default_icon": "icon.png" },
"content_scripts": [{
"matches": ["*://*.mozilla.org/*"],
"js": ["inject.js"]
}]
}
inject.js:
console.log('WORKING!');
When navigating to a mozilla website it is necessary to click on browser.action
(Manifest V3) for the script to be injected, see result in https://bugzilla.mozilla.org/attachment.cgi?id=9327281
Comment 5•2 years ago
|
||
Note: issue has been reproduced in Firefox 111.0.1 (Windows 64-bits).
Comment 6•11 months ago
|
||
Comment 7•11 months ago
|
||
I reproduced it in my environment as well.
This issue is caused by a lack of permissions.
In order for content_scripts
to work, you need to open permissions on the Manage Extension screen and configure settings.
https://bugzilla.mozilla.org/attachment.cgi?id=9390760
Alternatively, you may need to do the following with your extension:
Step 1. Add host_permissions in manifest.json
"host_permissions": [
"*://*.mozilla.org/*"
]
Step 2. Request permissions
Unlike Google Chrome, Firefox does not prompt users for host_permissions
during installation.
Therefore, you must make the permission request yourself.
const manifest = chrome.runtime.getManifest();
const permissions = { "origins": manifest.host_permissions };
if (!await chrome.permissions.contains(permissions)) {
await chrome.permissions.request(permissions);
}
Requested permissions and user prompts
Most browsers treat host_permissions as optional. If you request permissions using this key, users may get prompted to grant those permissions during installation. As of June 2023, Safari, Firefox, and some Chromium-based browsers don't prompt the user during installation .
host_permissions - Mozilla | MDN
Comment 8•6 months ago
|
||
Marking report as invalid because content scripts can run, provided that host permissions have been granted.
Relatively recently, from Firefox 127, host permissions are granted at install time (bug 1889402) (but not new ones on update). That should reduce the severity of this bug.
Description
•