【问题标题】:How to match list of patterns entered by the user in firefox extension如何匹配用户在 Firefox 扩展中输入的模式列表
【发布时间】:2017-05-12 16:19:03
【问题描述】:

Mozilla 插件有一个MatchPattern API,可以将 URL 与模式进行比较。我寻找的不是固定的 URL 模式,而是用户给出的列表。 Mozilla 提供的链接中的示例采用硬编码模式。如何使变量match 读取存储中的 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

【问题讨论】:

  • 您的问题不清楚。请至少提供您想要的示例。
  • 仅供参考:如果可能,您应该使用 WebExtensions 而不是附加 SDK。到目前为止,只有基于 WebExtension 的扩展被接受审查并在 AMO 上列出(您仍然可以为已列出的不基于 WebExtensions 的扩展提供更新)。自 Firefox 57(计划于 2017 年 11 月 14 日发布)起,将从 Firefox 的发布版本中删除对基于非 WebExtensions 的扩展的支持。

标签: javascript firefox firefox-addon firefox-addon-sdk


【解决方案1】:

这变得如此微不足道。简单地说,通过将引用 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

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多