#12 Added runAfterLoad in aniwatch Core

This commit is contained in:
Serraniel 2020-07-29 13:37:08 +02:00
parent 0ca4b42431
commit 1b66379515
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3

View file

@ -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
});
});
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);
}