Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript chrome.webNavigation未定义_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript chrome.webNavigation未定义

Javascript chrome.webNavigation未定义,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在写我的第一个谷歌扩展代码,我得到了一个奇怪的错误,我从来没有见过在我的生活 Uncaught TypeError: Cannot read property 'onCompleted' of undefined background.js:4 (anonymous function) 顺便说一句,我对Javascript非常陌生(虽然我对Java有丰富的知识) 我的代码如下所示: chrome.webNavigation.onCompleted.addListener(function(

我正在写我的第一个谷歌扩展代码,我得到了一个奇怪的错误,我从来没有见过在我的生活

Uncaught TypeError: Cannot read property 'onCompleted' of undefined background.js:4
(anonymous function)
顺便说一句,我对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";
        }
    }
}
});

chrome.webNavigation
,与大多数chrome.*API一样,只能从扩展的后台脚本访问,而不能从内容脚本访问。尽管您的文件名为
background.js
,但您的清单显示您正在将其用作内容脚本


在这种情况下使用内容脚本是正确的,因为您需要与DOM交互。从您发布的代码片段来看,似乎不需要使用WebNavigationAPI。您只需将清单中的内容脚本设置为
run\u at:document\u end
,DOM完成后它就会运行。检查@rsanschez answer顶部的,如果您在扩展的后台脚本中调用
chrome.webNavigation
,但仍然存在此错误,您可能还需要在
manifest.json
中添加
webNavigation
权限

"permissions": [
    "webNavigation"
  ]
|

您需要确保在
manifest.json
中使用
persistent:false注册扩展名

"background": {
    "scripts": [
        "./scripts/saving/extension/background.js"
    ],
    "persistent": false // <---- HERE!!!!!
}
“背景”:{
“脚本”:[
“/scripts/saving/extension/background.js”
],

“persistent”:false//webNavigation必须是未定义的。您在清单中添加权限了吗?-
chrome。webNavigation
的计算结果为
未定义
,因此您无法访问属性
onCompleted
。是否设置了此属性?(编辑:请参见上文)@Rick这是我的想法,但我有权限(只是忘了将我的舱单添加到我的post o.o中)。非常感谢。非常简单的问题,直到更多关于chrome的API。有没有办法在内容脚本中使用webNavigation?@KevenWang,没有。哇,谢谢!我以为这是我权限的问题,所以它没有定义,因为我的.manifest文件有问题,但实际上它只是试图在一个co中引用它文本脚本。
"background": {
    "scripts": [
        "./scripts/saving/extension/background.js"
    ],
    "persistent": false // <---- HERE!!!!!
}