2010-06-15 45 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

相关问题