From 1b663795156e87919e67f1c838939813be2e57ea Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 13:37:08 +0200 Subject: [PATCH] #12 Added runAfterLoad in aniwatch Core --- utils/aniwatchCore.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index fb7587a..4552c98 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -1,4 +1,5 @@ let __scripts = []; +let __afterLoadScripts = []; function registerScript(func, pattern = '.*') { __scripts.push({ "function": func, "pattern": pattern }); @@ -24,4 +25,30 @@ observer.observe(document.documentElement || document.body, { childList: true, subtree: true, attributes: true -}); \ No newline at end of file +}); + +function runAfterLoad(func, pattern = '.*') { + __afterLoadScripts.push({ "function": func, "pattern": pattern }); +} + +document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false); + +function awaitPageLoaded() { + let preLoader = document.getElementById('preloader'); + + if (typeof preLoader === 'undefined') { + return; + } + + let loop = setInterval(() => { + if (preLoader.style.display === "none") { + clearInterval(loop); + + __afterLoadScripts.forEach(script => { + if (window.location.pathname.match(script.pattern)) { + script.function(); + } + }) + } + }, 100); +} \ No newline at end of file