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

相關問題