Search code examples
javascriptgreasemonkeytampermonkey

Grease/tamper monkey script to look for specific HTML in page and alert


I would like a grease/tamper monkey script, that when I visit a page, it looks for the following HTML on the page, and if it is present alert.

<p>
  <script 
    type='text/javascript' 
    src='https://site_im_visiting.com/?a(6 hex characters)=(numbers)'>
  </script>
</p>

Additionally, I would like to look inside an array (of about 4k sites), to see if the site is in the array.


Solution

  • Figured it out on my own, but instead of looking for the HTML, looked for the URL of the src of the script block

    var scripts = document.getElementsByTagName("script")
    var expression = /.*\/\?a[a-f0-9]{6}\=\d+?/gi;
    const domains =["something.com","google.com"]
    for (var i = 0; i < scripts.length; ++i) {
        var regex = new RegExp(expression)
        var t = scripts[i].src
        if (t.match(regex)) {        
            const url = new URL(t)
            if(domains.includes(url.hostname)){
                console.log(url.hostname)
            }else{
                alert(url.hostname)
            }
        }
    }