Wait Until Exists Version v0.2

A faster way than document ready

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/6881/27464/Wait%20Until%20Exists%20Version%20v02.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

/*
 * 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 = []
  }
})