Javascript chrome.webRequest.onBeforeRequest.addListener无法读取属性';onBeforeRequest';未定义的
我正在尝试构建自己的chrome扩展,并尝试使用Javascript chrome.webRequest.onBeforeRequest.addListener无法读取属性';onBeforeRequest';未定义的,javascript,jquery,google-chrome,google-chrome-extension,webrequest,Javascript,Jquery,Google Chrome,Google Chrome Extension,Webrequest,我正在尝试构建自己的chrome扩展,并尝试使用onBeforeRequest添加事件处理程序 Mymanifest.json: { "manifest_version": 2, "name": "My extension", "description": "some descrpition", "version": "1.0", "permissions": [ "activeTab", "tabs",
onBeforeRequest
添加事件处理程序
Mymanifest.json
:
{
"manifest_version": 2,
"name": "My extension",
"description": "some descrpition",
"version": "1.0",
"permissions": [
"activeTab",
"tabs",
"webRequest",
"webNavigation",
"management",
"http://*/*",
"https://*/*"
],
"background": {
"scripts": [
"js/jquery-2.1.4.min.js",
"js/background.js"
],
"persistent": true
},
"browser_action": {
"default_icon": "imgs/img.png",
"default_title": "extension"
},
"icons" : {
"64" : "imgs/vergrootglas.png"
}
}
Mybackground.js
:
function callback(param1,param2,param3){
alert(param1);
alert(param2);
alert(param3);
}
//alert("test");
chrome.webRequest.onBeforeRequest.addListener(callback);
我把这个装进了我的chrome里。但每次我在控制台中收到以下信息:
未捕获的TypeError:无法读取的属性“onBeforeRequest”
未定义
我不知道我错了什么,我发现了:
但代码的示例似乎与我所做的完全相同。
我在这里遗漏了什么?上面的评论对我来说毫无意义。上面有一个背景脚本,它似乎有适当的权限。。。一些额外的评论可能会有帮助 您需要在清单文件中添加背景页面,并在清单中添加适当的权限,以便背景页面可以访问WebRequestAPI。请参见此示例: 正如Mihai提到的,如果需要让内容脚本执行操作,请查看以下页面: 将其添加到内容脚本中(您可以将问候语更改为动作,并将问候语更改为背景脚本应执行的动作): 将此添加到您的背景页面(如果语句和PEF根据消息形成不同的操作,您可以这样做):
我只是遇到了一个类似的问题,除了这个问题对我来说与权限无关,它实际上是一个网页问题
我的背景脚本意外地与我的内容脚本捆绑在一起。后台js导出内容脚本使用的某些常量,导致仅限于后台文件的方法(如
chrome.webRequest
)作为内容脚本的一部分被调用,因此undefined…(未定义)的cannot read属性不能在内容脚本中这样做,那么我应该怎么做呢?使用chrome扩展API提供的任何方式在扩展脚本和内容脚本之间进行通信(我只知道firefox插件对不起,它们使用?.port.emit
和和?.port.on
在哪里?是self
还是其他什么-不确定chrom的工作原理是否相同way@FIA2008,这没有任何意义。您可以上传扩展名并将其链接到此处的注释中吗?也可以使用console.log
而不是alert
。
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});