Skip to content

Instantly share code, notes, and snippets.

@PizzaBrandon
Forked from buu700/jquery.waituntilexists.js
Last active August 24, 2023 14:23
Show Gist options
  • Select an option

  • Save PizzaBrandon/5709010 to your computer and use it in GitHub Desktop.

Select an option

Save PizzaBrandon/5709010 to your computer and use it in GitHub Desktop.

Revisions

  1. PizzaBrandon revised this gist Jun 5, 2013. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ;(function ($) {
    ;(function ($, window) {

    var intervals = {};
    var removeListener = function(selector) {
    @@ -59,4 +59,4 @@ $.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {
    return $this;
    };

    }(jQuery));
    }(jQuery, window));
    2 changes: 1 addition & 1 deletion jquery.waituntilexists.min.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    (function(e){var b={},f=function(a){b[a]&&(window.clearInterval(b[a]),b[a]=null)};e.fn.waitUntilExists=function(a,g,h){var c=this.selector,d=e(c),j=d.not(function(){return e(this).data("waitUntilExists.found")});"remove"===a?f(c):(j.each(a).data("waitUntilExists.found",!0),g&&d.length?f(c):h||(b[c]=window.setInterval(function(){d.waitUntilExists(a,g,!0)},500)));return d}})(jQuery);
    (function(e,f){var b={},g=function(a){b[a]&&(f.clearInterval(b[a]),b[a]=null)};e.fn.waitUntilExists=function(a,h,j){var c=this.selector,d=e(c),k=d.not(function(){return e(this).data("waitUntilExists.found")});"remove"===a?g(c):(k.each(a).data("waitUntilExists.found",!0),h&&d.length?g(c):j||(b[c]=f.setInterval(function(){d.waitUntilExists(a,h,!0)},500)));return d}})(jQuery,window);
  2. PizzaBrandon revised this gist Jun 5, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jquery.waituntilexists.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    (function(e){var b={},f=function(a){b[a]&&(window.clearInterval(b[a]),b[a]=null)};e.fn.waitUntilExists=function(a,g,h){var c=this.selector,d=e(c),j=d.not(function(){return e(this).data("waitUntilExists.found")});"remove"===a?f(c):(j.each(a).data("waitUntilExists.found",!0),g&&d.length?f(c):h||(b[c]=window.setInterval(function(){d.waitUntilExists(a,g,!0)},500)));return d}})(jQuery);
  3. PizzaBrandon revised this gist Jun 4, 2013. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -12,12 +12,16 @@ var removeListener = function(selector) {
    var found = 'waitUntilExists.found';

    /**
    * @function
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    * @param {function|string} handler A function to execute at the time when the element is inserted or string "remove" to remove the listener from the given selector
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example jQuery(selector).waitUntilExists(function);
    */
    * @function
    * @property {object} jQuery plugin which runs handler function once specified
    * element is inserted into the DOM
    * @param {function|string} handler
    * A function to execute at the time when the element is inserted or
    * string "remove" to remove the listener from the given selector
    * @param {bool} shouldRunHandlerOnce
    * Optional: if true, handler is unbound after its first invocation
    * @example jQuery(selector).waitUntilExists(function);
    */

    $.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {

    @@ -32,11 +36,8 @@ $.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {
    }
    else {

    if ($elements.length) {

    //
    $elements.each(handler).data(found, true);
    }
    // Run the handler on all found elements and mark as found
    $elements.each(handler).data(found, true);

    if (shouldRunHandlerOnce && $this.length) {

  4. PizzaBrandon revised this gist Jun 4, 2013. 1 changed file with 47 additions and 16 deletions.
    63 changes: 47 additions & 16 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,61 @@
    ;(function ($) {

    var intervals = {};
    var removeListener = function(selector) {

    if (intervals[selector]) {

    window.clearInterval(intervals[selector]);
    intervals[selector] = null;
    }
    };
    var found = 'waitUntilExists.found';

    /**
    * @function
    * @function
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    * @param {function|string} handler A function to execute at the time when the element is inserted or string "remove" to remove the listener from the given selector
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $(selector).waitUntilExists(function);
    * @example jQuery(selector).waitUntilExists(function);
    */

    $.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {

    $.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);
    var selector = this.selector;
    var $this = $(selector);
    var $elements = $this.not(function() { return $(this).data(found); });

    if (!isChild)
    {
    (window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
    window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
    ;
    if (handler === 'remove') {

    // Hijack and remove interval immediately if the code requests
    removeListener(selector);
    }
    else if (handler === "remove" || (shouldRunHandlerOnce && $elements.length))
    {
    window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
    else {

    if ($elements.length) {

    //
    $elements.each(handler).data(found, true);
    }

    if (shouldRunHandlerOnce && $this.length) {

    // Element was found, implying the handler already ran for all
    // matched elements
    removeListener(selector);
    }
    else if (!isChild) {

    // If this is a recurring search or if the target has not yet been
    // found, create an interval to continue searching for the target
    intervals[selector] = window.setInterval(function () {

    $this.waitUntilExists(handler, shouldRunHandlerOnce, true);
    }, 500);
    }
    }

    return $this;
    }

    };
    }(jQuery));
  5. PizzaBrandon revised this gist Jun 4, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions jquery.waituntilexists.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    (function ($) {
    ;(function ($) {

    /**
    * @function
    * @function
    * @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
    * @param {function} handler A function to execute at the time when the element is inserted
    * @param {function|string} handler A function to execute at the time when the element is inserted or string "remove" to remove the listener from the given selector
    * @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
    * @example $(selector).waitUntilExists(function);
    */
    @@ -19,7 +19,7 @@ $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
    window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
    ;
    }
    else if (shouldRunHandlerOnce && $elements.length)
    else if (handler === "remove" || (shouldRunHandlerOnce && $elements.length))
    {
    window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
    }
  6. @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));
  7. @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;
    }
    }
  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
    @@ -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) {
  9. @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)
  10. @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)
  11. @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)
    {
  12. @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.'); })
  13. @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;
    }
  14. 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;
    }
  15. 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;
  16. 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;
  17. 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;
    }