是否可以在webRequest目标中拥有多个URL

时间:2017-06-20 11:24:39

标签: firefox firefox-addon firefox-webextensions

是否可以在webRequest的urls参数中包含多个目标。怎么样?

这是一个不起作用的例子并且给了我这个错误:

The webRequest.addListener filter doesn't overlap with host permissions.

**** ****修订:

这是示例代码main.js

myArray=["yahoo.com", "google.co.uk"];
patterns=[];
for (var x=0; x<myArray.length; x++)
{
    var aURL="\"https://*."+myArray[x]+"/*\""
    patterns.push(aURL);
}//end for

console.log("the array: "+patterns);


function logURL(requestDetails)
{
    console.log("inside logURL");
    console.log("*******************************");
    console.log("Loading: " + requestDetails.url);
    console.log("*******************************");
}//end logUTL

browser.webRequest.onBeforeRequest.addListener(
  logURL,
  {urls: patterns,
   types: ["main_frame"]}
);

这是清单:

{
    "manifest_version": 2,
    "name": "test",
    "version": "1.0",
    "description": "",
    "background": {
    "scripts": ["main.js"]
    },

    "icons": {
    "64": "icons/myicon.png"
    },

    "permissions":[
    "<all_urls>",
    "activeTab",
    "tabs",
    "activeTab",
    "webRequest"
    ]
}

2 个答案:

答案 0 :(得分:1)

您必须使用数组作为模式。

myArray=["yahoo.com", "google.co.uk"];
patterns=myArray.map(item=>`https://*.${item}/*`)

console.log("the array: "+patterns);

function logURL(requestDetails)
{
    console.log("inside logURL");
    console.log("*******************************");
    console.log("Loading: " + requestDetails.url);
    console.log("*******************************");
}//end logUTL

browser.webRequest.onBeforeRequest.addListener(
  logURL,
  {urls: patterns,
   types: ["main_frame","xmlhttprequest"]}
);

addListener的第二个参数是RequestFilter

答案 1 :(得分:0)

是否有理由添加双引号?

-var aURL="\"https://*." + myArray[x] + "/*\""
+var aURL="https://*." + myArray[x] + "/*"