Javascript 如何匹配用户在firefox扩展中输入的模式列表
Mozilla插件有一个API,可以将URL与模式进行比较。我寻找的不是一个固定的URL模式,而是一个由用户给出的列表。mozilla提供的链接中的示例采用了硬编码模式。如何使变量Javascript 如何匹配用户在firefox扩展中输入的模式列表,javascript,firefox,firefox-addon,firefox-addon-sdk,Javascript,Firefox,Firefox Addon,Firefox Addon Sdk,Mozilla插件有一个API,可以将URL与模式进行比较。我寻找的不是一个固定的URL模式,而是一个由用户给出的列表。mozilla提供的链接中的示例采用了硬编码模式。如何使变量匹配读取存储中的URL列表 var match = new MatchPattern("*://mozilla.org/"); var uri = BrowserUtils.makeURI("https://mozilla.org/"); match.matches(uri); // < true
匹配
读取存储中的URL列表
var match = new MatchPattern("*://mozilla.org/");
var uri = BrowserUtils.makeURI("https://mozilla.org/");
match.matches(uri); // < true
uri = BrowserUtils.makeURI("https://mozilla.org/path");
match.matches(uri); // < false
var match=new MatchPattern(“*://mozilla.org/”;
var uri=BrowserUtils.makeURI(“https://mozilla.org/");
匹配。匹配(uri);//<真的
uri=BrowserUtils.makeURI(“https://mozilla.org/path");
匹配。匹配(uri);//<假的
这一切变得如此微不足道。简单地说,通过将引用url存储在一个数组中,然后构建一个模式,逐个迭代数组项,直到找到与任何数组项匹配的url
我最终使用了search
函数,而不是match
。这件作品看起来像这样:
//the url to be tested.
var url="www.example.com";
for (var x=0; x<myArray.length; x++) //loop over the list
{
//for a constructing a RegEx in a string.
//the following says the pattern startes with myArray[x], followed by "/[anything]/"
//We use "\\" in the string to represent "\" in the Regex.
//We write "\\/" in the string to represent the Regex "\/"
var pattern= "("+myArray[x]+")"+"(\\/.*)*(\\/)*";"
//test if the pattern equals the value
if(url.search(pattern)==0)
console.log("url matched");
else
console.log("url did not match");
}//end for
//要测试的url。
var url=“www.example.com”;
对于(var x=0;xy),您的问题不清楚。请至少提供您想要的示例。仅供参考:如果可能的话,您应该使用附加SDK,而不是附加SDK。到目前为止,只有基于WebExtension的扩展被接受审查并在AMO上列出(您仍然可以提供已列出的不基于WebExtensions的扩展的更新)。自Firefox 57发布版本(计划于2017年11月14日发布)起,Firefox将不再支持基于非WebExtensions的扩展。