2010-06-15 42 views
2

它有一種方法來重新加載一個腳本從greasemonkey?它有一種方法來重新加載一個greasemonkey腳本?

例如: 當我在某個特定網站上輸入時,來自greasemonkey的腳本正常工作,但是當我更改頁面時(我猜想網站中的asp),腳本不會重新加載才能生效。 。

我該如何解決?

+0

我的腳本似乎在頁面加載中運行。可能您正在觀察的行爲是當頁面使用AJAX並且頁面的一部分通過Javascript重新加載時。 – MZB 2010-06-15 03:15:41

回答

2

將Greasemonkey代碼包裝在一個函數中,然後設置文檔更改事件處理程序來調用它。像這樣...

/*--- To "refire" our Greasemonkey code on AJAX changes, we wrap it in 
    a function and call it on a DOM change event. 
*/ 

var zGbl_DOM_ChangeTimer    = ''; 
var bGbl_ChangeEventListenerInstalled = false; 


/*--- Run everything after the document has loaded. Avoids race- 
     conditions and excessive "churn". 
*/ 
window.addEventListener ("load", MainAction, false); 


function MainAction() 
{ 
    if (!bGbl_ChangeEventListenerInstalled) 
    { 
     bGbl_ChangeEventListenerInstalled = true; 

     /*--- Notes: 
       (1) If the ajax loads to a specific node, add this 
        listener to that, instead of the whole body. 
       (2) iFrames may require different handling. 
     */ 
     document.addEventListener ("DOMSubtreeModified", HandleDOM_ChangeWithDelay, false); 
    } 

    //--- **************************** 
    //--- *** YOUR CODE GOES HERE. *** 
    //--- **************************** 
} 


function HandleDOM_ChangeWithDelay (zEvent) 
{ 
    /*--- DOM changes will come hundreds at a time, we wait a fraction 
      of a second after the LAST change in a batch. 
    */ 
    if (typeof zGbl_DOM_ChangeTimer == "number") 
    { 
     clearTimeout (zGbl_DOM_ChangeTimer); 
     zGbl_DOM_ChangeTimer = ''; 
    } 
    zGbl_DOM_ChangeTimer  = setTimeout (function() { MainAction(); }, 222); //-- 222 milliseconds 
} 
+0

工作,謝謝=) – Shady 2010-06-17 09:15:42

+0

該腳本只能在鼠標動作後X時間執行? (鼠標點擊) – Shady 2010-06-18 01:13:53

+0

是的。您將移除DOMSubtreeModified偵聽器並添加如下內容:'YourNode.addEventListener(「click」,HandleDOM_ChangeWithDelay,false);'。這並不難。如果需要,請發佈另一個問題。 – 2010-06-18 01:30:52

0

Greasemonkey應該適用於每一頁加載,所以我認爲你遇到的問題是由於用於有問題的userscript的@ include/@排除規則。你能指點我們這個用戶腳本的來源嗎?以及您在問題中提到的兩個頁面網址?

+0

即使在AJAX頁面加載? – jibiel 2012-09-28 05:54:34

相關問題