我正在编写可以记录所有HTTP请求的chrome扩展,就像控制台窗口中的网络选项卡中的请求一样。
清单文件如下
{
"manifest_version": 2,
"name": "Youtube Controls",
"description": "This extension demonstrates a browser action with kittens.",
"version": "1.0",
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"content_scripts":[{
"matches": ["https://www.youtube.com/*"],
"js":["youtube.js"]
}],
"background":{
"scripts": ["wb.js"]
},
"browser_action": {
"default_icon": "youtube.png",
"default_popup": "popup.html"
}
}
youtube.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
chrome.runtime.sendMessage({
greeting: 'hello'
});
});
wb.js(后台脚本)
chrome.runtime.onMessage.addListener(function(request, sender, response){
console.log(request);
chrome.webRequest.onCompleted.addListener(function(details){
console.log(details);
},{
urls: ["<all_urls>"]
});
});
如果我删除chrome.webRequest.onCompleted.addLister()
我可以看到console.log正在运行,但如果webRequest脚本在那里,它会在控制台中抛出错误。这是错误:
Error in event handler for (unknown): Cannot read property 'error' of undefined
Stack trace: TypeError: Cannot read property 'error' of undefined
at chrome-extension://ammnjfgoofnjeoglmbhbeijmmnmfjbdk/popup.js:13:30
at disconnectListener (extensions::messaging:335:9)
at EventImpl.dispatchToListener (extensions::event_bindings:397:22)
at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:93:26)
at EventImpl.dispatch_ (extensions::event_bindings:379:35)
at EventImpl.dispatch (extensions::event_bindings:403:17)
at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:93:26)
at dispatchOnDisconnect (extensions::messaging:290:27) extensions::uncaught_exception_handler:9
有什么问题?如何解决?