如何自动和顺序打开页面列表?

时间:2012-09-04 09:26:00

标签: javascript web greasemonkey sequential

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

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

脚本的工作方式如下:打开网站1,等待5秒,打开网站2,等待5等等。

我不知道如何让脚本按顺序打开网站,或者将实际网址与列表进行比较并继续下一个?

2 个答案:

答案 0 :(得分:4)

This approach, for Chrome,也适用于Greasemonkey。

将您的网站放在一个数组中,但您还必须将@include, @exclude, and @match directives设置为在相应的网站上启动。

总而言之,这里有一个完整的脚本

// ==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];
}

答案 1 :(得分:1)

我能想到的两种方法是:

使用gm_getvaluegm_setvalue检索,将list中当前网站的索引存储到gm的持久内存中。

或使用类似的东西:

setTimeout(function(){
    window.location.href = (list.length > list.indexOf(window.location.href)) ? list[list.indexOf(window.location.href)+1] : list[0];
},5000)