I use the following code in ViolentMonkey (or GreaseKit or TamperMonkey) to refresh a page every 30 seconds:
setTimeout(function(){ location.reload(); }, 30*1000);
This has been working fine for many years. But now, I want my code to NOT refresh the page if the following phrase is present: Important shizzle
(The reason I don't want it to refresh under this condition is because then I will no longer be able to see what was written.)
I know almost no Javascript. I've watched tutorials on YouTube, to try and learn the basics. I often google small questions and find answers on Stackoverflow (thanks all) - but I'm still very slow
Important shizzle
- if it exists then end the script.setTimeout(function(){ location.reload(); }, 30*1000);
Alas I cannot find an elegant Javascript command to abruptly end the script.
if( !document.body.textContent.includes("Important shizzle")) location.reload();
The problem is that the above doesn't do it every 30 seconds, it just does it once
You can read the .innerText
property of the body, then use String#includes
to see if your phrase is present.
If it is present you can return
out of the function to end the script.
Something like this:
const timeout = setTimeout(function () {
if (document.body.innerText.includes('Important shizzle')) return;
location.reload();
}, 30 * 1000);