Capturing the document.write function

I'm working on a Chrome extension that requires me to intercept the document.write function (Note: I'm using a content script). I am using the method here: http://sitr.us/2012/09/04/monkey-patching-document-write.html But it doesn't work 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" warning inside my hook is never called when I call document.write on the webpage. What am I doing wrong?

+3


source to share


1 answer


Your extension runs in its own sandbox and does not have access to the JavaScript environment of the web page . Overwriting document.write

your extension does not affect the function of document.write

the web page itself.

Here's a quote from the docs :

However, content scripts have some limitations. They can not:

  • Use chrome. * API (excluding chrome.extension parts)
  • Use variables or functions defined by their extension pages
  • Use variables or functions defined by web pages or other content scripts



To change the function of document.write

a webpage, you have to insert your script into the DOM .

+4


source







All Articles