Skip to content

Instantly share code, notes, and snippets.

@jnm2
Forked from BrockA/waitForKeyElements.js
Created November 15, 2016 04:10

Revisions

  1. Brock revised this gist Nov 16, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion waitForKeyElements.js
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,7 @@ function waitForKeyElements (
    iframeSelector
    );
    },
    500
    300
    );
    controlObj [controlKey] = timeControl;
    }
  2. Brock revised this gist May 18, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions waitForKeyElements.js
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,7 @@ function waitForKeyElements (
    .find (selectorTxt);

    if (targetNodes && targetNodes.length > 0) {
    btargetsFound = true;
    /*--- Found target node(s). Go through each and act if they
    are new.
    */
    @@ -49,11 +50,13 @@ function waitForKeyElements (

    if (!alreadyFound) {
    //--- Call the payload function.
    jThis.data ('alreadyFound', true);
    actionFunction (jThis);
    var cancelFound = actionFunction (jThis);
    if (cancelFound)
    btargetsFound = false;
    else
    jThis.data ('alreadyFound', true);
    }
    } );
    btargetsFound = true;
    }
    else {
    btargetsFound = false;
  3. Brock revised this gist May 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion waitForKeyElements.js
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,8 @@ function waitForKeyElements (

    if (!alreadyFound) {
    //--- Call the payload function.
    actionFunction (jThis);
    jThis.data ('alreadyFound', true);
    actionFunction (jThis);
    }
    } );
    btargetsFound = true;
  4. Brock created this gist May 7, 2012.
    89 changes: 89 additions & 0 deletions waitForKeyElements.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
    that detects and handles AJAXed content.
    Usage example:
    waitForKeyElements (
    "div.comments"
    , commentCallbackFunction
    );
    //--- Page-specific function to do what we want when the node is found.
    function commentCallbackFunction (jNode) {
    jNode.text ("This comment changed by waitForKeyElements().");
    }
    IMPORTANT: This function requires your script to have loaded jQuery.
    */
    function waitForKeyElements (
    selectorTxt, /* Required: The jQuery selector string that
    specifies the desired element(s).
    */
    actionFunction, /* Required: The code to run when elements are
    found. It is passed a jNode to the matched
    element.
    */
    bWaitOnce, /* Optional: If false, will continue to scan for
    new elements even after the first match is
    found.
    */
    iframeSelector /* Optional: If set, identifies the iframe to
    search.
    */
    ) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined")
    targetNodes = $(selectorTxt);
    else
    targetNodes = $(iframeSelector).contents ()
    .find (selectorTxt);

    if (targetNodes && targetNodes.length > 0) {
    /*--- Found target node(s). Go through each and act if they
    are new.
    */
    targetNodes.each ( function () {
    var jThis = $(this);
    var alreadyFound = jThis.data ('alreadyFound') || false;

    if (!alreadyFound) {
    //--- Call the payload function.
    actionFunction (jThis);
    jThis.data ('alreadyFound', true);
    }
    } );
    btargetsFound = true;
    }
    else {
    btargetsFound = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj = waitForKeyElements.controlObj || {};
    var controlKey = selectorTxt.replace (/[^\w]/g, "_");
    var timeControl = controlObj [controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound && bWaitOnce && timeControl) {
    //--- The only condition where we need to clear the timer.
    clearInterval (timeControl);
    delete controlObj [controlKey]
    }
    else {
    //--- Set a timer, if needed.
    if ( ! timeControl) {
    timeControl = setInterval ( function () {
    waitForKeyElements ( selectorTxt,
    actionFunction,
    bWaitOnce,
    iframeSelector
    );
    },
    500
    );
    controlObj [controlKey] = timeControl;
    }
    }
    waitForKeyElements.controlObj = controlObj;
    }