I’m working on a Chrome extension that requires me to intercept the document.write function (Note: I am using content script). I’m using the method here: http://sitr.us/2012/09/04/monkey-patching-document-write.html
But it’s not working correctly. This is what I have right now:
(function() {
var originalWrite = document.write;
alert("checkpoint 1");
document.write = function() {
alert("checkpoint 2");
//secret stuff here
return Function.prototype.apply.call(
originalWrite, document, arguments);
}
})();
However, the “checkpoint 2” alert inside my hook never gets called when I call document.write on a web page. What am I doing wrong?
Your extension runs in its own sandbox, and has no access to the web page’s JavaScript environment. Overwriting
document.writein your extension does not affect thedocument.writefunction of the web page itself.Here’s a quote from the docs:
To alter the
document.writefunction of the webpage, you’ll have to insert your script into the DOM.