MIDI.js/js/Window/DOMLoader.XMLHttp.js
2012-11-06 13:06:08 -08:00

130 lines
3.8 KiB
JavaScript

/*
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;
};
}