2012-09-04 77 views
2

我想用Greasemonkey连续加载网页列表。如何自动连续打开页面列表?

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

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

我不知道如何使脚本露天场地的顺序,也许比较实际的URL和列表并移动到下一个?

回答

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]; 
} 
+0

感谢您的代码。它工作得很好。只是另一个小问题。有没有办法迫使Firefox从头开始加载一个网页,即使是在同一个域中?因为我注意到,如果我在同一网站的列表中插入2页,它将停止工作,因为它不会完全重新加载页面,并且GM脚本不再执行。 – KingBOB

+0

在发生这种情况时,指定两个或更多特定网址。他们是不同的只有锚或主题标签? –

+0

已更新脚本以处理** WELL BEHAVED ** AJAX加载的“新”页面。如果这不能为您解决问题,请打开一个新问题并提供问题页面的确切网址。 –

1

两种方式我能想到的这样做是:

使用gm_getvaluegm_setvalue检索,存储当前站点的索引中list通用汽车的永久存储器。

,或者使用类似:

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

谢谢!您的代码按预期工作。 ;) – KingBOB