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