2017-05-12 3 views
-1

MozillaアドオンにはURLとパターンを比較するMatchPattern APIがあります。私が探しているのは、固定URLパターンではなく、ユーザーが指定したリストです。 mozillaが提供するリンクの例は、ハードコーディングされたパターンを想定しています。変数matchをストレージ内のURLのリストを読み取るにはどうすればよいですか?Firefoxの拡張機能でユーザーが入力したパターンのリストの一致方法

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 
+0

あなたの質問は不明です。少なくとも、あなたが望むものの例を挙げてください。 – Makyen

+0

FYI:可能な限り、アドオンSDKの代わりに[WebExtensions](https://developer.mozilla.org/en-US/Add-ons/WebExtensions)を使用する必要があります。この時点で、WebExtensionベースの拡張機能のみがAMOのレビューとリスト作成のために受け入れられています(WebExtensionsに基づいていない既にリストされている拡張機能に更新を提供することはできます)。非WebExtensionsベースの拡張機能のサポートは、Firefox 57のリリース版から削除され、2017-11-14に予定されています。 – Makyen

答えて

0

これは非常に些細なものでした。単純に、参照URLを配列に格納し、パターンを作成して、配列アイテムのいずれかと一致するURLが見つかるまで、配列アイテムを1つずつ繰り返します。

matchではなくsearch機能を使用しました。作品は次のようになります:

//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 
関連する問題