【问题标题】:Chrome extension JavaScript eventlistener not working in content scriptchrome扩展javascript事件监听器在内容脚本中不起作用
【发布时间】:2015-09-24 01:17:06
【问题描述】:

我正在尝试将事件侦听器添加到我正在开发的 chrome 扩展的内容脚本中。

我的内容脚本:

   console.log("test 1");
document.addEventListener('DOMContentLoaded', function () {
    console.log("test 2");
});

我知道内容脚本正在运行,因为打印了第一条消息。但是,事件侦听器永远不会为我触发。

我错过了什么?我觉得好像有点傻。

我的清单.json

{
    "manifest_version": 2,

    "name": "test",
    "description": "testtesttesttest",
    "version": "1.0",
    "permissions": [
        "tabs", "http://*/*", "https://*/*"
    ],
    "browser_action": {
        "default_popup": "popup.html",
        "default_title": "Settings"
    },
    "permissions": [
    "tabs", "http://*/*", "https://*/*"
    ],
    "content_scripts": [{
        "matches": ["http://*/*","https://*/*"],
        "js": ["colorLink.js"],
        "run_at": "document_end", 
        "all_frames": true
    }]
}

【问题讨论】:

  • 哦!这就说得通了。你能写这个作为答案吗?我会选择它作为正确的。

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


【解决方案1】:

内容脚本在 DOMContentLoaded 之后发生在 "run_at": "document_end" 注入。在这种情况下,您不需要事件监听器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-30
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多