3

I am unable to figure out how can I use regular expression match patterns in Firefox addons (using Add-on Builder). I tried using match-pattern package.

My main.js looks like this:

var { MatchPattern } = require("match-pattern");
//Matching all urls containing moz and chrome
var pattern = new MatchPattern(/.*moz.*/);
var pattern2 = new MatchPattern(/.*chrome.*/);

var pageMod = require("page-mod");
var data = require("self").data;

pageMod.PageMod({
    include: [pattern, pattern2],
    contentScriptWhen: 'ready',
    contentScriptFile: [data.url('jquery-1.7.1.min.js'),data.url('jquery-ui.js')]

});

But Error console is showing this error:

Timestamp: 3/19/2012 9:03:34 PM Error: An exception occurred. Traceback (most recent call last): File "resource://jid0-m6oqvn6bm6wcoo89bddsxwddkou-at-jetpack/api-utils/lib/match-pattern.js", line 87, in MatchPattern_test this.regexp.exec(urlStr)[0] == urlStr) TypeError: this.regexp.exec is not a function

What am I doing wrong here? Can I pass regexp match patterns in include array of pageMod? Any help would be appreciated.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Juzer Ali
  • 4,109
  • 3
  • 35
  • 62

1 Answers1

9

You don't need to use the match-pattern package directly, it is being used by the page-mod module internally. Just pass the regular expressions in the include parameter:

pageMod.PageMod({
    include: [/.*moz.*/, /.*chrome.*/],
    ...
});
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • 2
    Yeah, the ability to use regular regex objects / expressions is not as well advertised in the docs as it could be. – therealjeffg Mar 20 '12 at 16:50
  • Well, it's there in the documentation if you look closely but an example using regexps/lists of expressions in `page-mod` would be nice. – Wladimir Palant Mar 20 '12 at 17:19