2020-07-29 10:03:25 +02:00
|
|
|
let __scripts = [];
|
2020-07-29 13:37:08 +02:00
|
|
|
let __afterLoadScripts = [];
|
2020-08-04 17:42:31 +02:00
|
|
|
let __afterPopstateScripts = [];
|
|
|
|
let __afterPathnameChangeScripts = [];
|
2020-07-29 10:03:25 +02:00
|
|
|
|
2020-07-29 12:12:16 +02:00
|
|
|
function registerScript(func, pattern = '.*') {
|
|
|
|
__scripts.push({ "function": func, "pattern": pattern });
|
2020-07-29 10:03:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:14:38 +02:00
|
|
|
function runScripts(node) {
|
2020-07-29 12:12:16 +02:00
|
|
|
__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-04-25 22:20:26 +02:00
|
|
|
}
|
2020-07-29 10:35:57 +02:00
|
|
|
});
|
|
|
|
});
|
2020-07-29 10:03:25 +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
|
2020-07-29 13:37:08 +02:00
|
|
|
});
|
|
|
|
|
2020-08-04 11:36:17 +02:00
|
|
|
function findPreloader() {
|
|
|
|
return document.getElementById('preloader');
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:37:08 +02:00
|
|
|
function runAfterLoad(func, pattern = '.*') {
|
2020-08-05 12:25:08 +02:00
|
|
|
let preloader = findPreloader();
|
|
|
|
if (typeof preloader !== undefined && preloader.style.display !== "none") {
|
2020-08-04 11:36:17 +02:00
|
|
|
__afterLoadScripts.push({ "function": func, "pattern": pattern });
|
|
|
|
} else {
|
|
|
|
func();
|
|
|
|
}
|
2020-07-29 13:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
|
|
|
|
|
|
|
|
function awaitPageLoaded() {
|
2020-08-04 11:36:17 +02:00
|
|
|
let preLoader = findPreloader();
|
|
|
|
|
|
|
|
let runScripts = () => {
|
|
|
|
__afterLoadScripts.forEach(script => {
|
|
|
|
if (window.location.pathname.match(script.pattern)) {
|
|
|
|
script.function();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-07-29 13:37:08 +02:00
|
|
|
|
|
|
|
if (typeof preLoader === 'undefined') {
|
2020-08-04 11:36:17 +02:00
|
|
|
runScripts();
|
2020-07-29 13:37:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let loop = setInterval(() => {
|
|
|
|
if (preLoader.style.display === "none") {
|
|
|
|
clearInterval(loop);
|
|
|
|
|
2020-08-04 11:36:17 +02:00
|
|
|
runScripts();
|
2020-07-29 13:37:08 +02:00
|
|
|
}
|
|
|
|
}, 100);
|
2020-08-04 17:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04 17:42:31 +02:00
|
|
|
}
|
2020-08-20 16:15:35 +02:00
|
|
|
})
|
2020-08-04 17:42:31 +02:00
|
|
|
}
|