I've developed a Chrome extension that records user behavior while browsing web pages by adding event listeners to customers' web pages using a Chrome content script
.
The code in the content script
looks something like this:
var recordingEvents = ['click', 'input', 'change'];
recordingEvents.forEach(function (e) {
window.addEventListener(e, handler, true);
});
For example, here's a snippet from a custom page:
<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>
This snippet uses document.open
and document.write
to update the content of an iframe.
My question is this: The event listeners are attached to the window
object, but when document.open
is used, it removes all event listeners as shown below.
https://i.sstatic.net/DQ0fh.png
Is there a way to prevent document.open
from removing event listeners? Or is there a way to monitor document.open
so I can manually reattach listeners afterwards?