From bd0ff96f8aba67749ae00b1389dda2d46f8102fd Mon Sep 17 00:00:00 2001 From: Myk Melez Date: Thu, 18 Dec 2014 16:56:15 -0800 Subject: [PATCH] rename and refactor conflicting 'load' function --- index.js | 35 ++--------------------------------- libs/load.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index f14738f7..b013bf8d 100644 --- a/index.js +++ b/index.js @@ -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" }); diff --git a/libs/load.js b/libs/load.js index 11758036..50feb9a1 100644 --- a/libs/load.js +++ b/libs/load.js @@ -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');