-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire_script.js
86 lines (78 loc) · 2.09 KB
/
require_script.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
(function() {
function requireScript(url, cb, timeout) {
httpRequest(url, 'GET', function(response, status) {
if (status === 200) {
// script -> Blob -> Object URL
var blob = new Blob([response], {type: 'text/javascript'});
var blob_url = URL.createObjectURL(blob);
var is_in_window = (typeof window === 'object');
if (is_in_window) {
// FIXME: Remove the script element when it is done loading
var script = document.createElement('script');
document.head.appendChild(script);
script.onload = function() {
console.info('Loaded script "' + url + '".');
URL.revokeObjectURL(blob_url);
if (cb) cb();
};
script.onerror = function() {
console.error('Loading script "' + url + '" failed.');
URL.revokeObjectURL(blob_url);
};
script.src = blob_url;
} else {
try {
importScripts(blob_url);
console.info('Loaded script "' + url + '".');
if (cb) cb();
} catch (err) {
console.error('Loading script "' + url + '" failed.');
}
}
} else if (status) {
console.error('Downloading script "' + url + '" failed with status code: ' + status);
} else {
console.error('Downloading script "' + url + '" failed.');
}
}, timeout);
}
function httpRequest(url, method, cb, timeout) {
timeout = timeout || 3000;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
/*
console.info(this.response);
console.info(this.status);
console.info(this.readyState);
*/
if (this.readyState === 4) {
if (cb) {
cb(this.response, this.status);
cb = null;
}
} else if (this.readyState === 0) {
if (cb) {
cb(null);
cb = null;
}
}
};
xhr.onerror = function() {
if (cb) {
cb(null);
cb = null;
}
};
xhr.open(method, url, true);
xhr.timeout = timeout;
xhr.send(null);
}
// Figure out if we are running in a Window or Web Worker
var scope = null;
if (typeof window === 'object') {
scope = window;
} else if (typeof importScripts === 'function') {
scope = self;
}
scope.requireScript = requireScript;
})();