1

For work, I spend a lot of time on domain-specific forums. Throughout the day, I check those forums to see if there are any questions and answers relevant to what I am working on. However, the particular site I am using does not automatically update, and so I have to refresh it. This isn't that big of a deal, but I decided I would save the following JavaScript script as a bookmark:

setInterval(function() { document.location.reload(true); }, 1000);

Essentially, I wanted to force the page to reload every second. However, when it reloads it seems it causes my script to stop running, and so the only effect of my script is a single reload. It's no better than just refreshing the site by hand.

I suppose the more general question is "is there any way to keep a script running even when a user travels to a new site?" I'm guessing not, as then sites could track you as you browsed the rest of the internet. Still, it seems that there might be levels of script execution; perhaps executing a script from a bookmark is different than a <script> tag on a webpage; if I were a browser designer I would have taken that into account and treated is as "browser code" running behind the scenes independant of the currently loaded page. Is that the way browsers work, or does it see all scripts as just running on the currently loaded page?

Thanks!

2 Answers2

1

"is there any way to keep a script running even when a user travels to a new site? I'm guessing not."

Yep. Well, you have to think in terms of the Client-Server infrastructure. Anytime the browser reloads, it has to send a new request to the server which technically would kill all running JS processes within the current DOM. Which is why just using setInterval by itself would not work either because the process is killed and never carry over the new reload.

Solution: You have to use User Scripts

In order to make this work, you would have to put your script within the context of the browser alone, not the Client-Server architecture. That way, since the context of execution is the Browser and not the DOM, a page refresh does not kill the JS process. The process is only killed when you close the Browser.

If you are using Chrome, check out the Chrome extension Tampermonkey I believe that is the best way to manage user scripts in the browser. Add your script by creating a New Script, save, then under the script settings, add your targeted site under the user matches

enter image description here

Limitation: This solution would work only for yourself (who can install extensions or add user script on your own browser) and not for other users of the website because we are outside of the Client-Server context.

maevadevs
  • 221
  • 2
  • 6
  • Are User Scripts essentially just extra code the browser executes as if it was its own? That's what it sounds like, and that's pretty cool. – Arik Rundquist Jun 29 '18 at 19:55
  • If that's the case, would I be correct in assuming that the process for adding a user script would vary by browser (if that browser even lets you add user scripts)? It's less a question of language capability and more a question of how the browser runs? – Arik Rundquist Jun 29 '18 at 19:59
0

Please follow these steps:

1. Install Tampermonkey as a Chrome extension
2. On Tampermonkey add a user script (+) and paste the follwing code
3. 

// ==UserScript==
// @name         Reload Page
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Reload given web page at a given pace (10 minutes in this case)
// @author       Your name or nick ;)
// @match        http://address_of_web_page_to_reload
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var minutes = 10;
    setTimeout (function(){ location.reload(); }, minutes*60*1000);
})();
  1. Save the script and open a new web page with the desired address
  2. Check that the page refreshes itself every 10 minutes!
  3. Have fun! pib
pibolito
  • 11
  • 1
  • 2