Can I prevent warning () with Google Chrome extension

Can I create a Google Chrome extension so that the page does not execute alert() ?

+9
javascript google-chrome google-chrome-extension alert
source share
3 answers

Thanks. That helped. However, I realized that I needed to do this in order to make it work.

 location.href="javascript: window.alert = function(x) {console.log(x)};" 

If I wanted to remove warnings and confirmations, I can do

 location.href="javascript: window.alert = function(x) {console.log(x)}; window.confirm = function(){return true;};"; 
-5
source share

Yes you can, alert () is just a JavaScript method, you can override its functionality.

 window.alert = function alert(msg) { console.log('Hidden Alert ' + msg); }; 

Remember to run this script content in document_start in the manifest using the run_at script manifest modifier.

I believe there is an extension that just does this. The developer calls it Nice Alert. https://chrome.google.com/extensions/detail/ehnbelnegmgdnjaghgomaakjcmpcakhk

+14
source share

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; /*simulates user clicking yes*/ }; }; 

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); 
+10
source share

All Articles