#23 Run script directly if page already loaded

(cherry picked from commit 8460c862e97866e43e06e7624b2ce4f38d0ecd0d)
This commit is contained in:
Serraniel 2020-08-04 11:36:17 +02:00
parent dd5790695a
commit 5f63ecb336
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3

View file

@ -27,16 +27,33 @@ observer.observe(document.documentElement || document.body, {
attributes: true attributes: true
}); });
function findPreloader() {
return document.getElementById('preloader');
}
function runAfterLoad(func, pattern = '.*') { function runAfterLoad(func, pattern = '.*') {
if (findPreloader()) {
__afterLoadScripts.push({ "function": func, "pattern": pattern }); __afterLoadScripts.push({ "function": func, "pattern": pattern });
} else {
func();
}
} }
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false); document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
function awaitPageLoaded() { function awaitPageLoaded() {
let preLoader = document.getElementById('preloader'); let preLoader = findPreloader();
let runScripts = () => {
__afterLoadScripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function();
}
});
};
if (typeof preLoader === 'undefined') { if (typeof preLoader === 'undefined') {
runScripts();
return; return;
} }
@ -44,11 +61,7 @@ function awaitPageLoaded() {
if (preLoader.style.display === "none") { if (preLoader.style.display === "none") {
clearInterval(loop); clearInterval(loop);
__afterLoadScripts.forEach(script => { runScripts();
if (window.location.pathname.match(script.pattern)) {
script.function();
}
})
} }
}, 100); }, 100);
} }