0

I am trying to write a tampermoney script that would close a tab when found a certain word or phrase on a webpage. But when I use document.body.textContent, the words that are not visible on the page (i.e. cannot find using Ctrl + F) would also be detected by the script. Is there a way that would only look for the visible content?

var blacklist = ["Apple"],
    re = new RegExp(blacklist.join('|'), "i");
if (re.test(document.body.textContent)) {
  var win = window.open("","_self");
  win.close();
Geoffrey
  • 3
  • 1
  • I believe that is what `innerText` does? – SuperStormer Jul 29 '22 at 19:23
  • @SuperStormer you mean replacing `document.body.textContent` by `document.body.innerText` ? I tried, but it would neither detect the visible word nor the non-visible word – Geoffrey Jul 29 '22 at 19:36
  • Check this Q/A [How do I get just the visible text with jQuery (or Javascript)?](https://stackoverflow.com/questions/1846177/how-do-i-get-just-the-visible-text-with-jquery-or-javascript) – Baro Jul 29 '22 at 20:48
  • @SuperStormer The `innerText` works after I added the `window.onload` function. However, this would take a few seconds before the function could be trigged as the loading of the webpage takes time. Is it possible to trigger the function immediately once the word "Apple" is visible instead of having to wait for the whole page to be loaded? – Geoffrey Jul 31 '22 at 09:34

0 Answers0