chrome.webNavigation未定义 [英] chrome.webNavigation undefined

查看:136
本文介绍了chrome.webNavigation未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了我的第一个Google扩展程序代码,并且遇到了一个我从未见过的奇怪错误。

  Uncaught TypeError:无法读取未定义background.js的属性'onCompleted':4 
(匿名函数)

我对Javascript btw很体面(对Java有丰富的知识)。



我的代码如下所示:

  chrome.webNavigation.onCompleted.addListener(function(){//错误在这里
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;
}
}
}
});

我一直无法找到任何可靠的信息, >

编辑:



  {
manifest_version :2,
...,
权限:[
webNavigation
],
...,
content_scripts:[
{
matches:[http://www.mysite.com/*],
css:[ur_customstyle.css],
js:[background.js]
}
]
}


解决方案

chrome.webNavigation ,因为大多数chrome。* API只能从扩展的后台脚本访问,而不是来自内容脚本。虽然您的文件名为 background.js ,但您的清单显示您将其用作内容脚本。



在这种情况下使用内容脚本是正确的,因为您需要与DOM进行交互。从您发布的代码片段看来,您似乎不需要使用webNavigation API。您可以简单地将您的清单中的内容脚本设置为 run_at:document_end ,并在DOM完成后立即运行。检查 http://developer.chrome.com/extensions/content_scripts.html


I am writing my first Google extension code, and I am getting a weird error I have never seen in my life.

Uncaught TypeError: Cannot read property 'onCompleted' of undefined background.js:4
(anonymous function)

I am decently new to Javascript btw (have extensive knowledge in Java though).

My code looks like this:

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";
        }
    }
}
});

I haven't been able to find any solid information on my I am getting this error.

Any help would be highly appreciated :)

EDIT:

Was brought to my attention that I forgot some vital info xD My manfiest:

{
"manifest_version": 2,
...,
"permissions": [
    "webNavigation"
],
...,
"content_scripts":[
    {
        "matches":["http://www.mysite.com/*"],
        "css":["ur_customstyle.css"],
        "js":["background.js"]
    }
]
}

解决方案

chrome.webNavigation, as most chrome.* APIs, can only be accessed from the background script of an extension, not from content scripts. Although your file is named background.js, your manifest shows that you are using it as a content script.

It is right to use a content script in this case because you need to interact with the DOM. From the fragment of code you posted, it seems that you don't need to use the webNavigation API. You can simply set your content script in your manifest to run_at: document_end, and it will run as soon as the DOM is complete. Check http://developer.chrome.com/extensions/content_scripts.html

这篇关于chrome.webNavigation未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆