How to run a content_script without user click on WebExtension v3

78 views Asked by At

I'm new to WebExtensions and trying to port a minimal example called borderify from v2 to v3.

I tried the minimal step of setting manifest.json's manifest_version: 3, but I quickly learned this breaks the content script running automatically.

manifest.json

{
  "manifest_version": 3,
  "name": "Borderify",
  "content_scripts": [
    {
      "matches": ["*://*.mozilla.org/*"],
      "js": ["borderify.js"]
    }
  ],
  ...
}

borderify.js aka the content script

document.body.style.border = "5px solid red";

v2 works

v3 broken

On v3, it appears necessary to manually click the extension icon in the browser UI to actually run the content_scripts. This seems like a regression -- what am I missing?

How canĀ I get the content script to automatically load the old v2 way? It'd be ideal to avoid needing permissions, if possible. Is this behavior possible in v3?

Firefox 119.0, macOS 13.6

2

There are 2 answers

0
paws On

TIL there appears to be a new (mandatory?) permissions granting flow with v3 extensions.

After installing a v3 extension, when browsing to a domain that matches it's content_script's matches, the extension icon will get a dot:

Badge

and the user has to manually activate the extension in a separate click:

Manual opt-in

I can understand the argument for it but it's still adding annoying friction for non-technical users.

I hope to find if there's a way to avoid this extra manual step e.g. with host_permissions or some other trick.

4
Mehmet Baker On

Yes, you are right. Apparently, they have changed the behavior. The content scripts will not run unless the user explicitly allows running the extension for the specified hosts in the host_permissions.

There is a note on the Content Scripts page at Mozilla Developer Network:

Note: Content scripts are only executed if the extension is granted host permissions for the domain.

However, restricted domains can never be accessed, regardless of the granted permissions.

Starting with Manifest V3, host permissions are not automatically granted at install time. Users may opt in or out of host permissions after installing the extension.