diff --git a/src/javascript/enhancements/notifications.js b/src/javascript/enhancements/notifications.js new file mode 100644 index 0000000..e97e530 --- /dev/null +++ b/src/javascript/enhancements/notifications.js @@ -0,0 +1,33 @@ +import * as core from '../utils/aniwatchCore'; +import * as helper from '../utils/helpers'; + +export function init() { + core.runAfterLoad(() => { + updateNotificationsInTitle(); + }, ".*"); + + core.runAfterLocationChange(() => { + updateNotificationsInTitle(); + }, ".*"); +} + +function getNotificationCount() { + if (core.isLoggedIn()) { + let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; + let notificationCount = menuUserText.match(/\d+/)?.[0] ?? 0; + return notificationCount; + } else { + return 0; + } +} + +function updateNotificationsInTitle() { + let count = getNotificationCount(); + + if (helper.assigned(count) && count > 0) { + // 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 diff --git a/src/javascript/index.js b/src/javascript/index.js index 9d86b8a..408eba4 100644 --- a/src/javascript/index.js +++ b/src/javascript/index.js @@ -5,6 +5,7 @@ import { initHelpers } from './utils/helpers'; // enhancements import { init as animeRequests } from './enhancements/animeRequests'; import { init as languageDisplay } from './enhancements/languageDisplay'; +import { init as notifications } from './enhancements/notifications'; import { init as quickSearch } from './enhancements/quickSearch'; // core @@ -16,4 +17,5 @@ initHelpers(); // enhancements animeRequests(); languageDisplay(); +notifications(); quickSearch(); \ No newline at end of file diff --git a/src/javascript/utils/aniwatchCore.js b/src/javascript/utils/aniwatchCore.js index abe5afa..db6c599 100644 --- a/src/javascript/utils/aniwatchCore.js +++ b/src/javascript/utils/aniwatchCore.js @@ -1,7 +1,9 @@ import * as helper from './helpers'; +/* SCRIPT LOGICS */ let __scripts = []; let __afterLoadScripts = []; +let __afterLocationChangeScripts = []; export function initCore() { let observer = new MutationObserver(mutations => { @@ -18,6 +20,29 @@ export function initCore() { attributes: true }); + 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')) { + __afterLocationChangeScripts.forEach(script => { + if (window.location.pathname.match(script.pattern)) { + script.function(); + } + }); + } + }) + }); + + loadingBarObserver.observe(loadingBar, { + attributes: true, + attributeOldValue: true, + attributeFilter: ['style'], + }); + + }, '.*') + helper.onReady(() => awaitPageLoaded()); } @@ -63,10 +88,30 @@ function awaitPageLoaded() { } let loop = setInterval(() => { - if (preLoader.style.display === "none") { + if (preLoader.style.display === "none" && document.readyState === 'complete') { clearInterval(loop); runScripts(); } }, 100); +} + +/* PATHNAME LOGIC */ +export function runAfterLocationChange(func, pattern = '.*') { + __afterLocationChangeScripts.push({ "function": func, "pattern": pattern }); +} + +/* LOGIN LOGIC */ +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; + } + }); + + return result; } \ No newline at end of file diff --git a/src/javascript/utils/helpers.js b/src/javascript/utils/helpers.js index a52968e..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); } diff --git a/src/stylesheets/aniwatchplus.scss b/src/stylesheets/aniwatchplus.scss index ab8fc0e..67a2367 100644 --- a/src/stylesheets/aniwatchplus.scss +++ b/src/stylesheets/aniwatchplus.scss @@ -3,4 +3,5 @@ // enhancements @import './enhancements/lists'; -@import './enhancements/tabs'; \ No newline at end of file +@import './enhancements/tabs'; +@import './enhancements/watch2gether'; \ No newline at end of file diff --git a/src/stylesheets/enhancements/_watch2gether.scss b/src/stylesheets/enhancements/_watch2gether.scss new file mode 100644 index 0000000..a260283 --- /dev/null +++ b/src/stylesheets/enhancements/_watch2gether.scss @@ -0,0 +1,6 @@ +.material-icons { + // Fix icon position for room creator + &[aria-label="Room creator"] { + vertical-align: text-top; + } +}