Search code examples
javascriptgoogle-chromegreasemonkey

How to get text from alert box?


I need to get text from alert box.

<script type="text/javascript">
   alert('Some text')
</script>

I don't have enough reputation to upload images..so i upload code instead of image :)

Is there any way to get text "from popup" on Chrome using Greasemonkey?


Solution

  • Query is not clear ..but if I understand it correctly ..there is a JavaScript on a page that results in an alert()

    You can get the text of that alert from the JavaScript on the page.

    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
      }
    } 
    

    In your case, the above function will return Some text.