src/javascript/enhancements/notifications.ts   A
last analyzed

Complexity

Total Complexity 9
Complexity/F 3

Size

Lines of Code 38
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 9
mnd 6
bc 6
fnc 3
bpm 2
cpm 3
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A notifications.ts ➔ getNotificationCount 0 8 5
A notifications.ts ➔ updateNotificationsInTitle 0 9 2
A notifications.ts ➔ init 0 11 2
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
}