Search code examples
google-chrome-extensionfirefox-addonfirefox-addon-sdkbrowser-addons

Firefox addon development - tabs api - blacklist url patterns from inject the exstension code (like in chrome)


I am using the addons sdk/tabs api to inject content scripts into tabs when they are loaded like this:

tabs.on(ready, function (tab) {
  var worker = tab.attach({
    contentScriptWhen: 'end',
    contentScriptFile: myAwesomeArrayOfScripts
  });

  ...

Is there an easy way i can prevent the attach on domain patterns? Most importantly i need to prevent it to work on about: domains like the new tab page of firefox.

Obviously i'm able to control execution with code like this:

if (tab.url.indexOf('about:') === 0) return;

But it looks very unclear solution compared to chrome declarative manifest, where you can have this:

"content_scripts": [
  {
    "matches": [ "http://*/*", "https://*/*", "file://*/*" ],

Is there anything similar? Firefox documentation is confusing... too many things and too many versions and articles from the past.


Solution

  • Use the sdk/page-mod module instead of attaching content scripts via tabs events if you want to declare a content script. Then you can use the includeand exclude keys to specify URL patterns (exclude was introduced in Firefox 32).

    Using Firefox's tab.attach is similar to chrome.tabs.executeScript, which should only be used to imperatively execute a content script.