首页 > 解决方案 > Chrome的内容脚本默认不允许添加EventListeners?

问题描述

为了清楚起见,我阅读了多个类似的问题
1 2 3 4 5
,它们都与我的无关。

据我了解,WebAPI内容脚本即使在隔离环境中运行,仍然可以操作页面的 DOM 并添加 EventListener,至少 Developer.Chrome 和 MDN 是这么说的。但是,我遇到的情况似乎不是真的。

这是一个示例扩展:

清单.json

{
    "manifest_version": 2,
    "name": "Foo",
    "version": "0",
    "permissions":
    [
        "storage",
        "https://steamcommunity.com/*"
    ],
    "content_scripts":
    [ {

        "matches": ["*://steamcommunity.com/groups/*"],
        "js": ["content.js"],
        "run_at":    "document_idle",
        "all_frames": true
    } ]
}

内容.js

'use strict';

const butArea = document.querySelector(".grouppage_join_area");

function queueGroup()
{ 
    alert('yay!');
}

if (butArea)
{
    butArea.addEventListener("click", queueGroup, false);
}

加载解压并转到https://steamcommunity.com/groups/SteamClientBeta。点击他按钮,没有警报。通过控制台手动添加 eventListener ,它只是 werks。我也尝试click/onclick了属性,但结果相同。

是的,我可以将我的代码注入页面,这里没有问题,但我真的不想这样做,因为它应该按原样工作,不是吗?否则 - 为什么不,我错过了什么?

标签: javascriptgoogle-chromedomgoogle-chrome-extensionevent-listener

解决方案


推荐阅读