-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.js
50 lines (46 loc) · 1.09 KB
/
util.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
export function FetchImage(url)
{
return new Promise((resolve, reject) =>
{
const img = document.createElement("img");
img.onload = (() => resolve(img));
img.onerror = reject;
img.src = url;
});
}
export function FetchDocument(url)
{
// Load the URL as a document using XHR (since fetch() doesn't do document type yet).
return new Promise((resolve, reject) =>
{
const xhr = new XMLHttpRequest();
xhr.onload = (() =>
{
if (xhr.status >= 200 && xhr.status < 300)
{
resolve(xhr.response);
}
else
{
reject(new Error("Failed to fetch '" + url + "': " + xhr.status + " " + xhr.statusText));
}
});
xhr.onerror = reject;
xhr.open("GET", url);
xhr.responseType = "document";
xhr.send();
});
}
export function AddStylesheet(url)
{
// Simply promisify adding a <link rel="stylesheet"> element to the main document <head> tag.
return new Promise((resolve, reject) =>
{
const link = document.createElement("link");
link.rel = "stylesheet";
link.onload = (() => resolve(link));
link.onerror = reject;
link.href = url;
document.head.appendChild(link);
});
}