AniwatchPlus/utils/aniwatchCore.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

let __scripts = [];
let __afterLoadScripts = [];
function registerScript(func, pattern = '.*') {
__scripts.push({ "function": func, "pattern": pattern });
}
2020-07-29 11:14:38 +02:00
function runScripts(node) {
__scripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function(node);
}
});
2020-07-29 11:14:38 +02:00
}
2020-07-29 10:35:57 +02:00
let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
for (let i = 0; i < mutation.addedNodes.length; i++) {
2020-07-29 11:14:38 +02:00
runScripts(mutation.addedNodes[i]);
}
2020-07-29 10:35:57 +02:00
});
});
2020-07-29 10:35:57 +02:00
observer.observe(document.documentElement || document.body, {
childList: true,
2020-07-29 11:14:38 +02:00
subtree: true,
attributes: true
});
function findPreloader() {
return document.getElementById('preloader');
}
function runAfterLoad(func, pattern = '.*') {
if (findPreloader()) {
__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);
}