Firefox WebExtensions WebRequest 示例不工作

Firefox WebExtensions WebRequest Example not working

我正在尝试使用 webRequest 示例 given here。这个简单的扩展应该将所有请求记录到控制台,但它并没有这样做。在我的实际代码中,我包含了一行将页面的边框颜色更改为红色并且确实有效,所以我知道扩展代码是 运行.

要加载扩展程序,我将 about:debugging 并通过 Load Temporary Add-on 按钮加载它

这是我的实际代码

manifest.json

{
    "description": "something something dark side",
    "manifest_version": 2,
    "name": "Interceptz",
    "version": "1.0",
    "icons": {
        "48": "icons/border-48.png"
    },

    "applications": {
        "gecko": {
            "id": "borderify@mozilla.org",
            "strict_min_version": "45.0"
        }
    },

    "permissions": [
        "webRequest",
    "*://*.mozilla.com/"
    ],

    "content_scripts": [
        {
            "matches": ["*://*.mozilla.org/*"],
            "js": ["intercept.js"]
        }
    ]
}

如您所见,我已经更改了 mozilla 页面上给出的示例的清单,因为复制和粘贴 json 而不做任何更改会导致 firefox 甚至无法加载扩展

intercept.js

document.body.style.border = "5px solid red";

function logURL(requestDetails) {
  console.log("Loading: " + requestDetails.url);
}

chrome.webRequest.onBeforeRequest.addListener(
  logURL,
  {urls: ["<all_urls>"]}
);

您不能将 webRequest API 放在内容脚本中。您需要将 intercept.js 分开,以将 manifest.js 重命名为 manifest.js 并修复权限。

manifest.js上:

{
"description": "something something dark side",
"manifest_version": 2,
"name": "Interceptz",
"version": "1.0",
"icons": {
    "48": "icons/border-48.png"
},

"applications": {
    "gecko": {
        "id": "borderify@mozilla.org",
        "strict_min_version": "45.0"
    }
},

"permissions": [
    "webRequest"
],

"background": {
  "scripts": ["background.js"]
 },

"content_scripts": [
    {
        "matches": ["*://*.mozilla.org/*"],
        "js": ["intercept.js"]
    }
]
}

intercept.js:

document.body.style.border = "5px solid red";

background.js:

function logURL(requestDetails) {
  console.log("Loading: " + requestDetails.url);
}

chrome.webRequest.onBeforeRequest.addListener(
  logURL,
  {urls: ["<all_urls>"]}
);

当然,还要添加 /icons/border-48.png

original tutorial of Mozilla 中棘手的部分是替换这一行:

browser.webRequest.onBeforeRequest.addListener

这一行:

window.chrome.webRequest.onBeforeRequest.addListener(

删除插件,重启浏览器,重新添加插件。查看工作示例 here