From 6c225408ceb43beef43bfa4cae64754edf5bad68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3nalan=20de=20Lima?= <67329824+jonalan7@users.noreply.github.com> Date: Fri, 16 Jun 2023 13:48:43 +0000 Subject: [PATCH] fix: wait For Objects webpack --- src/lib/wapi/wapi.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/lib/wapi/wapi.js b/src/lib/wapi/wapi.js index c2fd52616..4b89645b6 100644 --- a/src/lib/wapi/wapi.js +++ b/src/lib/wapi/wapi.js @@ -178,21 +178,35 @@ function injectParasite() { } } +async function waitForObjects() { + return new Promise((resolve) => { + const checkObjects = () => { + if ( + window[injectConfig.webpack] && + Array.isArray(window[injectConfig.webpack]) && + window[injectConfig.webpack].every( + (item) => Array.isArray(item) && item.length > 0 + ) + ) { + resolve(); + } else { + setTimeout(checkObjects, 200); + } + }; + + checkObjects(); + }); +} + (async () => { - // window[injectConfig.webpack] = window[injectConfig.webpack] || []; - if (typeof window[injectConfig.webpack] === 'undefined') { - window[injectConfig.webpack] = []; - } - while (true) { - const last = window[injectConfig.webpack].length - 1; - if ( - !window[injectConfig.webpack][last][0].includes(injectConfig.parasite) && - document.querySelectorAll('#app .two').length - ) { - injectParasite(); - break; - } - await sleep(2000); + await waitForObjects(); + + const last = window[injectConfig.webpack].length - 1; + if ( + !window[injectConfig.webpack][last][0].includes(injectConfig.parasite) && + document.querySelectorAll('#app .two').length + ) { + injectParasite(); } })();