Wait Until Exists Version v0.2

A faster way than document ready

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/6881/27464/Wait%20Until%20Exists%20Version%20v02.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

/*
 * Wait Until Exists Version v0.2 - http://javascriptisawesome.blogspot.com/
 *
 *
 * TERMS OF USE - Wait Until Exists
 *
 * Open source under the BSD License.
 *
 * Copyright © 2011 Ivan Castellanos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * Neither the name of the author nor the names of contributors may be used to endorse
 * or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/
 
 
(function(){
  var _waitUntilExists = {
    pending_functions : [],
    loop_and_call : function()
    {
      if(!_waitUntilExists.pending_functions.length){return}
      for(var i=0;i<_waitUntilExists.pending_functions.length;i++)
      {
        var obj = _waitUntilExists.pending_functions[i];
        var resolution = document.getElementById(obj.id);
        if(obj.id == document){
          resolution = document.body;
        }
        if(resolution){
          var _f = obj.f;
          _waitUntilExists.pending_functions.splice(i, 1)
          if(obj.c == "itself"){obj.c = resolution}
          _f.call(obj.c)
          i--
        }
      }
    },
    global_interval : setInterval(function(){_waitUntilExists.loop_and_call()},5)
  }
  if(document.addEventListener){
    document.addEventListener("DOMNodeInserted", _waitUntilExists.loop_and_call, false);
    clearInterval(_waitUntilExists.global_interval);
  }
  window.waitUntilExists = function(id,the_function,context){
    context = context || window
    if(typeof id == "function"){context = the_function;the_function = id;id=document}
    _waitUntilExists.pending_functions.push({f:the_function,id:id,c:context})
  }
  waitUntilExists.stop = function(id,f){
    for(var i=0;i<_waitUntilExists.pending_functions.length;i++){
      if(_waitUntilExists.pending_functions[i].id==id && (typeof f == "undefined" || _waitUntilExists.pending_functions[i].f == f))
      {
        _waitUntilExists.pending_functions.splice(i, 1)
      }
    }
  }
  waitUntilExists.stopAll = function(){
    _waitUntilExists.pending_functions = []
  }
})