-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptloader.js
110 lines (107 loc) · 3.29 KB
/
scriptloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018-2019 Campbell Crowley. All rights reserved.
// Author: Campbell Crowley ([email protected])
/**
* Loads page's necessary scripts after page has loaded to help with
* initial load times, and possibly to later allow for better caching control.
* @module ScriptLoader/
*/
(function() {
window.onload = preinit;
/**
* Additional scripts to fetch after page has loaded.
*
* @default
* @constant
* @private
* @type {Array.<{url: string, init: ?Array.<string>}>}
*/
const additionalScripts = [
{
url: 'https://dev.campbellcrowley.com/trax/traxCommon.js',
init: ['TraX', 'Common'],
},
{
url: 'https://dev.campbellcrowley.com/trax/pagecontroller.js',
init: ['TraX'],
},
{
url: 'https://dev.campbellcrowley.com/trax/canvascontroller.js',
init: ['TraX', 'Canvases'],
},
{url: 'https://dev.campbellcrowley.com/trax/constants.js'},
{url: 'https://dev.campbellcrowley.com/trax/keepAwake.js'},
{url: 'https://dev.campbellcrowley.com/trax/lz-string.min.js'},
{
url: 'https://dev.campbellcrowley.com/trax/videocontroller.js',
init: ['TraX', 'Video'],
},
{url: 'https://dev.campbellcrowley.com/socket.io.js'},
{
url: 'https://dev.campbellcrowley.com/trax/sidebarcontroller.js',
init: ['Sidebar'],
},
{url: 'https://c6.patreon.com/becomePatronButton.bundle.js', nowait: true},
];
let numInit = 0;
/**
* Fetch additional necessary scripts.
*
* @private
*/
function preinit() {
const head = document.getElementsByTagName('head')[0];
for (let i = 0; i < additionalScripts.length; i++) {
const newScript = document.createElement('script');
newScript.src = additionalScripts[i].url;
newScript.type = 'text/javascript';
if (additionalScripts[i].nowait) {
numInit++;
} else {
newScript.onload = handleScriptLoad;
}
head.appendChild(newScript);
}
}
/**
* A script loaded.
*
* @private
*/
function handleScriptLoad() {
numInit++;
if (numInit < additionalScripts.length) return;
console.log('INIT: begin');
for (let i = 0; i < additionalScripts.length; i++) {
if (typeof additionalScripts[i].init !== 'object') continue;
console.log('INIT:', additionalScripts[i].init);
try {
getScriptRef(window, additionalScripts[i].init, 0).init();
} catch (e) {
console.error('Failed to init', additionalScripts[i].init, e);
if (TraX && TraX.showMessageBox) {
TraX.showMessageBox(
'Failed to load scripts! Only data collection may work!', 30000,
true);
}
}
}
console.log('INIT: complete');
}
/**
* Gets a reference to the requested script object via nemspace names.
*
* @private
* @param {Object} win The parent obeject to start looking in. Usually window.
* @param {Array.<string>} namespaces Array of namespace names to recurse
* through.
* @param {number} index The array index to start looking at.
* @return {Object} Reference to the requested object.
*/
function getScriptRef(win, namespaces, index) {
if (index < namespaces.length) {
return getScriptRef(win[namespaces[index]], namespaces, ++index);
} else {
return win;
}
}
})();