0

I am making a Chrome extension that runs a code when a web page's specific alert dialog is shown to user. (By alert dialog, I mean Javascript's 'window.alert()') I want to detect the alert dialog's text, and run a code. Is there any way for Chrome extension to detect alert dialog's text shown by the web page? Or any other way to detect alert dialog?

For example, a foreign website alerts a user by window.alert("Banana pie"). My extension will detect 'Banana' and run a code.

1 Answers1

0

I can't see your code but try to call a function instead of an alert. For example in part of code where you put alert('something') put a function that will make that alert and do whatever you want too. And you can also make a function that takes alerts text:

 function getAlert() {

  // get all scripts
  var elem = document.scripts;

  // loop and check
  for (var i = 0, len = elem.length; i < len; i++) {

    var txt = elem[i].textContent.match(/alert\(['"]([^'"]+)['"]\)/);
    if (txt) { return txt[1]; } // if matched, return the alert text and stop
  }
} 

Took this from How to get text from alert box? Hope this helps

Lazar
  • 1
  • 3
  • Ah I meant 'when' a website alerts a user, my code will run 'at that time'. Not just checking whether if the website has that alert. Also, I am dealing with a website made by others(not me), so I can't just change that website to call my function. – Alternative415 Aug 23 '22 at 10:46
  • Ooooh, well sorry, don't know then. – Lazar Aug 23 '22 at 11:28