views:

99

answers:

2

Can I create a Google chrome extension to prevent the page from doing an alert() ?

+1  A: 

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

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

Just remember to run that content script at document_start within the manifest via run_at manifest content script modifier.

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

Mohamed Mansour
A: 

Thank you. That helped. However, I realized I needed to do this to get it to work

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

if I wanted to remove alerts and confirms, I can do

location.href="javascript: window.alert = function(x) {console.log(x)}; window.confirm = function(){return true;};";
Drew LeSueur