1

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.

paolo.caminiti
  • 526
  • 4
  • 15

1 Answers1

5

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.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Sorry, for the late approval of the response, i've been away from this project for while. When using the page-mod to inject scripts, can i still use port.emit and port.on like i do with the tabs api? – paolo.caminiti Oct 09 '14 at 15:52
  • @paolo.caminiti Yes. Use the `onAttach` event to get a `worker`, then communicate with the tab's content script using `worker.port`. For an example, see https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/page-mod#Communicating_With_Content_Scripts – Rob W Oct 09 '14 at 15:54