简体   繁体   English

动态生成的iframe内容上的Eventlistener不会触发

[英]Eventlistener on dynamically generated iframe content does not fire

I have a chrome extension that injects an iframe into a loaded page. 我有一个chrome扩展程序,可以将iframe注入已加载的页面。 The content of the iframe is programmatically generated. iframe的内容以编程方式生成。 It includes a button to which a click event listener is added. 它包括一个添加了单击事件侦听器的按钮。 But it does not always seem to fire. 但这并不总是会触发。 Most of the times it works, but one case in which it doesn't work is when the extension is freshly loaded (as in installed). 在大多数情况下,它可以工作,但其中一种不起作用的情况是,该扩展程序是刚装入的(如已安装)。 Below is the gist of my code, do you see any issue? 以下是我的代码的要旨,您看到任何问题吗?

content.js: content.js:

var frame = document.createElement("iframe");

frame.onload = function() {
    var doc = frame.contentDocument || frame.contentWindow.document;

    var closeButton = doc.createElement("input");
    closeButton.type = "button";
    closeButton.value = "X";
    closeButton.addEventListener("click", function() {
        document.body.removeChild(frame);
    });

    doc.body.appendChild(closeButton);
};

document.body.appendChild(frame);

manifest.json 的manifest.json

{
    "name": "eventlistener-bug",
    "version": "0.1",
    "content_scripts": [{
        "run_at": "document_idle",
        "matches": ["http://*/*", "https://*/*"],
        "js": ["content.js"]
    }],
    "permissions": ["http://*/*", "https://*/*"],
    "manifest_version": 2
}

So far, it looks like this is a bug in chrome. 到目前为止,这似乎是chrome中的错误。 It has been reported and accepted: https://bugs.chromium.org/p/chromium/issues/detail?id=745771 已被报告并接受: https : //bugs.chromium.org/p/chromium/issues/detail?id=745771

A fix is planned for version 61. 计划对版本61进行修复。

Possible workarounds are re-adding the event listener, eg in a mouseover event handler for the iframe. 可能的解决方法是重新添加事件侦听器,例如在iframe的鼠标悬停事件处理程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM