Chrome 扩展 JavaScript 事件监听器在内容脚本中不工作
Chrome extension JavaScript eventlistener not working in content script
我正在尝试为我正在开发的 Chrome 扩展程序向我的内容脚本添加一个事件侦听器。
我的内容脚本:
console.log("test 1");
document.addEventListener('DOMContentLoaded', function () {
console.log("test 2");
});
我知道内容脚本正在运行,因为第一条消息已打印。但是,事件侦听器永远不会为我触发。
我错过了什么?我觉得好像有点傻。
我的manifest.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
}]
}
内容脚本在 "run_at": "document_end"
处注入,发生在 DOMContentLoaded 之后。在这种情况下,您不需要事件侦听器。
我正在尝试为我正在开发的 Chrome 扩展程序向我的内容脚本添加一个事件侦听器。
我的内容脚本:
console.log("test 1");
document.addEventListener('DOMContentLoaded', function () {
console.log("test 2");
});
我知道内容脚本正在运行,因为第一条消息已打印。但是,事件侦听器永远不会为我触发。
我错过了什么?我觉得好像有点傻。
我的manifest.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
}]
}
内容脚本在 "run_at": "document_end"
处注入,发生在 DOMContentLoaded 之后。在这种情况下,您不需要事件侦听器。