Firefox – Using JavaScript and Greasemonkey to reload just one tab in Firefox

firefoxgreasemonkeyjavascript

I am new to Greasemonkey and javascript but have found the script below to reload a page every 5 minutes.

// ==UserScript==
// @name        Auto Reload Protopage
// @namespace   http://blog.monstuff.com/archives/cat_greasemonkey.html
// @description Reload pages every 5 minutes
// @include     http://ww.bbc.co.uk
    // @grant               none
// ==/UserScript==

// based on code by Julien Couvreur
// and included here with his gracious permission

var numMinutes = 5;
window.setTimeout("document.location.reload();", numMinutes*60*1000);

This works but it reloads all the open tabs every 5 minutes and not just the one specified in the @include statement.

Is there any way of doing this?

Best Answer

That code has a corrupt metadata block, spaces are critical for that block, and extra spaces at the beginning of a line can break it -- causing the script to fire for all pages (the default behavior).

Update: The appearance of a corrupt block may just be a display bug here at SuperUser. Will investigate in a bit.
Updatier: The corrupt block is real, the OP's code is indented by a mix of tabs and spaces, which fooled SU's raw-post editor, but not the final display.

Also, the @include directive is specifying a webpage that doesn't exist. ww., versus www.. That line should be:

// @include     http://www.bbc.co.uk/

Or possibly:

// @include     http://www.bbc.co.uk/*

if you want more than just the home page effected.

Putting it all together and using setTimeout in the recommended way (Avoid use of "auto eval()"):

// ==UserScript==
// @name        Auto Reload Protopage
// @namespace   http://blog.monstuff.com/archives/cat_greasemonkey.html
// @description Reload pages every 5 minutes
// @include     http://www.bbc.co.uk/
// @grant       none
// ==/UserScript==

// based on code by Julien Couvreur
// and included here with his gracious permission

var numMinutes = 5;
setTimeout (location.reload, numMinutes*60*1000);