views:

68

answers:

2

It's there a way to reload a script from greasemonkey ?

For example: When I enter on some specific website, the script from the greasemonkey works correctly, but when I change the page (asp in the website I guess), the script doesn't reload to take effect...

How can I solve it?

A: 

Greasemonkey should work on every page load, so I think that the problem you are experiencing is due to the @include/@exclude rules used for the userscript in question. Can you please point us to the source of this user script? and the two page urls that you refer to in your question please?

Erik Vold
+1  A: 

Wrap your Greasemonkey code in a function and then set a document change event-handler to call it. Like so...

/*--- 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
}
Brock Adams
worked, thanks =)
Shady
could this script only be executed X time after a mouse action? (mouse click)
Shady
Yes. You'd remove the `DOMSubtreeModified` listener and add something like: `YourNode.addEventListener ("click", HandleDOM_ChangeWithDelay, false);` . It's not too hard to do. Post another question, if needed.
Brock Adams