AniwatchPlus/utils/aniwatchCore.js

77 lines
2 KiB
JavaScript
Raw Normal View History

let __scripts = [];
let __afterLoadScripts = [];
let __afterPopstateScripts = [];
let __afterPathnameChangeScripts = [];
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 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);
}
function runAfterPathnameChange(func, pattern = '.*') {
__afterPathnameChangeScripts.push({ "function": func, "pattern": pattern});
}
let locationPath = location.pathname;
let __loop = setInterval(() => {
if (locationPath != location.pathname) {
locationPath = location.pathname;
awaitPathnameChange();
}
}, 100);
function awaitPathnameChange() {
2020-08-20 16:15:35 +02:00
__afterPathnameChangeScripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function();
}
2020-08-20 16:15:35 +02:00
})
}