AniwatchPlus/utils/aniwatchCore.js

91 lines
2.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 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);
}
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
})
}