Bra*_*don 9 javascript google-chrome google-chrome-extension
我正在编写我的第一个Google扩展程序代码,我收到了一个我一生中从未见过的奇怪错误.
Uncaught TypeError: Cannot read property 'onCompleted' of undefined background.js:4
(anonymous function)
Run Code Online (Sandbox Code Playgroud)
我是Javascript顺便说一句新手(尽管有广泛的Java知识).
我的代码看起来像这样:
chrome.webNavigation.onCompleted.addListener(function(){//error right here
if(hasHostSuffix(document.URL,'mysite.com'))
{
console.log("Web navigation commited!");
var links = document.getElementsByName("a");
for(var i = 0; i < links.length; i++)
{
console.log(links[i].getAttribute("href"));
if(links[i].style.backgroundColor='#272727')
{
links[i].className="gone";
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我无法找到有关我的任何可靠信息我收到此错误.
任何帮助将非常感谢:)
编辑:
引起我的注意,我忘记了一些重要的信息xD我的manfiest:
{
"manifest_version": 2,
...,
"permissions": [
"webNavigation"
],
...,
"content_scripts":[
{
"matches":["http://www.mysite.com/*"],
"css":["ur_customstyle.css"],
"js":["background.js"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
rsa*_*hez 12
chrome.webNavigation,因为大多数chrome.*API,只能从扩展的后台脚本访问,而不能从内容脚本访问.虽然您的文件已命名background.js,但您的清单显示您正在将其用作内容脚本.
在这种情况下使用内容脚本是正确的,因为您需要与DOM进行交互.从您发布的代码片段中,您似乎不需要使用webNavigation API.您只需在清单中设置内容脚本run_at: document_end,它就会在DOM完成后立即运行.查看http://developer.chrome.com/extensions/content_scripts.html
Ero*_*lin 11
在@rsanchez回答之上,如果您chrome.webNavigation在扩展程序的后台脚本中调用了a ,但仍然有此错误,则可能还需要在您的扩展中添加webNavigation权限manifest.json.
"permissions": [
"webNavigation"
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6242 次 |
| 最近记录: |