Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何自动按顺序打开页面列表?_Javascript_Web_Greasemonkey_Sequential - Fatal编程技术网

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

Javascript 如何自动按顺序打开页面列表?,javascript,web,greasemonkey,sequential,Javascript,Web,Greasemonkey,Sequential,我想用Greasemonkey按顺序加载网页列表 var list = array ('http://www.google.com', 'site2', 'site3', 'site4'); window.location.href = list[0]; 脚本的工作方式如下:打开站点1,等待5秒,打开站点2,等待5秒,等等 我不知道如何使脚本按顺序打开站点,可能会将实际URL与列表进行比较,然后移动到下一个(?)。我可以想到两种方法: 使用,gm_setvalue进行检索,将当前站点的索引存储

我想用Greasemonkey按顺序加载网页列表

var list = array ('http://www.google.com', 'site2', 'site3', 'site4');
window.location.href = list[0];
脚本的工作方式如下:打开站点1,等待5秒,打开站点2,等待5秒,等等


我不知道如何使脚本按顺序打开站点,可能会将实际URL与列表进行比较,然后移动到下一个(?)。

我可以想到两种方法:

使用,
gm_setvalue
进行检索,将当前站点的索引存储在
列表中
到Greasemonkey的持久内存中

或者,使用类似于:

setTimeout(function(){
    window.location.href = (list.length > list.indexOf(window.location.href)) ? list[list.indexOf(window.location.href)+1] : list[0];
},5000)
,也将在Greasemonkey中工作

var list = array ('http://www.google.com', 'site2', 'site3', 'site4');
window.location.href = list[0];
将您的站点放在一个数组中,就像这样,但是您还必须将您的站点设置为在适当的站点上启动

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

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

谢谢你的代码。它工作得很出色。再问一个小问题。有没有办法强迫firefox从头开始加载网页,即使它位于同一个域中?因为我注意到,如果我在同一网站的列表中插入两个页面,它将停止工作,因为它没有完全重新加载页面,并且GM脚本不再执行。请给出两个或更多发生这种情况的特定URL。它们是否仅在锚或hashtags方面有所不同?更新了脚本以处理行为良好的AJAX加载的“新”页面。如果这还不能解决您的问题,请打开一个新问题,并提供问题页面的确切URL。