-
Notifications
You must be signed in to change notification settings - Fork 5
/
loader.js
103 lines (76 loc) · 1.82 KB
/
loader.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
// @ts-check
/**
* async script loader
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
LIB.ready(function () {
function runScripts() {
const scripts = document.querySelectorAll('script[type="text/script"],script[type="text/module"]');
const j = scripts.length;
let i = 0;
for (; i < j; i++) {
setTimeout(
(function (oldScript, script) {
return function () {
const parent = oldScript.parentElement;
script.text = oldScript.text;
if (oldScript.type == 'text/module') {
script.type = 'module';
}
try {
parent.insertBefore(script, oldScript);
parent.removeChild(oldScript);
} catch (e) {
console.error(e);
}
};
}(scripts[i], document.createElement("script"))),
0
);
}
const module = document.querySelector('template[data-type=module]');
if (module) {
setTimeout(function () {
module.parentElement.insertBefore(module.content, module);
module.remove();
}, 0)
}
}
const links = document.querySelectorAll(
'link[data-media]'
);
const j = links.length;
let i = 0, link;
for (; i < j; i++) {
link = links[i];
if (link.hasAttribute('data-media')) {
link.media = link.dataset.media;
link.removeAttribute("data-media");
} else {
link.removeAttribute("media");
}
}
const scripts = document.querySelectorAll(
'script[data-async]'
);
let count = scripts.length;
if (count === 0) {
runScripts();
}
else {
for (i = 0; i < count; i++) {
scripts[i].addEventListener('load', function () {
count--;
if (count == 0) {
runScripts()
}
})
}
}
});