#19 changing the title on page navigation

This commit is contained in:
kaffem 2020-08-04 17:42:31 +02:00
parent 05a4a730ec
commit 150d6bcfbb
2 changed files with 48 additions and 4 deletions

View file

@ -1,10 +1,21 @@
let __notificationCount = '';
runAfterLoad(() => { runAfterLoad(() => {
__notificationCount = getNotificationCount();
displayNotificationsInTitle(); displayNotificationsInTitle();
}, ".*"); }, ".*");
function displayNotificationsInTitle(){ runAfterPathnameChange(() => {
displayNotificationsInTitle();
}, ".*");
function getNotificationCount() {
let menu = document.getElementById('materialize-menu-dropdown'); let menu = document.getElementById('materialize-menu-dropdown');
let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0];
let notificationText = menuDropdowns.innerText.split(" ")[3]; let notificationText = menuDropdowns.innerText.split(" ")[3];
document.title = notificationText.split(" ")[1] + ' ' + document.title; return notificationText.split(" ")[1] + ' ';
} }
function displayNotificationsInTitle(){
document.title = __notificationCount + document.title;
}

View file

@ -1,5 +1,7 @@
let __scripts = []; let __scripts = [];
let __afterLoadScripts = []; let __afterLoadScripts = [];
let __afterPopstateScripts = [];
let __afterPathnameChangeScripts = [];
function registerScript(func, pattern = '.*') { function registerScript(func, pattern = '.*') {
__scripts.push({ "function": func, "pattern": pattern }); __scripts.push({ "function": func, "pattern": pattern });
@ -51,4 +53,35 @@ function awaitPageLoaded() {
}) })
} }
}, 100); }, 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() {
let preLoader = document.getElementById('preloader');
if (typeof preLoader === 'undefined') {
return;
}
let loop = setInterval(() => {
if (preLoader.style.display === "none") {
clearInterval(loop);
__afterPathnameChangeScripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function();
}
})
}
}, 100);
}