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