Search code examples
regexfirefox-addonfirefox-addon-sdkuserscriptscontent-script

how to exclude sites in a firefox addon contentscript using pagemod?


I'm working on a Firefox Addon version of a userscript. The addon syntax is really simple using the page-mod library. For example:

var data = require("self").data;
var pageMod = require("page-mod");
pageMod.PageMod({
  include: "*.example.com",
  contentScriptWhen: 'end',
  contentScriptFile: data.url("test.user.js")
});

This will run the script test.user.js for sites that match *.example.com. However in a typical userscript I can also specify an exclude line, so that I can easily bypass subdirectories of the included url. IOW, I want a line like this:

exclude: "*.example.com/somewhere",

But the pagemod docs don't list any such option (nor does it appear to exist). How would I specify an exclude in this case?

It looks like include can also accept a match-pattern built from a regex. However, I'm having trouble imagining the regex would match *.example.com but not *.example.com/subdir (but also *.example.com/othersubdirs).


Solution

  • It will not always be possible to do an exclude rule with regex in the include option, and it may get quite convoluted in those cases when it is possible.

    Probably the simplest, most robust way to do an exclude is to add it to your contentScriptFile.

    Something like this (untested):

    if (/^https?.*badSite\.net.*$/.test (location.href) )
        return;