AniwatchPlus/utils/aniwatchCore.js
Serraniel 324b3f5c76
#23 Fix if preloader always hidden
Previous if statement did not work because it still was found in DOM so it only was pushed into the array but not executed.
2020-08-05 12:25:08 +02:00

68 lines
1.6 KiB
JavaScript

let __scripts = [];
let __afterLoadScripts = [];
function registerScript(func, pattern = '.*') {
__scripts.push({ "function": func, "pattern": pattern });
}
function runScripts(node) {
__scripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function(node);
}
});
}
let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
for (let i = 0; i < mutation.addedNodes.length; i++) {
runScripts(mutation.addedNodes[i]);
}
});
});
observer.observe(document.documentElement || document.body, {
childList: true,
subtree: true,
attributes: true
});
function findPreloader() {
return document.getElementById('preloader');
}
function runAfterLoad(func, pattern = '.*') {
let preloader = findPreloader();
if (typeof preloader !== undefined && preloader.style.display !== "none") {
__afterLoadScripts.push({ "function": func, "pattern": pattern });
} else {
func();
}
}
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
function awaitPageLoaded() {
let preLoader = findPreloader();
let runScripts = () => {
__afterLoadScripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function();
}
});
};
if (typeof preLoader === 'undefined') {
runScripts();
return;
}
let loop = setInterval(() => {
if (preLoader.style.display === "none") {
clearInterval(loop);
runScripts();
}
}, 100);
}