Total Complexity | 9 |
Complexity/F | 3 |
Lines of Code | 38 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { getGlobalConfiguration, SETTINGS_websiteShowNotificationsCountInTab } from '../configuration/configuration'; |
||
2 | import * as core from '../utils/aniwatchCore'; |
||
3 | import * as helper from '../utils/helpers'; |
||
4 | |||
5 | export function init(): void { |
||
6 | getGlobalConfiguration().getProperty(SETTINGS_websiteShowNotificationsCountInTab, value => { |
||
7 | if (value) { |
||
8 | core.runAfterLoad(() => { |
||
9 | updateNotificationsInTitle(); |
||
10 | }, ".*"); |
||
11 | |||
12 | core.runAfterLocationChange(() => { |
||
13 | updateNotificationsInTitle(); |
||
14 | }, ".*"); |
||
15 | } |
||
16 | }); |
||
17 | } |
||
18 | |||
19 | function getNotificationCount(): number { |
||
20 | if (core.isLoggedIn()) { |
||
21 | let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4]; |
||
22 | let notificationCount = parseInt(menuUserText.match(/\d+/)?.[0]) ?? 0; |
||
23 | return notificationCount; |
||
24 | } else { |
||
25 | return 0; |
||
26 | } |
||
27 | } |
||
28 | |||
29 | function updateNotificationsInTitle(): void { |
||
30 | let count = getNotificationCount(); |
||
31 | |||
32 | if (helper.assigned(count) && count > 0) { |
||
33 | // document.title is updated after the event is triggered, so we delay our title update by a reasonable time |
||
34 | setTimeout(() => { |
||
35 | document.title = `(${count}) ${document.title}`; |
||
36 | }, 100); |
||
37 | } |
||
38 | } |