javascript - 在页面加载

时间:2016-06-15 08:18:35

标签: javascript jquery google-chrome google-chrome-extension

我不想在用户访问的网页完成加载后立即运行我的chrome扩展程序。

如果我点击它,扩展工作正常,但我希望它在页面加载时自动运行。

有没有办法做到这一点?

这是我的清单

{
    "manifest_version": 2,

    "name": "Simple Text Highlighter",
    "description": "This plugin notify of the word 'Simple' in the current page",
    "version": "1.0",

    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },

    "content_scripts": [{
        "matches": ["http://*/*", "https://*/*"],
        "js": ["jquery.min.js", "highlightSimple.js"],
        "css": ["highlight.css"],
        "run_at": "document_end"
    }],

    "permissions": [
        "tabs",
        "activeTab",
        "notifications",
        "http://*/",
        "https://*/"
    ]

}

这是JavaScript

(function () {
    chrome.tabs.executeScript(null, {
        file: 'highlight.css'
    }, function () {
        chrome.tabs.executeScript(null, {
            file: 'jquery.min.js'
        }, function () {
            chrome.tabs.executeScript(null, {
                file: 'replace.js'
            });
        });
    })
})();

提前致谢。

1 个答案:

答案 0 :(得分:0)

您已设置run_at字段document_end,这意味着您的脚本会在页面加载后立即注入。

  

在DOM完成之后立即注入文件,但是在加载了像图像和帧之类的子资源之前。

chrome.tabs.executeScript只能在扩展上下文中调用,内容脚本中不允许这样做。

相关问题