2

Is there some way for a Chrome extension to detect an alert box which shows up immediately after an external website's page-load? I understand that once an alert box shows up, JavaScript execution is suspended.

A certain website throws a specific alert and I need to be able to listen for that. Ideally, I want to extract the text within the alert.

AFisher
  • 85
  • 1
  • 8

1 Answers1

6

You can overwrite the native alert() function

var native_alert = alert;

alert = function(msg) {
    console.log('call to alert() - message: '+msg);
    native_alert(msg);    
}

alert('intercepted!');

DEMO: http://jsfiddle.net/jHTeK/2/

Pedro L.
  • 7,376
  • 3
  • 25
  • 27
  • This indeed works if you control the execution environment of the web page. However, the following accepted answer elaborates on your point in the context of the specific question I asked: http://stackoverflow.com/questions/12095924/is-it-possible-to-inject-a-javascript-code-that-overrides-the-one-existing-in-a Thanks for pointing me in the right direction. – AFisher Sep 01 '13 at 16:58
  • You only need to insert the script in the document. – Pedro L. Sep 01 '13 at 17:01