This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlhttprequest.js
74 lines (73 loc) · 3.11 KB
/
xmlhttprequest.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
/**
* Make an XMLHttpRequest for JSON data.
* @param address Address to request from
* @param callback function with (data, error) parameters to call after the request.
*/
function request(address, callback) {
setTimeout(function () {
var xhttp = new XMLHttpRequest();
xhttp.responseType = 'arraybuffer';
xhttp.onload = function (e) {
if (this.status === 200) {
var bytes = new Uint8Array(this.response);
callback(bytes, null);
}
};
// xhttp.onreadystatechange = function () {
// if (this.readyState === 4) {
// try {
// if (this.status === 200 || (this.status === 0 && this.responseText)) {
// setTimeout(function () {
// callback(new Uint8Array(this.response), null)
// }, 0);
// } else if (this.status === 404 || this.status === 403 || this.status === 500) {
// callback(null, "HTTP " + this.status + " (See " + address + ")")
// } else if (this.status === 400) {
// callback(null, this.responseText + " (See " + address + ")")
// } else if (this.status === 0) {
// callback(null, "Request was blocked. (Adblocker maybe?)")
// }
// } catch (e) {
// callback(null, e.message + " (See " + address + ")")
// }
// }
// };
xhttp.timeout = 45000;
xhttp.ontimeout = function () {
callback(null, "Timed out after 45 seconds. (" + address + ")")
};
xhttp.open("GET", address, true);
xhttp.send();
}, 0);
}
function requestJSON(address, callback) {
setTimeout(function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4) {
try {
if (this.status === 200 || (this.status === 0 && this.responseText)) {
var json = JSON.parse(this.responseText);
setTimeout(function () {
callback(json, null)
}, 0);
} else if (this.status === 404 || this.status === 403 || this.status === 500) {
callback(null, "HTTP " + this.status + " (See " + address + ")")
} else if (this.status === 400) {
callback(null, this.responseText + " (See " + address + ")")
} else if (this.status === 0) {
callback(null, "Request was blocked. (Adblocker maybe?)")
}
} catch (e) {
callback(null, e.message + " (See " + address + ")")
}
}
};
xhttp.timeout = 45000;
xhttp.ontimeout = function () {
callback(null, "Timed out after 45 seconds. (" + address + ")")
};
xhttp.open("GET", address, true);
xhttp.send();
}, 0);
}