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

8

1

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?

Neil Spencer

Posted 2012-10-25T18:00:59.170

Reputation: 83

http://ww.bbc.co.uk doesn't exist? – Laoujin – 2012-10-25T22:33:22.020

Sorry - that was just a typo – Neil Spencer – 2012-10-27T18:36:05.330

Answers

8

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);

Brock Adams

Posted 2012-10-25T18:00:59.170

Reputation: 2 000

1Brilliant - many thanks for this. The script is now indeed only operating on the page I want rather than all pages. – Neil Spencer – 2012-10-27T18:35:21.790

2

I'm not sure how to do this in Javascript, but Firefox has an addon called ReloadEvery. Install it, restart FF and then right click on the page and choose ReloadEvery and a time.

David

Posted 2012-10-25T18:00:59.170

Reputation: 6 593

1Yes - but then every time I open Firefox and go to the website I need to activate ReloadEvery. What I would like is for the reload to be automatically activated just by going to the page. The javascript above does this but also reloads all other tabs that are open. – Neil Spencer – 2012-10-25T20:44:43.630