0
我有一个chrome扩展,它将iframe注入加载的页面。 iframe的内容以编程方式生成。它包含一个添加了单击事件侦听器的按钮。但似乎并不总是如此。大多数情况下,它的工作原理是,但其中一种情况是无法正常工作的情况是扩展程序是新加载的(如已安装)。以下是我的代码的要点,您是否看到任何问题?关于动态生成iframe内容的Eventlistener不会触发
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
{
"name": "eventlistener-bug",
"version": "0.1",
"content_scripts": [{
"run_at": "document_idle",
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}],
"permissions": ["http://*/*", "https://*/*"],
"manifest_version": 2
}
当安装/更新扩展时,manifest.json中声明的内容脚本不会自动注入到现有的打开的选项卡中。您需要手动执行此操作,例如[Chrome扩展内容脚本在升级或安装后重新注入](// stackoverflow.com/a/11598753) – wOxxOm
@wOxxOm问题是__not__内容脚本根本没有加载。只有点击事件侦听器不总是工作。 – jjdoe
它只发生在FF吗?我知道它与iframe有一些奇怪的时间问题,它们要求我先将iframe附加到文档正文,然后继续在setTimeout回调中访问其内容。 – wOxxOm