Chrome扩展程序:Javascript内容脚本无权访问HTML元素

时间:2014-01-14 04:35:29

标签: javascript google-chrome-extension

我在SO中看过一些类似的线程,但我无法理解解决方案。 这是我的代码:

的manifest.json

{
    "name": "Test  5",
    "version": "1.0.0",
    "manifest_version": 2,
        "permissions": [
        "storage",
        "tabs",
        "http://*/*"
    ],
    "web_accessible_resources": ["jquery-2.0.3.min.map"],
    "content_scripts": [
        {
            "matches": [
                "*://*.google.es/*"
            ],
            "js":         ["jquery.js", "content_scripts.js"],
            "run_at":     "document_end",
            "all_frames": true
        }
    ]
}

content_scripts.js

var doFilter = function() { 
    var classR = document.getElementsByClassName("rc");
    console.log("2");
    console.log("classR: "+classR.length );
        //...etc
}

$(window).load(function() {
    console.log("1");
    doFilter(document.body);    
});

代码已作为Chrome解压扩展程序加载。我在Chrome中使用Dev Tools从我PC中的本地文件进行调试。

在与Web浏览器相同的标签页中打开Dev Tools时,我浏览到“www.google.es”并搜索一些关键字。 然后我就可以在Dev Tools控制台日志中看到了。问题是最后一个显示classR的长度为0.
enter image description here enter image description here

如您所见,按类名“rc”存在元素,所以看起来似乎是

  

document.getElementsByClassName( “RC”)

实际上没有访问加载的HTML文档。 我在这里错过了什么? 感谢。

0 个答案:

没有答案