Greasy Fork is available in English.
sleep, waitUntil, tryCatch...
Verzia zo dňa
Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greasyfork.org/scripts/536561/1592185/utils%20function.js
// ==UserScript==
// @name utils function
// @namespace http://tampermonkey.net/
// @version 0.0.1
// @description sleep, waitUntil, tryCatch...
// @author zhowiny
// @match *://*/*
// @icon data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' viewBox='0 0 24 24'%3E%3C!-- Icon from Material Design Icons by Pictogrammers - https://github.com/Templarian/MaterialDesign/blob/master/LICENSE --%3E%3Cpath fill='currentColor' d='M12.42 5.29c-1.1-.1-2.07.71-2.17 1.82L10 10h2.82v2h-3l-.44 5.07A4.001 4.001 0 0 1 2 18.83l1.5-1.5c.33 1.05 1.46 1.64 2.5 1.3c.78-.24 1.33-.93 1.4-1.74L7.82 12h-3v-2H8l.27-3.07a4.01 4.01 0 0 1 4.33-3.65c1.26.11 2.4.81 3.06 1.89l-1.5 1.5c-.25-.77-.93-1.31-1.74-1.38M22 13.65l-1.41-1.41l-2.83 2.83l-2.83-2.83l-1.43 1.41l2.85 2.85l-2.85 2.81l1.43 1.41l2.83-2.83l2.83 2.83L22 19.31l-2.83-2.81z'/%3E%3C/svg%3E
// @grant none
// ==/UserScript==
async function sleep(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
async function waitUntil(condition, timeout) {
return new Promise((resolve, reject) => {
const t = Date.now();
const raf = async () => {
if (await condition()) {
resolve();
} else if (timeout > 0 && Date.now() - t > timeout) {
reject({message: `timeout(${timeout})`});
} else {
requestAnimationFrame(raf);
}
}
requestAnimationFrame(raf);
});
}
async function tryCatch(promise) {
try {
const data = await promise;
return { data, error: null };
} catch (error) {
return { data: null, error: error };
}
}
(function() {
'use strict';
window.__utils__ = {
sleep,
waitUntil,
tryCatch,
}
})();