Existing code
I use the following code in ViolentMonkey (or GreaseKit or TamperMonkey) to refresh a page every 30 seconds:
setTimeout(function(){ location.reload(); }, 30*1000);
Can I make it stop?
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 am unenlightened
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
Strategy thoughts
- Search for the phrase
Important shizzle
- if it exists then end the script. - Then I'd just have my existing code:
setTimeout(function(){ location.reload(); }, 30*1000);
Alas I cannot find an elegant Javascript mand to abruptly end the script.
Would this work?
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
Existing code
I use the following code in ViolentMonkey (or GreaseKit or TamperMonkey) to refresh a page every 30 seconds:
setTimeout(function(){ location.reload(); }, 30*1000);
Can I make it stop?
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 am unenlightened
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
Strategy thoughts
- Search for the phrase
Important shizzle
- if it exists then end the script. - Then I'd just have my existing code:
setTimeout(function(){ location.reload(); }, 30*1000);
Alas I cannot find an elegant Javascript mand to abruptly end the script.
Would this work?
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
Share Improve this question edited Aug 28, 2021 at 20:26 Nicholas Kunze asked Aug 28, 2021 at 19:27 Nicholas KunzeNicholas Kunze 537 bronze badges 7-
3
if( !document.body.textContent.includes("Important shizzle")) location.reload();
– Niet the Dark Absol Commented Aug 28, 2021 at 19:29 - How exactly is the tag fluid-mac-app-engine related to your post? – esqew Commented Aug 28, 2021 at 19:29
- It's because Fluid has built in "greasekit" functionality (same thing as greasemonkey) – Nicholas Kunze Commented Aug 28, 2021 at 19:33
-
It's easy, but the true question to answer is HOW do you know when
Important shizzle
is on the page, with your code? The RELOAD script must run after the CHECK script that reads the emplacement of the textImportant shizzle
. – KeitelDOG Commented Aug 28, 2021 at 19:35 -
@KeitelDOG can I use
if( !document.body.textContent.includes("Important shizzle")
? – Nicholas Kunze Commented Aug 28, 2021 at 19:43
3 Answers
Reset to default 3You 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);
You can do it this way :
setInterval(reload, 30*1000);
function reload() {
if ( isReloadOK() ) location.reload();
}
function isReloadOK(){
if (document.body.textContent.includes("Important shizzle")) return false;
return true;
}
You can have the timeout and you can ADD an interval and I will use the example from what you have shown already.. the most important part is clearTimeout
var timeout=setTimeout(function(){ location.reload(); }, 30*1000);
var interval=setInterval(()=>{
let condition = document.body.textContent.includes("Important shizzle");
if(condition){clearTimeout(timeout); clearInterval(interval)}
},0);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745560488a4633078.html
评论列表(0条)