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
|
|
|
} |