WaitUntil

等待满足某种条件后执行,目前不支持嵌套调用

Verze ze dne 11. 10. 2020. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/412875/856255/WaitUntil.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         WaitUntil
// @namespace    http://bbs.91wc.net/?wait-until
// @version      0.1
// @description  等待满足某种条件后执行,目前不支持嵌套调用
// @author       Wilson
// ==/UserScript==

var WaitUntil = (function(){
    var waitUntil=function(condfunc, callback, interval, trys){
        var getGuid = getGuid||function() {
            var d = new Date().getTime();
            var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = (d + Math.random()*16)%16 | 0;
                d = Math.floor(d/16);
                return (c=='x' ? r : (r&0x3|0x8)).toString(16);
            });
            return uuid;
        };
        var timer = {}, counter={};
        var waiter = function(condfunc, callback, interval, trys, guid){
            guid = guid || getGuid();
            interval = interval || 100;
            trys = trys || 300;
            counter[guid] = counter[guid] ? counter[guid]++ : 1;
            if(counter[guid]>trys){
                if(timer[guid]) clearTimeout(timer[guid]);
                //callback('fail');
                return;
            }
            timer[guid] = setTimeout(function(){
                if(condfunc()){
                   if(timer[guid]) clearTimeout(timer[guid]);
                    callback('ok');
                } else {
                    if(timer[guid]) clearTimeout(timer[guid]);
                    waiter(condfunc, callback, interval, trys, guid);
                }
            }, interval);
        }
        waiter(condfunc, callback, interval, trys);
    }
    return waitUntil;
})();

//使用示例
//WaitUntil(function(){return typeof jQuery !=="undefined" && $(".test").length>0}, function(){
//    alert($(".test").html());
//});

//其他参数
//condfunc 满足条件的函数
//callback 条件满足后执行的回调函数
//interval 监听满足条件的时间间隔,单位毫秒,默认100
//trys 尝试次数,超过这个数值就停止监听,默认为300(因默认间隔是100毫秒,所以默认停止监控时间为300*100毫秒,即30秒)