Can't get MatchPattern

For the sake of understanding MatchPattern, I am trying this SDK code (because it does not work in webextensions). I need to write a pattern for matching any URL of the form:

http://[anything.]myArray[index][/anything]

Where the [anything.] and [/anything] are optional. The code gives errors and does not work. For example, this an error that I’m getting:

The match result of this is code is always false (no match is found). Can you clarify what I am missing? Is the RegEx written in a wrong format?

This is the code I am running:

index.js:

var { MatchPattern } = require("sdk/util/match-pattern");
var { Cc,Ci,Cu,CC } = require("chrome");
Cu.import("resource://gre/modules/MatchPattern.jsm");
Cu.import("resource://gre/modules/BrowserUtils.jsm");

var myArray = ["facebook.com", "google.com"];


var link="http://www.google.com/xys";

for (var x=0; x<myArray.length; x++)
{
	var match = new MatchPattern("(http:\\/\\/)(.*\\.)*"+"("+myArray[x]+")"+"(\\/.*)*(\\/)*");
	console.log("loop: "+x);
	var uri=BrowserUtils.makeURI(link);

	console.log("match result is: "+match.matches(uri));
}//end for loop

and the package.json:

{
  "title": "My Jetpack Addon",
  "name": "test",
  "version": "0.0.1",
  "description": "A basic add-on",
  "main": "index.js",
  "engines": {
    "firefox": ">=38.0a1",
    "fennec": ">=38.0a1"
  },
  "license": "MIT",
  "keywords": [
    "jetpack"
  ]
}

Match patterns use a specific format. The MDN document clearly explains how it works.

For example:
var myArray = ["*://*.facebook.com/", "*://*.google.com/"];

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Match_patterns

But, what if I want to go through a list of domains so I want the domain name to be myArray[x] ?? is this possible? the format they are specifying does not contain variables and when I tried it did not work with me. Is there any way?

Then you have to sort them using RegEx and not Match Pattern.

I want to prepare list of patterns to use them in the filter array in:

Will an array with RegEx strings work? As I understand I have to use MatchPattern but I might be wrong. Please, correct me if I’m wrong.

With the webRequest APIs, you have to use match patterns.

It is possible to convert match patterns to regular expressions:


But not the other way around (match patterns can only express a subset of what regular expressions can).
But you can convert your array of domains into match patterns, according to the http://[anything.]myArray[index][/anything] rule:

const domains = [ 'example.com', 'mozilla.org', 'google.co.uk', ];
const patterns = domains.map(domain => `*://*.${ domain }/*`);

I also included the https protocol. See the second example.