0

What I'm trying to do

It's an extension that, once the page loads, hides a div that contains a child with class 'example'.

What happens

Nothing. On the js page anything that I place is ignored. I have tried using the console and creating page alerts but nothing will work.

Code

manifest.json:

{
"name": "Example",
"version": "1.0",
"default_locale": "en",
"permissions": [
    "http://example.com/*"
    ],
"content_scripts": [
    {
        "matches": [
            "http://example.com/*"
        ],
        "js": [
            "js/jquery/jquery.js",
            "src/inject/inject.js"
        ]
    }
]
}

src/inject/inject.js:

chrome.extension.sendMessage({}, function(response) {
var readyStateCheckInterval = setInterval(function() {
    if (document.readyState === "complete") {
        clearInterval(readyStateCheckInterval);

                    //Nothing here actually happens
                    alert("Extension loaded");
                    console.log("Extension loaded");

                    //The script that I *want* to happen
                    $('.example-stamp').parent().parent.().parent().hide();
    }
}, 10);
});

Notes

The jquery.js file is the latest download from jQuery.com. There are no errors when loading the unpacked extension into Chrome, and nothing - not even errors - appear in the console.

I have been testing it on http://www.example.com.

4

2 回答 2

0
  • chrome.extension.sendMessage用于从内容脚本向后台页面或其他扩展页面发送消息。由于您仅定义内容脚本,因此不会收到该消息,并且您的回调函数将永远不会被调用。
  • 通常你不需要等待文档完成状态来操作 DOM。默认情况下,Chrome 会document_idle在文档准备好和文档完成后之间的某个时间运行您的内容脚本。见Content Scriptsrun_at定义中的 manifest 参数
  • 如果您确实需要在页面加载完成后运行代码,您可以设置run_atdocument_start监听窗口加载事件。当您使用 jQuery 时,您可以执行以下操作:
$(window).on("加载", function() {
    console.log("已加载扩展");
    $('.example-stamp').parent().parent.().parent().hide();
});
于 2013-09-08T13:54:04.100 回答
0

怎么修

改变

"content_scripts": [
    {
        "matches": [
            "http://example.com/*"
        ],

"content_scripts": [
    {
        "matches": [
            "http://*.example.com/*"
        ],

为什么

答案与代码无关,js而是与清单有关。

content-script出于某种原因,除非json 包含该字段http://*.example.com/*,否则 javascript 根本不会注入,因为http://example.com它本身不起作用,即使那标头 URL。

于 2013-09-09T11:09:28.590 回答