-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
167 lines (144 loc) · 4.21 KB
/
utils.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Javascript Utilities
Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's
permission.
*/
"use strict";
/* Load a binary data. cb(data, len) is called with data = null and
* len = -1 in case of error. Otherwise len is the length in
* bytes. data can be a string, Array or Uint8Array depending on
* the implementation. */
function load_binary(url, callback) {
var req, typed_array, is_ie;
req = new XMLHttpRequest();
req.open('GET', url, true);
/* completion function */
req.onreadystatechange = function () {
var data, len, buf;
if (req.readyState !== 4) {
return;
}
if (req.status != 200 && req.status != 0) {
callback(null, -1);
return;
}
if (is_ie) {
data = new VBArray(req.responseBody).toArray();
len = data.length;
callback(data, len);
return;
}
if (typed_array && 'mozResponse' in req) {
/* firefox 6 beta */
data = req.mozResponse;
} else if (typed_array && req.mozResponseArrayBuffer) {
/* Firefox 4 */
data = req.mozResponseArrayBuffer;
} else if ('responseType' in req) {
/* Note: in Android 3.0 there is no typed arrays so its
returns UTF8 text */
data = req.response;
} else {
data = req.responseText;
typed_array = false;
}
if (data === null || data === undefined) {
//TODO: report error to GUI (!)
callback(null, -1);
return;
}
if (typed_array) {
len = data.byteLength;
buf = new Uint8Array(data, 0, len);
callback(buf, len);
} else {
len = data.length;
callback(data, len);
}
};
is_ie = (typeof ActiveXObject == "function");
if (!is_ie) {
typed_array = ((typeof ArrayBuffer != 'undefined') && (typeof Uint8Array != 'undefined'));
if (typed_array && 'mozResponseType' in req) {
/* firefox 6 beta */
req.mozResponseType = 'arraybuffer';
} else if (typed_array && 'responseType' in req) {
/* Chrome */
req.responseType = 'arraybuffer';
} else {
req.overrideMimeType('text/plain; charset=x-user-defined');
typed_array = false;
}
}
req.send(null);
}
/*
jQuery.ajax({
//context: me,
dataType: "script",
cache: true,
url: url,
async: false
});
*/
function loadScript(url, success_callback, error_callback) {
var script = document.createElement('script');
/*
var isLoaded = false;
script.onreadystatechange = function () {
if ((script.readyState == 'complete' || script.readyState == 'loaded') && !isLoaded) {
if (callback)
callback();
}
};
*/
// TODO: make this cross-browser
if (success_callback) {
script.onload = function () {
success_callback();
};
}
if (error_callback) {
script.onerror = function () {
error_callback();
};
}
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
var parent = document.body; //document.getElementsByTagName('head').item(0) || document.documentElement;
parent.appendChild(script);
}
function loadScripts(scripts, success_callback, error_callback) {
var counter = scripts.length;
var i;
var was_error = false;
if (counter == 0) {
success_callback();
return;
}
var operation_complete = function () {
if (was_error)
error_callback();
else
success_callback();
};
var success = function () {
if (--counter <= 0) {
operation_complete();
}
};
var error = function () {
was_error = true;
if (--counter <= 0) {
operation_complete();
}
};
for (i = counter - 1; i >= 0; i--) {
// load all scripts asynchronously
loadScript(scripts[i], success, error);
}
}
self.load_binary = load_binary;
self.loadScript = loadScript;
self.loadScripts = loadScripts;