我试图关注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>"]}
);
答案 0 :(得分:1)
您不能将webRequest API放在内容脚本中。您需要将 intercept.js 分开,将 manifest.js 重命名为 manifest.json 并修复权限。
<强>的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"
],
"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
答案 1 :(得分:0)
original tutorial of Mozilla 中棘手的部分是替换这一行:
browser.webRequest.onBeforeRequest.addListener
用这一行:
window.chrome.webRequest.onBeforeRequest.addListener(
删除插件,重启浏览器,再次添加插件。查看工作示例 here