0

I have this script which closes a tab if it finds a certain element on the page:

// ==UserScript==
// @name        Name
// @namespace   bbb
// @match       https://example.com
// @include     about:config
// @version     1
// @grant       none
// ==/UserScript==



var intv = setInterval(function() {
 
if (/(container)|(mediumheight)/i.test (document.body.innerHTML) )

{
    window.close()
}
 
    }, 1000);

How can I make it close the page if it finds this:

<span id="some">0</span>

But it has to check for both the id (some) and for the number (0). Both must be on the page for the script to fire.

If it was just the id (some) I could do it, but I need both to be true for the script to fire.

Can anyone help? Thanks

Scorpys
  • 3
  • 4

1 Answers1

0
<span id="some">0</span> 

But it has to check for both the id (some) and for the number (0). Both must be on the page for the script to fire.

Here is an example of how you get check it:

const sp = document.querySelector('#some'); // find id (some)
if (sp && sp.textContent === '0') {
 // run the function
}
erosman
  • 7,094
  • 7
  • 27
  • 46
  • Thanks for the reply Erosman, I appreciate it :) – Scorpys Aug 06 '21 at 22:28
  • Just to let you know that I just ended up trying this out and putting it to use, and it works like a charm. Thanks again :) – Scorpys Aug 19 '21 at 02:31
  • Hey Erosman. So it seems this doesn't work if the ID and Text are in an iFrame. How to make it work on an iframe? – Scorpys Oct 02 '21 at 21:10
  • @Scorpys Check out [QuerySelector for Web Elements Inside iframe](https://stackoverflow.com/questions/26630519/queryselector-for-web-elements-inside-iframe) – erosman Oct 03 '21 at 04:53
  • I looked through it, but it doesn't really help me. Thanks anyway :) – Scorpys Oct 03 '21 at 05:03
  • @Scorpys First you need to get the `iframe` and then `contentWindow.document.body.querySelector('#some')`. There is not enough details for a proper answer. – erosman Oct 03 '21 at 05:12
  • Okay, but that would still have me not knowing how to get the content of the #some. I asked a detailed question here - https://stackoverflow.com/questions/69422111/how-to-run-script-only-if-divcontains-in-an-iframe-greasemonkey I used the code you gave me in this thread to show what I tried. – Scorpys Oct 03 '21 at 05:14