From 76d2a0fcd9e1167b97bbc9f988071a9f69a8a3c8 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Wed, 29 Jul 2020 22:48:49 +0200 Subject: [PATCH 01/21] #19 Added displayNotificationsInTitle in notifications --- enhancements/notifications.js | 10 ++++++++++ manifest.json | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 enhancements/notifications.js diff --git a/enhancements/notifications.js b/enhancements/notifications.js new file mode 100644 index 0000000..310dffd --- /dev/null +++ b/enhancements/notifications.js @@ -0,0 +1,10 @@ +runAfterLoad(() => { + displayNotificationsInTitle(); +}, ".*"); + +function displayNotificationsInTitle(){ +let menu = document.getElementById('materialize-menu-dropdown'); +let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; +let notificationText = menuDropdowns.innerText.split(" ")[3]; +document.querySelector('head > title').insertAdjacentText('afterbegin', notificationText.split(" ")[1] + ' '); +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 22d1468..a975e11 100644 --- a/manifest.json +++ b/manifest.json @@ -22,7 +22,8 @@ ], "js": [ "enhancements/quickSearch.js", - "enhancements/animeRequests.js" + "enhancements/animeRequests.js", + "enhancements/notifications.js" ], "run_at": "document_end" } -- 2.43.4 From 1715e8fa7f6bda9f22b90c5630e6c4d32fd3e257 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Wed, 29 Jul 2020 23:09:13 +0200 Subject: [PATCH 02/21] using space indentation instead of tab --- enhancements/notifications.js | 8 ++++---- manifest.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/enhancements/notifications.js b/enhancements/notifications.js index 310dffd..af1b7e5 100644 --- a/enhancements/notifications.js +++ b/enhancements/notifications.js @@ -3,8 +3,8 @@ runAfterLoad(() => { }, ".*"); function displayNotificationsInTitle(){ -let menu = document.getElementById('materialize-menu-dropdown'); -let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; -let notificationText = menuDropdowns.innerText.split(" ")[3]; -document.querySelector('head > title').insertAdjacentText('afterbegin', notificationText.split(" ")[1] + ' '); + let menu = document.getElementById('materialize-menu-dropdown'); + let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; + let notificationText = menuDropdowns.innerText.split(" ")[3]; + document.querySelector('head > title').insertAdjacentText('afterbegin', notificationText.split(" ")[1] + ' '); } \ No newline at end of file diff --git a/manifest.json b/manifest.json index a975e11..24ac9e7 100644 --- a/manifest.json +++ b/manifest.json @@ -23,7 +23,7 @@ "js": [ "enhancements/quickSearch.js", "enhancements/animeRequests.js", - "enhancements/notifications.js" + "enhancements/notifications.js" ], "run_at": "document_end" } -- 2.43.4 From 05a4a730ec6eec8933242311fd4fbb261ceca5d3 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Sat, 1 Aug 2020 02:36:40 +0200 Subject: [PATCH 03/21] #19 changing the title via document.title --- enhancements/notifications.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enhancements/notifications.js b/enhancements/notifications.js index af1b7e5..2e63723 100644 --- a/enhancements/notifications.js +++ b/enhancements/notifications.js @@ -6,5 +6,5 @@ function displayNotificationsInTitle(){ let menu = document.getElementById('materialize-menu-dropdown'); let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; let notificationText = menuDropdowns.innerText.split(" ")[3]; - document.querySelector('head > title').insertAdjacentText('afterbegin', notificationText.split(" ")[1] + ' '); + document.title = notificationText.split(" ")[1] + ' ' + document.title; } \ No newline at end of file -- 2.43.4 From 150d6bcfbb61c7a33f02ecba896c60b8878b5211 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Tue, 4 Aug 2020 17:42:31 +0200 Subject: [PATCH 04/21] #19 changing the title on page navigation --- enhancements/notifications.js | 17 ++++++++++++++--- utils/aniwatchCore.js | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/enhancements/notifications.js b/enhancements/notifications.js index 2e63723..c6c984f 100644 --- a/enhancements/notifications.js +++ b/enhancements/notifications.js @@ -1,10 +1,21 @@ +let __notificationCount = ''; + runAfterLoad(() => { + __notificationCount = getNotificationCount(); displayNotificationsInTitle(); }, ".*"); -function displayNotificationsInTitle(){ +runAfterPathnameChange(() => { + displayNotificationsInTitle(); +}, ".*"); + +function getNotificationCount() { let menu = document.getElementById('materialize-menu-dropdown'); let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; let notificationText = menuDropdowns.innerText.split(" ")[3]; - document.title = notificationText.split(" ")[1] + ' ' + document.title; -} \ No newline at end of file + return notificationText.split(" ")[1] + ' '; +} + +function displayNotificationsInTitle(){ + document.title = __notificationCount + document.title; +} diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index 4552c98..79a67f2 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -1,5 +1,7 @@ let __scripts = []; let __afterLoadScripts = []; +let __afterPopstateScripts = []; +let __afterPathnameChangeScripts = []; function registerScript(func, pattern = '.*') { __scripts.push({ "function": func, "pattern": pattern }); @@ -51,4 +53,35 @@ function awaitPageLoaded() { }) } }, 100); -} \ No newline at end of file +} + +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); +} -- 2.43.4 From b65b0ce29c02fc07a18791a5578ad17b0fffd453 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 20 Aug 2020 16:04:58 +0200 Subject: [PATCH 05/21] added a basic way to retrieve the login status --- utils/helpers.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/utils/helpers.js b/utils/helpers.js index ed73b4f..5061ff9 100644 --- a/utils/helpers.js +++ b/utils/helpers.js @@ -1,5 +1,6 @@ var isShiftPressed = false; var isCtrlPressed = false; +var isLoggedIn = false; function isHtmlElement(object) { return object instanceof HTMLElement; @@ -22,4 +23,20 @@ function handleKeyToggle(event, isPressed) { } else if (event.key === 'Control') { isCtrlPressed = isPressed; } -} \ No newline at end of file +} + +function retrieveLoginStatus() { + let menu = document.getElementById('materialize-menu-dropdown'); + let menuItem = menu.innerText.split('\n')[4]; + if (menuItem === 'Login') { + isLoggedIn = false; + console.log(isLoggedIn); + } + else if (menuItem.includes('User')) { + isLoggedIn = true; + console.log(isLoggedIn); + } + else { + console.error("Something went wrong, couldn't retrieve login status."); + } +} -- 2.43.4 From a6654ebbf760eb99ce01a425451c7348056146c3 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 20 Aug 2020 16:09:08 +0200 Subject: [PATCH 06/21] simplified retrieving the notification count --- enhancements/notifications.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/enhancements/notifications.js b/enhancements/notifications.js index c6c984f..c3db45e 100644 --- a/enhancements/notifications.js +++ b/enhancements/notifications.js @@ -10,10 +10,8 @@ runAfterPathnameChange(() => { }, ".*"); function getNotificationCount() { - let menu = document.getElementById('materialize-menu-dropdown'); - let menuDropdowns = Array.from(menu.querySelectorAll('ul.dropdown')).slice(-1)[0]; - let notificationText = menuDropdowns.innerText.split(" ")[3]; - return notificationText.split(" ")[1] + ' '; + let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; + return menuUserText.split(" ")[1] + ' '; } function displayNotificationsInTitle(){ -- 2.43.4 From c7ac8450b71c4b317ee9498335dac06a52d04775 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 20 Aug 2020 16:14:07 +0200 Subject: [PATCH 07/21] added login checks before retrieving notification count --- enhancements/notifications.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/enhancements/notifications.js b/enhancements/notifications.js index c3db45e..e91f6e5 100644 --- a/enhancements/notifications.js +++ b/enhancements/notifications.js @@ -1,6 +1,7 @@ let __notificationCount = ''; runAfterLoad(() => { + retrieveLoginStatus(); __notificationCount = getNotificationCount(); displayNotificationsInTitle(); }, ".*"); @@ -10,8 +11,10 @@ runAfterPathnameChange(() => { }, ".*"); function getNotificationCount() { - let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; - return menuUserText.split(" ")[1] + ' '; + if (isLoggedIn) { + let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; + return menuUserText.split(" ")[1] + ' '; + } } function displayNotificationsInTitle(){ -- 2.43.4 From ef83390a4d5f8fa067c88e6d397a0033cc5bea4f Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 20 Aug 2020 16:15:35 +0200 Subject: [PATCH 08/21] removed unnecessary functionality --- utils/aniwatchCore.js | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index 79a67f2..0b98875 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -68,20 +68,9 @@ let __loop = setInterval(() => { }, 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(); - } - }) + __afterPathnameChangeScripts.forEach(script => { + if (window.location.pathname.match(script.pattern)) { + script.function(); } - }, 100); + }) } -- 2.43.4 From 9fe2668c3afec7a4b100cffc9ae722d1baa13fe7 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 20 Aug 2020 17:47:00 +0200 Subject: [PATCH 09/21] #19 Added checks for notifications --- enhancements/notifications.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/enhancements/notifications.js b/enhancements/notifications.js index e91f6e5..daeb1e2 100644 --- a/enhancements/notifications.js +++ b/enhancements/notifications.js @@ -13,10 +13,26 @@ runAfterPathnameChange(() => { function getNotificationCount() { if (isLoggedIn) { let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; - return menuUserText.split(" ")[1] + ' '; + let notificationCount = menuUserText.split("")[6]; + console.log(notificationCount); + // If there are no notifications + if (Number.isNaN(parseInt(notificationCount)) || typeof notificationCount === 'undefined') { + console.warn("NaN or undefined"); + return ``; // Otherwise displayNotificationsInTitle() throws undefined again + } + // Notifications present + else { + return `(${notificationCount}) `; + } } } -function displayNotificationsInTitle(){ - document.title = __notificationCount + document.title; +function displayNotificationsInTitle() { + console.log(__notificationCount); + if (typeof __notificationCount === 'undefined') { + console.error("NoTiFiCaTiOnCoUnT uNdEfInEd!"); + } + else { + document.title = __notificationCount + document.title; + } } -- 2.43.4 From 67d59d55727494197f89e4776012cc5458f77619 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 20 Aug 2020 17:53:05 +0200 Subject: [PATCH 10/21] removed unnecessary logging --- utils/helpers.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/utils/helpers.js b/utils/helpers.js index 5061ff9..bd14c5c 100644 --- a/utils/helpers.js +++ b/utils/helpers.js @@ -36,7 +36,4 @@ function retrieveLoginStatus() { isLoggedIn = true; console.log(isLoggedIn); } - else { - console.error("Something went wrong, couldn't retrieve login status."); - } } -- 2.43.4 From e1ca8a7fac471022d0a3577957f32a6c017b15a4 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Thu, 10 Sep 2020 18:15:41 +0200 Subject: [PATCH 11/21] removed obsulete code --- utils/aniwatchCore.js | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index a4a3e08..ee22f8e 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -1,6 +1,5 @@ let __scripts = []; let __afterLoadScripts = []; -let __afterPopstateScripts = []; let __afterPathnameChangeScripts = []; function registerScript(func, pattern = '.*') { -- 2.43.4 From 1962547d75f7e23243e68f1f9ac38d8b0a7e88da Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 11 Sep 2020 16:41:22 +0200 Subject: [PATCH 12/21] #19 First fixes after the great merge --- src/javascript/enhancements/notifications.js | 35 +++++++++++--------- src/javascript/index.js | 2 ++ src/javascript/utils/aniwatchCore.js | 18 ++++++++-- src/javascript/utils/helpers.js | 16 +++------ 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js index daeb1e2..32b2f4a 100644 --- a/src/javascript/enhancements/notifications.js +++ b/src/javascript/enhancements/notifications.js @@ -1,22 +1,26 @@ +import * as core from '../utils/aniwatchCore'; +import * as helper from '../utils/helpers'; + let __notificationCount = ''; -runAfterLoad(() => { - retrieveLoginStatus(); - __notificationCount = getNotificationCount(); - displayNotificationsInTitle(); -}, ".*"); +export function init() { + core.runAfterLoad(() => { + retrieveLoginStatus(); + __notificationCount = getNotificationCount(); + displayNotificationsInTitle(); + }, ".*"); -runAfterPathnameChange(() => { - displayNotificationsInTitle(); -}, ".*"); + core.runAfterPathnameChange(() => { + displayNotificationsInTitle(); + }, ".*"); +} function getNotificationCount() { - if (isLoggedIn) { + if (core.isLoggedIn()) { let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; - let notificationCount = menuUserText.split("")[6]; - console.log(notificationCount); + let notificationCount = menuUserText.split('')[6]; // If there are no notifications - if (Number.isNaN(parseInt(notificationCount)) || typeof notificationCount === 'undefined') { + if (Number.isNaN(parseInt(notificationCount)) || !helper.assigned(notificationCount)) { console.warn("NaN or undefined"); return ``; // Otherwise displayNotificationsInTitle() throws undefined again } @@ -29,10 +33,9 @@ function getNotificationCount() { function displayNotificationsInTitle() { console.log(__notificationCount); - if (typeof __notificationCount === 'undefined') { + if (!helper.assigned(__notificationCount)) { console.error("NoTiFiCaTiOnCoUnT uNdEfInEd!"); - } - else { + } else { document.title = __notificationCount + document.title; } -} +} \ No newline at end of file diff --git a/src/javascript/index.js b/src/javascript/index.js index 0052b29..69cf4fc 100644 --- a/src/javascript/index.js +++ b/src/javascript/index.js @@ -7,6 +7,7 @@ import { initHelpers } from './utils/helpers'; // enhancements import { init as animeRequests } from './enhancements/animeRequests'; import { init as lists } from './enhancements/lists'; +import { init as notifications } from './enhancements/notifications'; import { init as quickSearch } from './enhancements/quickSearch'; // core @@ -18,4 +19,5 @@ initHelpers(); // enhancements animeRequests(); lists(); +notifications(); quickSearch(); \ No newline at end of file diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index 305a93d..1434969 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -72,8 +72,20 @@ function awaitPageLoaded() { }, 100); } -function runAfterPathnameChange(func, pattern = '.*') { - __afterPathnameChangeScripts.push({ "function": func, "pattern": pattern}); +export function runAfterPathnameChange(func, pattern = '.*') { + __afterPathnameChangeScripts.push({ "function": func, "pattern": pattern }); +} + +export function isLoggedIn() { + let menu = document.getElementById('materialize-menu-dropdown'); + let result = true; + + menu.innerText.split('\n').forEach(item => { + if (item === 'Login') { + result = false; + return; + } + }); } let locationPath = location.pathname; @@ -90,4 +102,4 @@ function awaitPathnameChange() { script.function(); } }) -} +} \ No newline at end of file diff --git a/src/javascript/utils/helpers.js b/src/javascript/utils/helpers.js index e483fb1..053aa18 100644 --- a/src/javascript/utils/helpers.js +++ b/src/javascript/utils/helpers.js @@ -18,6 +18,10 @@ export function onReady(fn) { } } +export function assigned(obj) { + return !(typeof obj === 'undefined' || obj === null); +} + function handleKeyDown(event) { handleKeyToggle(event, true); } @@ -32,16 +36,4 @@ function handleKeyToggle(event, isPressed) { } else if (event.key === 'Control') { isCtrlPressed = isPressed; } -} - -export function retrieveLoginStatus() { - let menu = document.getElementById('materialize-menu-dropdown'); - let menuItem = menu.innerText.split('\n')[4]; - if (menuItem === 'Login') { - isLoggedIn = false; - console.log(isLoggedIn); - } else if (menuItem.includes('User')) { - isLoggedIn = true; - console.log(isLoggedIn); - } } \ No newline at end of file -- 2.43.4 From 065ce97adc9b5f4cb696853ec71d4de366a37274 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 11 Sep 2020 16:49:45 +0200 Subject: [PATCH 13/21] #19 Fixed missing return value in isLoggedIn function --- src/javascript/utils/aniwatchCore.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index 1434969..0d9ab6c 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -86,6 +86,8 @@ export function isLoggedIn() { return; } }); + + return result; } let locationPath = location.pathname; -- 2.43.4 From 7e44b0d538744e4a6c6294281e5a44f93eb5984f Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 11 Sep 2020 17:36:36 +0200 Subject: [PATCH 14/21] #19 Patch browser functions to get event on location change --- src/javascript/utils/aniwatchCore.js | 59 ++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index 0d9ab6c..5ba9891 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -1,5 +1,6 @@ import * as helper from './helpers'; +/* SCRIPT LOGICS */ let __scripts = []; let __afterLoadScripts = []; let __afterPathnameChangeScripts = []; @@ -19,6 +20,9 @@ export function initCore() { attributes: true }); + patchBrowser(); + window.addEventListener('locationchange', (event) => handleLocationChanged(event)); + helper.onReady(() => awaitPageLoaded()); } @@ -72,10 +76,49 @@ function awaitPageLoaded() { }, 100); } +/* PATHNAME LOGIC */ export function runAfterPathnameChange(func, pattern = '.*') { __afterPathnameChangeScripts.push({ "function": func, "pattern": pattern }); } +function handleLocationChanged(event) { + __afterPathnameChangeScripts.forEach(script => { + if (window.location.pathname.match(script.pattern)) { + script.function(); + } + }); +} + +function patchBrowser() { + // patches several browser functions to dispatch a "locationchange" event + // as an extension is not allowed to override these functions we have to inject this as a script tag into the head + let scriptContent = `history.pushState = (func => function pushState() { + let result = func.apply(this, arguments); + window.dispatchEvent(new Event('pushstate')); + window.dispatchEvent(new Event('locationchange')); + + return result; + })(history.pushState); + + history.replaceState = (func => function replaceState() { + let result = func.apply(this, arguments); + window.dispatchEvent(new Event('replacestate')); + window.dispatchEvent(new Event('locationchange')); + return result; + })(history.replaceState); + + window.addEventListener('popstate', () => { + window.dispatchEvent(new Event('locationchange')) + });` + + let head = document.getElementsByTagName("head")[0]; + let newScript = document.createElement('script'); + newScript.type = 'text/javascript'; + newScript.innerHTML = scriptContent; + head.appendChild(newScript); +} + +/* LOGIN LOGIC */ export function isLoggedIn() { let menu = document.getElementById('materialize-menu-dropdown'); let result = true; @@ -88,20 +131,4 @@ export function isLoggedIn() { }); return result; -} - -let locationPath = location.pathname; -let __loop = setInterval(() => { - if (locationPath != location.pathname) { - locationPath = location.pathname; - awaitPathnameChange(); - } -}, 100); - -function awaitPathnameChange() { - __afterPathnameChangeScripts.forEach(script => { - if (window.location.pathname.match(script.pattern)) { - script.function(); - } - }) } \ No newline at end of file -- 2.43.4 From b1aef75409b3d538f4ab9255e860724bd54d9439 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 11 Sep 2020 17:57:17 +0200 Subject: [PATCH 15/21] #19 Imporvments in notification logic --- src/javascript/enhancements/notifications.js | 36 ++++++++------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js index 32b2f4a..7719064 100644 --- a/src/javascript/enhancements/notifications.js +++ b/src/javascript/enhancements/notifications.js @@ -1,41 +1,33 @@ import * as core from '../utils/aniwatchCore'; import * as helper from '../utils/helpers'; -let __notificationCount = ''; - export function init() { core.runAfterLoad(() => { - retrieveLoginStatus(); - __notificationCount = getNotificationCount(); - displayNotificationsInTitle(); + updateNotificationsInTitle(); }, ".*"); core.runAfterPathnameChange(() => { - displayNotificationsInTitle(); + console.log('CHANGE') + updateNotificationsInTitle(); }, ".*"); } function getNotificationCount() { if (core.isLoggedIn()) { let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; - let notificationCount = menuUserText.split('')[6]; - // If there are no notifications - if (Number.isNaN(parseInt(notificationCount)) || !helper.assigned(notificationCount)) { - console.warn("NaN or undefined"); - return ``; // Otherwise displayNotificationsInTitle() throws undefined again - } - // Notifications present - else { - return `(${notificationCount}) `; - } + console.log(menuUserText); + let notificationCount = menuUserText.match(/\d+/)?.[0] ?? 0; + console.log(notificationCount); + return notificationCount; + } else { + return 0; } } -function displayNotificationsInTitle() { - console.log(__notificationCount); - if (!helper.assigned(__notificationCount)) { - console.error("NoTiFiCaTiOnCoUnT uNdEfInEd!"); - } else { - document.title = __notificationCount + document.title; +function updateNotificationsInTitle() { + let count = getNotificationCount(); + + if (helper.assigned(count) && count > 0) { + document.title = `(${count}) ${document.title}`; } } \ No newline at end of file -- 2.43.4 From 08ce64ad8bfecd49d1d6e0ff82aeb5aea1355460 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 11 Sep 2020 18:01:23 +0200 Subject: [PATCH 16/21] #19 Last fixes --- src/javascript/enhancements/notifications.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js index 7719064..efdbd83 100644 --- a/src/javascript/enhancements/notifications.js +++ b/src/javascript/enhancements/notifications.js @@ -7,7 +7,6 @@ export function init() { }, ".*"); core.runAfterPathnameChange(() => { - console.log('CHANGE') updateNotificationsInTitle(); }, ".*"); } @@ -15,9 +14,7 @@ export function init() { function getNotificationCount() { if (core.isLoggedIn()) { let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; - console.log(menuUserText); let notificationCount = menuUserText.match(/\d+/)?.[0] ?? 0; - console.log(notificationCount); return notificationCount; } else { return 0; @@ -28,6 +25,9 @@ function updateNotificationsInTitle() { let count = getNotificationCount(); if (helper.assigned(count) && count > 0) { - document.title = `(${count}) ${document.title}`; + // document.title is updated after the event is triggered, so we delay our title update by a reasonable time + setTimeout(() => { + document.title = `(${count}) ${document.title}`; + }, 100); } } \ No newline at end of file -- 2.43.4 From 585c1684a29f2927fcbecc9bcae00c64cee76389 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Sun, 25 Oct 2020 10:54:32 +0100 Subject: [PATCH 17/21] #19 Added loading bar observer --- src/javascript/utils/aniwatchCore.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index 5ba9891..857db5b 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -20,8 +20,31 @@ export function initCore() { attributes: true }); - patchBrowser(); - window.addEventListener('locationchange', (event) => handleLocationChanged(event)); + // patchBrowser(); + // window.addEventListener('locationchange', (event) => handleLocationChanged(event)); + + runAfterLoad(() => { + let loadingBar = document.getElementById('enable-ani-cm'); + let loadingBarObserver = new MutationObserver(mutations => { + mutations.forEach(mutation => { + // enable-ani-cm node changes from display:none to display:block after loading + if (mutation.oldValue.includes('display: none')) { + __afterPathnameChangeScripts.forEach(script => { + if (window.location.pathname.match(script.pattern)) { + script.function(); + } + }); + } + }) + }); + + loadingBarObserver.observe(loadingBar, { + attributes: true, + attributeOldValue: true, + attributeFilter: ['style'], + }); + + }, '.*') helper.onReady(() => awaitPageLoaded()); } -- 2.43.4 From a840f644b723cb885232f4184698db2a27bc5f7d Mon Sep 17 00:00:00 2001 From: Serraniel Date: Sun, 25 Oct 2020 10:56:50 +0100 Subject: [PATCH 18/21] #19 Removed old and obsolete pathname change logic --- src/javascript/enhancements/notifications.js | 2 +- src/javascript/utils/aniwatchCore.js | 48 ++------------------ 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js index efdbd83..e97e530 100644 --- a/src/javascript/enhancements/notifications.js +++ b/src/javascript/enhancements/notifications.js @@ -6,7 +6,7 @@ export function init() { updateNotificationsInTitle(); }, ".*"); - core.runAfterPathnameChange(() => { + core.runAfterLocationChange(() => { updateNotificationsInTitle(); }, ".*"); } diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index 857db5b..1419678 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -3,7 +3,7 @@ import * as helper from './helpers'; /* SCRIPT LOGICS */ let __scripts = []; let __afterLoadScripts = []; -let __afterPathnameChangeScripts = []; +let __afterLocationChangeScripts = []; export function initCore() { let observer = new MutationObserver(mutations => { @@ -20,16 +20,13 @@ export function initCore() { attributes: true }); - // patchBrowser(); - // window.addEventListener('locationchange', (event) => handleLocationChanged(event)); - runAfterLoad(() => { let loadingBar = document.getElementById('enable-ani-cm'); let loadingBarObserver = new MutationObserver(mutations => { mutations.forEach(mutation => { // enable-ani-cm node changes from display:none to display:block after loading if (mutation.oldValue.includes('display: none')) { - __afterPathnameChangeScripts.forEach(script => { + __afterLocationChangeScripts.forEach(script => { if (window.location.pathname.match(script.pattern)) { script.function(); } @@ -100,45 +97,8 @@ function awaitPageLoaded() { } /* PATHNAME LOGIC */ -export function runAfterPathnameChange(func, pattern = '.*') { - __afterPathnameChangeScripts.push({ "function": func, "pattern": pattern }); -} - -function handleLocationChanged(event) { - __afterPathnameChangeScripts.forEach(script => { - if (window.location.pathname.match(script.pattern)) { - script.function(); - } - }); -} - -function patchBrowser() { - // patches several browser functions to dispatch a "locationchange" event - // as an extension is not allowed to override these functions we have to inject this as a script tag into the head - let scriptContent = `history.pushState = (func => function pushState() { - let result = func.apply(this, arguments); - window.dispatchEvent(new Event('pushstate')); - window.dispatchEvent(new Event('locationchange')); - - return result; - })(history.pushState); - - history.replaceState = (func => function replaceState() { - let result = func.apply(this, arguments); - window.dispatchEvent(new Event('replacestate')); - window.dispatchEvent(new Event('locationchange')); - return result; - })(history.replaceState); - - window.addEventListener('popstate', () => { - window.dispatchEvent(new Event('locationchange')) - });` - - let head = document.getElementsByTagName("head")[0]; - let newScript = document.createElement('script'); - newScript.type = 'text/javascript'; - newScript.innerHTML = scriptContent; - head.appendChild(newScript); +export function runAfterLocationChange(func, pattern = '.*') { + __afterLocationChangeScripts.push({ "function": func, "pattern": pattern }); } /* LOGIN LOGIC */ -- 2.43.4 From f0eebd28037a3a4e48bd80c2999f19113cf33b34 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Sun, 25 Oct 2020 11:21:48 +0100 Subject: [PATCH 19/21] #19 Fallback for badly loaded pages added --- src/javascript/enhancements/notifications.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js index e97e530..f58ec56 100644 --- a/src/javascript/enhancements/notifications.js +++ b/src/javascript/enhancements/notifications.js @@ -13,7 +13,22 @@ export function init() { function getNotificationCount() { if (core.isLoggedIn()) { - let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; + let menus = document.getElementById('materialize-menu-dropdown').innerText.split('\n'); + + // On some pages there is an issue if the website is loaded using them as entry point (eg. /Search). + // They don´t have their menu build completly directly and the above code does not return the "User" element :/ + // If this happens the menu is splitted into many more (~20) items than only 5. + // So if there are more, we just try again later. + // Additionally, if the element is there correctly it´s not updated with the notification count directly, so we wait a bit longer before retrying. + if (menus.length > 5) { + setTimeout(() => { + updateNotificationsInTitle(); + }, 2000); + + return undefined; + } + + let menuUserText = menus[4]; let notificationCount = menuUserText.match(/\d+/)?.[0] ?? 0; return notificationCount; } else { -- 2.43.4 From f3fe4fc717b02bc0aff4283062ce9a4dbebbfe30 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Sun, 25 Oct 2020 11:26:33 +0100 Subject: [PATCH 20/21] #19 changed the behaviour when we start our scripts --- src/javascript/utils/aniwatchCore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index 1419678..db6c599 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -88,7 +88,7 @@ function awaitPageLoaded() { } let loop = setInterval(() => { - if (preLoader.style.display === "none") { + if (preLoader.style.display === "none" && document.readyState === 'complete') { clearInterval(loop); runScripts(); -- 2.43.4 From ac33d37a53497542ef6080c7b8bef20db88a5ca3 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Sun, 25 Oct 2020 11:26:49 +0100 Subject: [PATCH 21/21] Revert "#19 Fallback for badly loaded pages added" This reverts commit f0eebd28037a3a4e48bd80c2999f19113cf33b34. --- src/javascript/enhancements/notifications.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js index f58ec56..e97e530 100644 --- a/src/javascript/enhancements/notifications.js +++ b/src/javascript/enhancements/notifications.js @@ -13,22 +13,7 @@ export function init() { function getNotificationCount() { if (core.isLoggedIn()) { - let menus = document.getElementById('materialize-menu-dropdown').innerText.split('\n'); - - // On some pages there is an issue if the website is loaded using them as entry point (eg. /Search). - // They don´t have their menu build completly directly and the above code does not return the "User" element :/ - // If this happens the menu is splitted into many more (~20) items than only 5. - // So if there are more, we just try again later. - // Additionally, if the element is there correctly it´s not updated with the notification count directly, so we wait a bit longer before retrying. - if (menus.length > 5) { - setTimeout(() => { - updateNotificationsInTitle(); - }, 2000); - - return undefined; - } - - let menuUserText = menus[4]; + let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; let notificationCount = menuUserText.match(/\d+/)?.[0] ?? 0; return notificationCount; } else { -- 2.43.4