Skip to content

Instantly share code, notes, and snippets.

@cargix1
Forked from buu700/jquery.waituntilexists.js
Last active October 3, 2017 13:36

Revisions

  1. cargix1 revised this gist Oct 3, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    var jQuery = require('jquery');

    (function ($) {

    /**
  2. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    (function ($) {

    /**
    * @function
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    @@ -23,4 +25,6 @@ $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    }

    return $this;
    }
    }

    }(jQuery));
  3. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * @function
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    * @param {function} handler
    * @param {function} handler A function to execute at the time when the element is inserted
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $(selector).waitUntilExists(function);
    */
    @@ -23,4 +23,4 @@ $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    }

    return $this;
    }
    }
  4. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    * @param {function} handler
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $('div.comments').waitUntilExists(commentCallbackFunction || function () { alert('function does not exist.'); })
    * @example $(selector).waitUntilExists(function);
    */

    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
  5. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@
    */

    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    var found = 'found';
    var $this = $(this.selector);
    var found = 'found';
    var $this = $(this.selector);
    var $elements = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);

    if (!isChild)
  6. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@
    * @example $('div.comments').waitUntilExists(commentCallbackFunction || function () { alert('function does not exist.'); })
    */

    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    var found = 'found';
    var $this = $(this.selector);
    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    var found = 'found';
    var $this = $(this.selector);
    var $elements = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);

    if (!isChild)
  7. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    * @function
    * @property {object} jQuery plugin which runs handler function when specified element is inserted into the DOM
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    * @param {function} handler
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $('div.comments').waitUntilExists(commentCallbackFunction || function () { alert('function does not exist.'); })
    @@ -9,14 +9,13 @@
    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    var found = 'found';
    var $this = $(this.selector);
    var $elements = $this.not(function () { return $(this).data(found); });

    $elements.each(function () { handler(this); $(this).data(found, true); });
    var $elements = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);

    if (!isChild)
    {
    window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {};
    window.waitUntilExists_Intervals[this.selector] = window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500);
    (window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
    window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
    ;
    }
    else if (shouldRunHandlerOnce && $elements.length)
    {
  8. @buu700 buu700 revised this gist Dec 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    * @function
    * @property {string} jQuery plugin which runs handler function when specified element is inserted into the DOM
    * @property {object} jQuery plugin which runs handler function when specified element is inserted into the DOM
    * @param {function} handler
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $('div.comments').waitUntilExists(commentCallbackFunction || function () { alert('function does not exist.'); })
  9. @buu700 buu700 revised this gist Dec 4, 2012. 2 changed files with 27 additions and 92 deletions.
    27 changes: 27 additions & 0 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * @function
    * @property {string} jQuery plugin which runs handler function when specified element is inserted into the DOM
    * @param {function} handler
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $('div.comments').waitUntilExists(commentCallbackFunction || function () { alert('function does not exist.'); })
    */

    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    var found = 'found';
    var $this = $(this.selector);
    var $elements = $this.not(function () { return $(this).data(found); });

    $elements.each(function () { handler(this); $(this).data(found, true); });

    if (!isChild)
    {
    window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {};
    window.waitUntilExists_Intervals[this.selector] = window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500);
    }
    else if (shouldRunHandlerOnce && $elements.length)
    {
    window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
    }

    return $this;
    }
    92 changes: 0 additions & 92 deletions waitForKeyElements.js
    Original file line number Diff line number Diff line change
    @@ -1,92 +0,0 @@
    /*--- 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) {
    btargetsFound = true;
    /*--- 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.
    var cancelFound = actionFunction (jThis);
    if (cancelFound)
    btargetsFound = false;
    else
    jThis.data ('alreadyFound', 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
    );
    },
    300
    );
    controlObj [controlKey] = timeControl;
    }
    }
    waitForKeyElements.controlObj = controlObj;
    }
  10. @BrockA BrockA 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;
    }
  11. @BrockA BrockA 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;
  12. @BrockA BrockA 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;
  13. @BrockA BrockA 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;
    }