我正在编写一个Chrome扩展程序。它用于记录用户在浏览网页时的行为。通过使用Chromecontent script
,将事件监听器添加到客户的网页中来实现这一点。
content script
中的代码如下:
var recordingEvents = ['click', 'input', 'change'];
recordingEvents.forEach(function (e) {
window.addEventListener(e, handler, true);
});
自定义页面示例:
<script>
function reload() {
var ifrw = document.getElementById("iframeResult").contentWindow;
ifrw.document.open();
ifrw.document.write("<div>abc</div>");
ifrw.document.close();
}
</script>
<body>
<input type="submit" onclick="reload();" value="Reload" />
<iframe id="iframeResult"></iframe>
</body>
它使用document.open
、document.write
来重写iframe的内容。
问题在于,我的事件监听器附加到window
对象上。而document.open
会删除所有事件监听器。如下图所示。
有没有办法避免document.open
删除事件监听器?或者观察document.open
,这样我就可以手动重新添加监听器了呢?
document.open
。 - SLakswindow.setTimeout
和document.write
呢? - Jonathan Graydocument.open
被调用在客户的页面上,这一点并不清楚。现在我想知道你尝试让代码工作的哪一部分,它应该具体做什么? - Jonathan Gray