Skip to content

API for extensions to exclusion/deny list their content scripts #653

@Robbendebiene

Description

@Robbendebiene

Basic Use Case: Users often want an exclusion/allow list functionality to disable add-ons on certain URLs.

Goal:

  • run or not run code in tabs depending on user defined urls/patterns
  • browser action icon should reflect whether the code runs or does not run
  • ideally avoid the tabs permission as self-deactivation is not a main feature of an add-on. It is kinda contradictory that content script disabling requires loosening the permissions.

Attempts:

Block the content script injection:

Approach:

scripting.registerContentScripts() seems like a wonderful fit for this, because it provides the excludeMatches functionality wherefore no knowledge about the tab urls has to be exposed.

  1. Store the exclusions in browser.storage.
  2. Retrieve the exclusions in the background script.
  3. Inside the background script call scripting.registerContentScripts() (or scripting.updateContentScripts()) with the exclusions as excludeMatches to register the content script.

Problems:

  • scripting.registerContentScripts() does not inject the script into already existing tabs. Any workaround via scripting.executeScript() requires the tabs permission because it should only be called for tabs whose url is not listed in the exclusions.
  • Adding an url/pattern to excludeMatches (via scripting.updateContentScripts()) does not remove/disable already injected content scripts.

Drawbacks:

  • Detecting whether the content script is injected or not from the background page is only possible via messaging from/to the content script. So toggling the browser action icon state would be possible if the above problems were solved.

Block the code inside the content script:

Approach:

  1. Register the content script via the content_scripts manifest key
  2. Store the exclusions in the browser.storage.
  3. Retrieve the exclusions in the content script.
  4. Inside the content script check whether whether the tabs URL matches any exclusion and run or not run the main content script's code.

Problems:

  • No direct access to the tab url in iframes (requires passing the window.location.href to background page and then back to all frames) or using the privacy unfriendly postMessage API.
  • The browser action icon either requires passing the url from the content script to the background script since it does not have access to the tab url without the tabs permission, or some other messaging.

Drawbacks:

  • Unnecessary injection/workload

Conclusion:

So far only the latter approach really works, but for a seemingly simple functionality as whitelisting/blacklisting it is cumbersome to implement. Also it feels wrong to rely on content script code while the goal is to avoid any content script injection.

Possible solutions:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions