JavaScript uses a prototype inheritance system, instead of having classes, objects have prototypes. Prototypes are real objects that are used as a reference to other objects to inherit methods and attributes.
A better strategy would be to override the write method in the prototype "document" (which for an HTML document is HTMLDocument). This should effectively wrap the method for all instances of the “document” inside the pages loaded into the browser, since they all use the same prototype.
Instead
document.write = function() { ... }
try something like this:
HTMLDocument.prototype.write= function() { ... }
UPDATE: This doesn't seem as easy as I originally thought, it doesn't seem to work on the first try.
source share