2

我希望使用 Greasemonkey 按顺序加载网页列表。

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

该脚本应按如下方式工作:打开站点 1,等待 5 秒,打开站点 2,等待 5 秒,等等。

我不知道如何使脚本按顺序打开站点,也许将实际 URL 与列表进行比较,然后继续下一个(?)。

4

2 回答 2

4

对于 Chrome,这种方法也适用于 Greasemonkey。

像这样将您的站点放在一个数组中,但您还必须设置您的@include@exclude@match指令以在适当的站点上触发。

把它们放在一起,这是一个完整的脚本

// ==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];
}
于 2012-09-04T10:09:54.277 回答
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)
于 2012-09-04T09:44:03.187 回答