src/javascript/utils/aniwatchCore.ts   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 2.13

Size

Lines of Code 130
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 94
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 17
mnd 9
bc 9
fnc 8
bpm 1.125
cpm 2.125
noi 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A aniwatchCore.ts ➔ registerScript 0 3 1
A aniwatchCore.ts ➔ initCore 0 40 4
A aniwatchCore.ts ➔ awaitPageLoaded 0 24 4
A aniwatchCore.ts ➔ findPreloader 0 3 1
A aniwatchCore.ts ➔ isLoggedIn 0 14 2
A aniwatchCore.ts ➔ runAfterLocationChange 0 4 1
A aniwatchCore.ts ➔ runScripts 0 5 2
A aniwatchCore.ts ➔ runAfterLoad 0 7 2
1
import * as helper from './helpers';
2
3
type ScriptCallback = () => void;
4
type NodeScriptCallback = (node: Node) => void;
5
6
type ScriptObj = {
7
    function: ScriptCallback,
8
    pattern: string
9
}
10
11
type NodeScriptObj = {
12
    function: NodeScriptCallback,
13
    pattern: string
14
}
15
16
/* SCRIPT LOGICS */
17
let __scripts: Array<NodeScriptObj> = [];
18
let __afterLoadScripts: Array<ScriptObj> = [];
19
let __afterLocationChangeScripts: Array<ScriptObj> = [];
20
21
export function initCore(): void {
22
    let observer = new MutationObserver(mutations => {
23
        mutations.forEach(mutation => {
24
            for (let i = 0; i < mutation.addedNodes.length; i++) {
25
                runScripts(mutation.addedNodes[i]);
26
            }
27
        });
28
    });
29
30
    observer.observe(document.documentElement || document.body, {
31
        childList: true,
32
        subtree: true,
33
        attributes: true
34
    });
35
36
    runAfterLoad(() => {
37
        let loadingBar = document.getElementById('enable-ani-cm');
38
        let loadingBarObserver = new MutationObserver(mutations => {
39
            mutations.forEach(mutation => {
40
                // enable-ani-cm node changes from display:none to display:block after loading
41
                if (mutation.oldValue.includes('display: none')) {
42
                    __afterLocationChangeScripts.forEach(script => {
43
                        if (window.location.pathname.match(script.pattern)) {
44
                            script.function();
45
                        }
46
                    });
47
                }
48
            })
49
        });
50
51
        loadingBarObserver.observe(loadingBar, {
52
            attributes: true,
53
            attributeOldValue: true,
54
            attributeFilter: ['style'],
55
        });
56
57
    }, '.*')
58
59
    helper.onReady(() => awaitPageLoaded());
60
}
61
62
export function registerScript(func: NodeScriptCallback, pattern: string = '.*'): void {
63
    __scripts.push({ function: func, pattern: pattern });
64
}
65
66
export function runScripts(node: Node): void {
67
    __scripts.forEach(script => {
68
        if (window.location.pathname.match(script.pattern)) {
69
            script.function(node);
70
        }
71
    });
72
}
73
74
function findPreloader(): HTMLElement {
75
    return document.getElementById('preloader');
76
}
77
78
export function runAfterLoad(func: ScriptCallback, pattern: string = '.*'): void {
79
    let preloader = findPreloader();
80
    if (typeof preloader !== undefined && preloader.style.display !== "none") {
81
        __afterLoadScripts.push({ function: func, pattern: pattern });
82
    } else {
83
        func();
84
    }
85
}
86
87
function awaitPageLoaded(): void {
88
    let preLoader = findPreloader();
89
90
    let runScripts = () => {
91
        __afterLoadScripts.forEach(script => {
92
            if (window.location.pathname.match(script.pattern)) {
93
                script.function();
94
            }
95
        });
96
    };
97
98
    if (!helper.assigned(preLoader)) {
99
        runScripts();
100
        return;
101
    }
102
103
    let loop = window.setInterval(() => {
104
        if (preLoader.style.display === "none" && document.readyState === 'complete') {
105
            window.clearInterval(loop);
106
107
            runScripts();
108
        }
109
    }, 100);
110
}
111
112
/* PATHNAME LOGIC */
113
export function runAfterLocationChange(func: ScriptCallback, pattern: string = '.*'): void {
114
    __afterLocationChangeScripts.push({ function: func, pattern: pattern });
115
}
116
117
/* LOGIN LOGIC */
118
export function isLoggedIn(): boolean {
119
    let menu = document.getElementById('materialize-menu-dropdown');
120
    let result = true;
121
122
    menu.innerText.split('\n').forEach(item => {
123
        if (item === 'Login') {
124
            result = false;
125
            return;
126
        }
127
    });
128
129
    return result;
130
}