#23 Run script directly if page already loaded #24
|
@ -27,16 +27,33 @@ observer.observe(document.documentElement || document.body, {
|
|||
attributes: true
|
||||
});
|
||||
|
||||
function findPreloader() {
|
||||
return document.getElementById('preloader');
|
||||
}
|
||||
|
||||
function runAfterLoad(func, pattern = '.*') {
|
||||
__afterLoadScripts.push({ "function": func, "pattern": pattern });
|
||||
if (findPreloader()) {
|
||||
__afterLoadScripts.push({ "function": func, "pattern": pattern });
|
||||
} else {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
|
||||
|
||||
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') {
|
||||
runScripts();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -44,11 +61,7 @@ function awaitPageLoaded() {
|
|||
if (preLoader.style.display === "none") {
|
||||
clearInterval(loop);
|
||||
|
||||
__afterLoadScripts.forEach(script => {
|
||||
if (window.location.pathname.match(script.pattern)) {
|
||||
script.function();
|
||||
}
|
||||
})
|
||||
runScripts();
|
||||
}
|
||||
}, 100);
|
||||
}
|
Loading…
Reference in a new issue