Firefox Nightly Android add-on content script loading only after opening add-on popup

234 Views Asked by At

I created an add-on that manipulates a speicfic site’s HTML based on the settings the user chooses in the add-on’s 'popup' (the settings are saved with ‘chrome.storage.local’, I just ported my code from chrome and it worked).

When the user opens a specific website, the add-on is supposed to run a continuous (interval) content script that changes the HTML, the problem is that it doesn’t, it requires the user to load the site, then open the add-on 'popup', then return to the site, and then the script starts working.

How can I fix that? How do I make the script run when the user opens the site? This is a problem only on android, not chrome or desktop firefox

Here's the manifest.json-

{
    "name": "9gag post filter",
    "description": "A post filter for 9GAG.com",
    "version": "1.1.2",
    "browser_specific_settings": {
        "gecko": {
          "id": "[email protected]",
          "strict_min_version": "42.0"
        }
    },
    "manifest_version": 3,
    "permissions": [
        "storage"
    ],
    "action": {
      "default_popup": "index.html"
    },
    "content_scripts": [{
        "css": ["assets/css/content.css"],
        "js": ["assets/js/jquery_slim_mini.js","assets/js/content.js"],
        "matches": ["https://9gag.com/*"]
      }]
}

And the whole source code (it's open source)

2

There are 2 best solutions below

0
On

This issue occurs not only on Android, but also on Desktop Firefox with newly installed extensions.
(This issue does not occur if you upgrade from Manifest v2)

This issue is caused by a lack of permissions. Firefox does not automatically grant execute permissions to the content_scripts domain in Manifest v3.

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

For now, you must specify host_permissions and request permissions yourself.

"host_permissions": [
    "https://9gag.com/"
]
const manifest = chrome.runtime.getManifest();
const permissions = { "origins": manifest.host_permissions };
if (!await chrome.permissions.contains(permissions)) {
      await chrome.permissions.request(permissions);
}

Detail: 1810910 - Extension manifest v3 content_scripts registered scripts are not run

0
On

The only solution I've found to this is to edit the manifest and go back to Manifest v2 (using mv2 examples like the old the Beastify demo). Of course that is less than desirable since it breaks compatibility with Chromium-based browsers, but it's worth mentioning I guess.