forked from mudcube/MIDI.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOMLoader.XMLHttp.js
130 lines (122 loc) · 3.82 KB
/
DOMLoader.XMLHttp.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
/*
DOMLoader.XMLHttp
--------------------------
DOMLoader.sendRequest({
url: "./dir/something.extension",
data: "test!",
onerror: function(event) {
console.log(event);
},
onload: function(response) {
console.log(response.responseText);
},
onprogress: function (event) {
var percent = event.loaded / event.total * 100 >> 0;
loader.message("loading: " + percent + "%");
}
});
*/
if (typeof(DOMLoader) === "undefined") var DOMLoader = {};
// Add XMLHttpRequest when not available
if (typeof (XMLHttpRequest) === "undefined") {
var XMLHttpRequest;
(function () { // find equivalent for IE
var factories = [
function () {
return new ActiveXObject("Msxml2.XMLHTTP")
}, function () {
return new ActiveXObject("Msxml3.XMLHTTP")
}, function () {
return new ActiveXObject("Microsoft.XMLHTTP")
}];
for (var i = 0; i < factories.length; i++) {
try {
factories[i]();
} catch (e) {
continue;
}
break;
}
XMLHttpRequest = factories[i];
})();
}
if (typeof ((new XMLHttpRequest()).responseText) === "undefined") {
// http://stackoverflow.com/questions/1919972/how-do-i-access-xhr-responsebody-for-binary-data-from-javascript-in-ie
var IEBinaryToArray_ByteStr_Script =
"<!-- IEBinaryToArray_ByteStr -->\r\n"+
"<script type='text/vbscript'>\r\n"+
"Function IEBinaryToArray_ByteStr(Binary)\r\n"+
" IEBinaryToArray_ByteStr = CStr(Binary)\r\n"+
"End Function\r\n"+
"Function IEBinaryToArray_ByteStr_Last(Binary)\r\n"+
" Dim lastIndex\r\n"+
" lastIndex = LenB(Binary)\r\n"+
" if lastIndex mod 2 Then\r\n"+
" IEBinaryToArray_ByteStr_Last = Chr( AscB( MidB( Binary, lastIndex, 1 ) ) )\r\n"+
" Else\r\n"+
" IEBinaryToArray_ByteStr_Last = "+'""'+"\r\n"+
" End If\r\n"+
"End Function\r\n"+
"</script>\r\n";
// inject VBScript
document.write(IEBinaryToArray_ByteStr_Script);
DOMLoader.sendRequest = function(conf) {
// helper to convert from responseBody to a "responseText" like thing
function getResponseText(binary) {
var byteMapping = {};
for (var i = 0; i < 256; i++) {
for (var j = 0; j < 256; j++) {
byteMapping[String.fromCharCode(i + j * 256)] = String.fromCharCode(i) + String.fromCharCode(j);
}
}
// call into VBScript utility fns
var rawBytes = IEBinaryToArray_ByteStr(binary);
var lastChr = IEBinaryToArray_ByteStr_Last(binary);
return rawBytes.replace(/[\s\S]/g, function (match) {
return byteMapping[match];
}) + lastChr;
};
//
var req = XMLHttpRequest();
req.open("GET", conf.url, true);
if (conf.responseType) req.responseType = conf.responseType;
if (conf.onerror) req.onerror = conf.onerror;
if (conf.onprogress) req.onprogress = conf.onprogress;
req.onreadystatechange = function (event) {
if (req.readyState === 4) {
if (req.status === 200) {
req.responseText = getResponseText(req.responseBody);
} else {
req = false;
}
if (conf.onload) conf.onload(req);
}
};
req.setRequestHeader("Accept-Charset", "x-user-defined");
req.send(null);
return req;
}
} else {
DOMLoader.sendRequest = function(conf) {
var req = new XMLHttpRequest();
req.open(conf.data ? "POST" : "GET", conf.url, true);
if (req.overrideMimeType) req.overrideMimeType("text/plain; charset=x-user-defined");
if (conf.data) req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
if (conf.responseType) req.responseType = conf.responseType;
if (conf.onerror) req.onerror = conf.onerror;
if (conf.onprogress) req.onprogress = conf.onprogress;
req.onreadystatechange = function (event) {
if (req.readyState === 4) {
if (req.status !== 200 && req.status != 304) {
if (conf.onerror) conf.onerror(event, false);
return;
}
if (conf.onload) {
conf.onload(req);
}
}
};
req.send(conf.data);
return req;
};
}