Make Greasemonkey script do a keypress - ctrl + f5

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,143
Reaction score
10,489
So I'm trying to create a little Gresemonkey script. I got this and it works for refreshing a webpage every 15 seconds:

Code:
setTimeout(function(){ location.reload(); }, 15*1000);

The problem is, I need the type of refresh to be either a "ctrl+f5" or a "ctrl+shift+r" keypress.

Can anyone tell me how I can make that happen?
 
Ok, I figured it out. Here's my script:

Code:
// ==UserScript==
// @name        reload
// @namespace   123
// @include     https://www.somesite.com*
// @version     1
// @grant       none
// ==/UserScript==

setTimeout(function(){ location.reload(true); }, 10*1000);

That does the reload I want.

But now I need something else...

How can I make this work in this way:

Execute refresh as immediately as soon as "somesite.com" loads, then don't refresh again for 60 seconds?

I need this refresh to happen as soon as "somesite.com" loads, and then I need it to not happen again for at least 60 seconds in order to allow for some other actions to take place.
 
So I found something here - https://stackoverflow.com/questions/4190442/run-a-greasemonkey-script-only-once-per-page-load

And made this:

Code:
// ==UserScript==
// @name        reload
// @namespace   123
// @include     https://www.somesite.com*
// @version     1
// @grant       none
// ==/UserScript==

window.addEventListener ("load", LocalMain, false);

function LocalMain () {
setTimeout(function(){ location.reload(true); }, 4*1000);
}

But it refreshes every 4 seconds. I just want it to refresh one time and then stop, not to keep refreshing every 4 seconds over and over again.

Any help?
 
You need to use setInterval instead of setTimeout.

Moronic answer...

Basically you're reloading the page, and every time that happens, the script runs again, and you're stuck in a loop, so you need to google persistent values in greasemonkey, looking that up you get GM.setValue and GM.getValue. Here's the script :

Code:
// ==UserScript==
// @name        reload
// @namespace   123
// @include     https://www.reddit.com*
// @version     1
// @grant    GM.getValue
// @grant    GM.setValue
// ==/UserScript==

GM.getValue("once",false).then(myFunc);

function myFunc(once){
 if(!once)
    window.addEventListener ("load", LocalMain, false);
}

function LocalMain () {
    setTimeout(function(){ location.reload(true); }, 4*1000);
  window.removeEventListener('load', LocalMain);
  GM.setValue("once",true);
}
 
You need to use setInterval instead of setTimeout.

I googled these after you answered, but, it doesn't make sense. Those two appear to be pretty much the same thing.

Thanks anyway

Moronic answer...

mahaha I died xD

Basically you're reloading the page, and every time that happens, the script runs again, and you're stuck in a loop, so you need to google persistent values in greasemonkey, looking that up you get GM.setValue and GM.getValue. Here's the script :

Code:
// ==UserScript==
// @name        reload
// @namespace   123
// @include     https://www.reddit.com*
// @version     1
// @grant    GM.getValue
// @grant    GM.setValue
// ==/UserScript==

GM.getValue("once",false).then(myFunc);

function myFunc(once){
 if(!once)
    window.addEventListener ("load", LocalMain, false);
}

function LocalMain () {
    setTimeout(function(){ location.reload(true); }, 4*1000);
  window.removeEventListener('load', LocalMain);
  GM.setValue("once",true);
}

Did it work? Do you still have a problem?

Man thank you very much for taking the time. I actually figured out a better way to solve my problem. It seems to be working fine for now, but if it stops working I'll keep your answer here as plan B, and use it if needed.

Thanks again :)
 
Back
Top