2011-07-17 23:09:13 +04:00
|
|
|
/**
|
|
|
|
* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
*/
|
2008-11-05 00:49:04 +03:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
function onload(event) {
|
|
|
|
if (event.target != xhr) {
|
|
|
|
throw "onload event.target != xhr";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.target.status != 200) {
|
|
|
|
var message = { type: "error",
|
|
|
|
error: event.target.status };
|
2011-11-11 04:29:34 +04:00
|
|
|
postMessage(message);
|
2008-11-05 00:49:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var message = { type: "load",
|
|
|
|
data: xhr.responseText };
|
2011-11-11 04:29:34 +04:00
|
|
|
postMessage(message);
|
2008-11-05 00:49:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
xhr.onload = onload;
|
|
|
|
xhr.addEventListener("load", onload, false);
|
|
|
|
xhr.removeEventListener("load", onload, false);
|
|
|
|
if (!xhr.onload) {
|
|
|
|
var message = { type: "error",
|
|
|
|
error: "Lost message listener!" };
|
2011-11-11 04:29:34 +04:00
|
|
|
postMessage(message);
|
2008-11-05 00:49:04 +03:00
|
|
|
}
|
|
|
|
|
2013-12-21 04:22:13 +04:00
|
|
|
xhr.onerror = function(event) {
|
2008-11-05 00:49:04 +03:00
|
|
|
if (event.target != xhr) {
|
|
|
|
throw "onerror event.target != xhr";
|
|
|
|
}
|
|
|
|
var message = { type: "error",
|
|
|
|
error: event.target.status };
|
2011-11-11 04:29:34 +04:00
|
|
|
postMessage(message);
|
2013-12-21 04:22:13 +04:00
|
|
|
};
|
|
|
|
xhr.onerror = xhr.onerror;
|
|
|
|
if (!xhr.onerror || xhr.onerror != xhr.onerror) {
|
|
|
|
throw "onerror wasn't set properly";
|
|
|
|
}
|
2008-11-05 00:49:04 +03:00
|
|
|
|
|
|
|
function onprogress(event) {
|
|
|
|
if (event.target != xhr) {
|
|
|
|
throw "onprogress event.target != xhr";
|
|
|
|
}
|
|
|
|
var message = { type: "progress",
|
|
|
|
current: event.loaded,
|
|
|
|
total: event.total };
|
2011-11-11 04:29:34 +04:00
|
|
|
postMessage(message);
|
2008-11-05 00:49:04 +03:00
|
|
|
}
|
|
|
|
xhr.addEventListener("progress", onprogress, false);
|
|
|
|
|
|
|
|
xhr.addEventListener("foopety", function(event) {}, false);
|
|
|
|
xhr.removeEventListener("doopety", function(event) {}, false);
|
|
|
|
|
2011-03-24 16:22:03 +03:00
|
|
|
xhr.onloadend = function(event) {
|
|
|
|
var message = { type: "loadend" };
|
2011-11-11 04:29:34 +04:00
|
|
|
postMessage(message);
|
2011-03-24 16:22:03 +03:00
|
|
|
}
|
|
|
|
|
2008-11-05 00:49:04 +03:00
|
|
|
var upload = xhr.upload;
|
|
|
|
upload.onprogress = function(event) { };
|
2011-11-11 04:29:34 +04:00
|
|
|
upload.addEventListener("foo", function(event) { }, false);
|
|
|
|
upload.removeEventListener("foo", function(event) { }, false);
|
2008-11-05 00:49:04 +03:00
|
|
|
upload.addEventListener("load", function(event) { }, false);
|
2011-11-11 04:29:34 +04:00
|
|
|
upload.removeEventListener("foo", function(event) { }, false);
|
|
|
|
upload.onload = function(event) {
|
|
|
|
var message = { type: "upload.load" };
|
|
|
|
postMessage(message);
|
|
|
|
}
|
2008-11-05 00:49:04 +03:00
|
|
|
|
|
|
|
onmessage = function(event) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (xhr.DONE != 4 || XMLHttpRequest.DONE != 4) {
|
|
|
|
throw "xhr constants not correct!";
|
|
|
|
}
|
|
|
|
if (xhr.readystate > xhr.UNSENT) {
|
2008-11-05 00:49:04 +03:00
|
|
|
throw "XHR already running!";
|
|
|
|
}
|
2011-11-11 04:29:34 +04:00
|
|
|
xhr.open("POST", event.data);
|
|
|
|
xhr.send("Data to send");
|
2008-11-05 00:49:04 +03:00
|
|
|
}
|