From b1aef75409b3d538f4ab9255e860724bd54d9439 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 11 Sep 2020 17:57:17 +0200 Subject: [PATCH] #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