2013-03-26 47 views
0

所以我有这样的脚本:脚本在Firefox暂存器而不是在Greasemonkey的

links = document.getElementsByClassName('thumb'); 
for (var i=links.length-1; i>=0; i--) 
{ 
links[i].href=links[i].href+'/popout'; 
} 

该页面后暂存器的作品,我想修改,被加载。

但是,当我把它放在greasemonkey中,它在页面完全加载之前运行,并且我需要mod的元素不存在,它不起作用。

这里是GS脚本:

// ==UserScript== 
// @name  popout 
// @namespace PylonPants 
// @include  http://*.twitch.tv/directory/* 
// @version  1 

// ==/UserScript== 

//if ('loading' == document.readyState) { 
// alert("This script is running at document-start time."); 
//} else { 
// alert("This script is running with document.readyState: " + document.readyState); 
//} 
// script runs at readyState = interactive and that brakes it 
// do not know how to make it wait till complete 

links = document.getElementsByClassName('thumb'); 
alert(links[0]); // i get "undefined" 
for (var i=links.length-1; i>=0; i--) 
{ 
alert('the script works'); //i never reach here 
alert(links[i].href); // nor here 
links[i].href=links[i].href+'/popout'; 
} 
alert('crazy'); // i get this then the page loads fully. 

回答

1

脚本必须等待页面的JavaScript加载这些缩略图。最简单,最可靠的方法就是使用the waitForKeyElements() utility

这里的一个完整的脚本使用jQuerywaitForKeyElements改变那些href S:

// ==UserScript== 
// @name  popout 
// @namespace PylonPants 
// @include  http://*.twitch.tv/directory/* 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js 
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js 
// @grant  GM_addStyle 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
    introduced in GM 1.0. It restores the sandbox. 
*/ 

waitForKeyElements ("a.thumb", adjustLinkHrefs); 

function adjustLinkHrefs (jNode) { 
    var newAddr = jNode.attr ("href") + '/popout'; 
    jNode.attr ("href", newAddr); 
} 
+0

谢谢回答 – user2209850 2013-03-26 07:33:04