-
Notifications
You must be signed in to change notification settings - Fork 8
/
utility.js
54 lines (51 loc) · 1.35 KB
/
utility.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
export async function loadAll(urls) {
return new Promise(resolve => {
let counter = urls.length;
let resHash = {};
urls.forEach(function (url) {
if (url.toLowerCase().match(/(\.png$)|(\.bmp$)|(\.jpg$)|(\.jpeg$)/)) {
let img = new Image();
img.onload = function () {
counter--;
resHash[url] = img;
if (counter === 0) {
resolve(resHash);
}
};
img.src = url;
} else {
let req = new XMLHttpRequest();
req.addEventListener("load", function (res) {
counter--;
resHash[url] = res.target.responseText;
if (counter === 0) {
resolve(resHash);
}
});
req.addEventListener("error", function () {
counter = -1;
});
req.open("GET", url, true);
req.send();
}
});
});
}
export async function getText(path) {
return new Promise(resolve => {
let req = new XMLHttpRequest();
req.addEventListener("load", function (res) {
resolve(res.target.responseText);
});
req.open("GET", path, true);
req.send();
});
}
export function uploadDataUrl(path, blob, callback) {
let req = new XMLHttpRequest();
req.addEventListener("load", function (res) {
callback.apply(null, [res]);
});
req.open("POST", path, true);
req.send(blob);
}