As @MrGlass said, Chrome extensions currently work in a separate environment, restricting access to the actual window object and providing a duplicate that is valid only for the extension.
To solve this problem, we can inject the script element directly into the document. Thus, you get access to the document environment and the real window object.
First, let's create a function (I also added โconfirmโ because some confirmations annoyed me):
var disablerFunction = function () { window.alert = function alert(msg) { console.log('Hidden Alert ' + msg); }; window.confirm = function confirm(msg) { console.log("Hidden Confirm " + msg); return true; }; };
Now, what we're going to do is convert this function to script text and enclose it in parentheses (to avoid possible conflicts with actual vars in the page environment):
var disablerCode = "(" + disablerFunction.toString() + ")();";
And finally, we introduce the script element and immediately delete it:
var disablerScriptElement = document.createElement('script'); disablerScriptElement.textContent = disablerCode; document.documentElement.appendChild(disablerScriptElement); disablerScriptElement.parentNode.removeChild(disablerScriptElement);
Daniel Mรถller
source share