-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
26 lines (23 loc) · 952 Bytes
/
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
// Setup body and script objects
let body = document.getElementsByTagName('BODY')[0];
let script = document.createElement('script');
script.type = 'text/javascript';
// Push loading spinner to screen
const entryDiv = document.currentScript.getAttribute('entry');
body.innerHTML +=
'<div style="position:absolute; z-index:100; top:50%; left: 50%; transform: translate(-50%, -50%);">' +
'<img src="' + document.currentScript.getAttribute('spinner') + '" alt="Loading">' +
'</div>';
// Create new script request
let xhr = new XMLHttpRequest(), method = "GET", url = document.currentScript.getAttribute('bundle');
xhr.responseType = 'blob';
// Load the script
xhr.open(method, url, true);
xhr.onload = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
script.src = URL.createObjectURL(xhr.response);
body.innerHTML = (entryDiv ? `<div id="${entryDiv}"></div>` : "");
body.appendChild(script);
}
};
xhr.send();