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
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?
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: