3

I wish to load a list of webpages sequentially, with Greasemonkey.

var list = array ('http://www.google.com', 'site2', 'site3', 'site4');
window.location.href = list[0];

The script should work as follows: open site 1, wait 5 seconds, open site 2, wait 5 seconds, etc.

I don't know how to make the script open sites in sequence, maybe compare the actual URL to the list and move on the next one(?).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Giorgio
  • 1,603
  • 5
  • 29
  • 52

2 Answers2

4

This approach, for Chrome, will also work in Greasemonkey.

Put your sites in an array, like that, but you must also set your @include, @exclude, and @match directives to fire on the appropriate sites.

Putting it all together, here's a complete script:

// ==UserScript==
// @name        Multipage, MultiSite slideshow of sorts
// @include     http://google.com/*
// @include     http://site2/*
// @include     http://site3/*
// @include     http://site4/*
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design
    change introduced in GM 1.0.
    It restores the sandbox.
*/

var urlsToLoad  = [
    'http://google.com/'
    , 'http://site2/somepage/'
    , 'http://site3/somepage/'
    , 'http://site4/somepage/'
];

/*--- Since many of these sites load large pictures, Chrome's and 
    Firefox's injection may fire a good deal before the image(s) 
    finish loading.
    So, insure script fires after load:
*/
window.addEventListener ("load", FireTimer, false);
if (document.readyState == "complete") {
    FireTimer ();
}
//--- Catch new pages loaded by WELL BEHAVED ajax.
window.addEventListener ("hashchange", FireTimer,  false);

function FireTimer () {
    setTimeout (GotoNextURL, 5000); // 5000 == 5 seconds
}

function GotoNextURL () {
    var numUrls     = urlsToLoad.length;
    var urlIdx      = urlsToLoad.indexOf (location.href);
    urlIdx++;
    if (urlIdx >= numUrls)
        urlIdx = 0;

    location.href   = urlsToLoad[urlIdx];
}
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks for your code. It works brillantly. Just another little question. Is there a way to force firefox to load from scratch a webpage even if is in the same domain? Because I've noticed that if I insert in the list 2 pages of the same website it stops working because it doens't fully reload the page and GM script is not executed anymore. – Giorgio Sep 04 '12 at 10:34
  • Give two, or more, specific URLs where this happens. Are they different only by anchors or hashtags? – Brock Adams Sep 04 '12 at 10:56
  • Updated the script to handle "new" pages loaded by **WELL BEHAVED** AJAX. If that doesn't fix it for you, please open a new question and provide exact URL's of the problem pages. – Brock Adams Sep 04 '12 at 11:19
1

Two ways I can think of for doing this are:

Using gm_getvalue, gm_setvalue to retrieve, store the index of current site in list to Greasemonkey's persistent memory.

Or, using something like:

setTimeout(function(){
    window.location.href = (list.length > list.indexOf(window.location.href)) ? list[list.indexOf(window.location.href)+1] : list[0];
},5000)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prasanth
  • 5,230
  • 2
  • 29
  • 61