rename and refactor conflicting 'load' function

This commit is contained in:
Myk Melez 2014-12-18 16:56:15 -08:00
Родитель e3aeb936ac
Коммит bd0ff96f8a
2 изменённых файлов: 33 добавлений и 33 удалений

Просмотреть файл

@ -466,39 +466,8 @@ DumbPipe.registerOpener("notification", function(message, sender) {
}
});
function load(file, responseType, successCb, failureCb, progressCb, length) {
var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.open("GET", file, true);
xhr.responseType = responseType;
if (progressCb) {
xhr.onprogress = function(e) {
if (e.lengthComputable) {
progressCb(e.loaded / e.total * 100);
} else if (length) {
progressCb(e.loaded / length * 100);
}
}
}
xhr.onload = function() {
if (xhr.status === 200) {
successCb(xhr.response);
} else {
failureCb();
}
};
xhr.onerror = function() {
failureCb();
};
xhr.send(null);
}
DumbPipe.registerOpener("JARDownloader", function(message, sender) {
load(urlParams.downloadJAD, "text", function(data) {
loadWithProgress(urlParams.downloadJAD, "text", function(data) {
try {
var manifest = {};
@ -522,7 +491,7 @@ DumbPipe.registerOpener("JARDownloader", function(message, sender) {
jarURL = urlParams.downloadJAD.substring(0, urlParams.downloadJAD.lastIndexOf("/") + 1) + jarName;
}
load(jarURL, "arraybuffer", function(data) {
loadWithProgress(jarURL, "arraybuffer", function(data) {
sender({ type: "done", data: data });
}, function() {
sender({ type: "fail" });

Просмотреть файл

@ -18,6 +18,37 @@ function load(file, responseType) {
});
}
function loadWithProgress(file, responseType, successCb, failureCb, progressCb, length) {
var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.open("GET", file, true);
xhr.responseType = responseType;
if (progressCb) {
xhr.onprogress = function(e) {
if (e.lengthComputable) {
progressCb(e.loaded / e.total * 100);
} else if (length) {
progressCb(e.loaded / length * 100);
}
}
}
xhr.onload = function() {
if (xhr.status === 200) {
successCb(xhr.response);
} else {
failureCb();
}
};
xhr.onerror = function(event) {
failureCb();
};
xhr.send(null);
}
function loadScript(path) {
return new Promise(function(resolve, reject) {
var element = document.createElement('script');