From b195dfc68725bc928da92543ffdf2bf509256003 Mon Sep 17 00:00:00 2001 From: Robbie Langer Date: Fri, 8 Jun 2018 18:30:49 -0400 Subject: rearmed xmlhttprequest with promises --- pkg93.js | 152 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 79 insertions(+), 73 deletions(-) diff --git a/pkg93.js b/pkg93.js index 3b7d3b0..55a377c 100644 --- a/pkg93.js +++ b/pkg93.js @@ -69,27 +69,31 @@ var pkg93 = { xhr.onprogress = e => { bardiv.innerHTML = _abarpkg93uses(60, e.loaded / e.total); }; - xhr.onerror = () => { - cli.log("ERR Fatal error while retriving package.json."); - }; - xhr.onload = () => { - var json = JSON.parse(xhr.responseText); - cli.log("NAME " + json.name); - cli.log("MSG " + json.msg); - for (let item of json.packages) { - try { - cli.log("OK " + item + "@" + source); - config.pkglist.push(item + "@" + source); - } catch (err) { - cli.log("ERR " + err.message); + return new Promise((res, rej) => { + xhr.onerror = () => { + cli.log("ERR Fatal error while retriving package.json."); + rej() + }; + xhr.onload = () => { + var json = JSON.parse(xhr.responseText); + cli.log("NAME " + json.name); + cli.log("MSG " + json.msg); + for (let item of json.packages) { + try { + cli.log("OK " + item + "@" + source); + config.pkglist.push(item + "@" + source); + } catch (err) { + cli.log("ERR " + err.message); + } } - } - }; - xhr.send(); + res() + }; + xhr.send(); + }) } localStorage[".pkg93/config.json"] = JSON.stringify(config); }, - get: async function(pkg, cli, callback) { + get: async function(pkg, cli) { cli = cli || {log: (i) => {$log(i);}}; var config = pkg93.getConfig(); cli.log("SRCH " + pkg); @@ -115,66 +119,68 @@ var pkg93 = { // fail silently } }; - xhr.onerror = () => { - cli.log("ERR Fatal error while retriving package.json."); - callback(false); - }; - xhr.onload = () => { - try { - cli.log("DONE " + pkgsource + "/" + pkgname + "/package.json"); - var json = JSON.parse(xhr.responseText); - localStorage[".pkg93/packages/" + pkgname + ".json"] = JSON.stringify(json); - cli.log("GET " + pkgsource + "/" + pkgname + "/" + json.inject); - var bardiv2 = cli.log(_abarpkg93uses(60, 0)); - var xhr2 = new XMLHttpRequest(); - xhr2.open("GET", pkgsource + "/" + pkgname + "/" + json.inject); - xhr2.setRequestHeader("X-Requested-With", "pkg93"); - xhr2.onprogress = e => { - try { - bardiv2.innerHTML = _abarpkg93uses(60, e.loaded / e.total); - } catch (err) { - // fail silently - } - }; - xhr2.onerror = () => { - cli.log("ERR Fatal error while retriving " + pkgsource + "/" + pkgname + "/" + json.inject); - callback(false); - }; - xhr2.onload = async () => { - try { - cli.log("DONE " + pkgsource + "/" + pkgname + "/" + json.inject); - var script = xhr2.responseText; - localStorage[".pkg93/packages/" + pkgname + ".js"] = script; - eval(script); - if (json.uninstall) { - // no xhr this time - var uninst = await (await (fetch(pkgsource + "/" + pkgname + "/" + json.uninstall))).text(); - localStorage[".pkg93/packages/" + pkgname + ".rm.js"] = uninst; + return new Promise((res, rej) => { + xhr.onerror = () => { + cli.log("ERR Fatal error while retriving package.json."); + rej(); + }; + xhr.onload = () => { + try { + cli.log("DONE " + pkgsource + "/" + pkgname + "/package.json"); + var json = JSON.parse(xhr.responseText); + localStorage[".pkg93/packages/" + pkgname + ".json"] = JSON.stringify(json); + cli.log("GET " + pkgsource + "/" + pkgname + "/" + json.inject); + var bardiv2 = cli.log(_abarpkg93uses(60, 0)); + var xhr2 = new XMLHttpRequest(); + xhr2.open("GET", pkgsource + "/" + pkgname + "/" + json.inject); + xhr2.setRequestHeader("X-Requested-With", "pkg93"); + xhr2.onprogress = e => { + try { + bardiv2.innerHTML = _abarpkg93uses(60, e.loaded / e.total); + } catch (err) { + // fail silently } - cli.log("OK Injected package!"); - if (!config.installed.includes(pkgname)) { - config.installed.push(pkgname); + }; + xhr2.onerror = () => { + cli.log("ERR Fatal error while retriving " + pkgsource + "/" + pkgname + "/" + json.inject); + rej(); + }; + xhr2.onload = async () => { + try { + cli.log("DONE " + pkgsource + "/" + pkgname + "/" + json.inject); + var script = xhr2.responseText; + localStorage[".pkg93/packages/" + pkgname + ".js"] = script; + eval(script); + if (json.uninstall) { + // no xhr this time + var uninst = await (await (fetch(pkgsource + "/" + pkgname + "/" + json.uninstall))).text(); + localStorage[".pkg93/packages/" + pkgname + ".rm.js"] = uninst; + } + cli.log("OK Injected package!"); + if (!config.installed.includes(pkgname)) { + config.installed.push(pkgname); + } + localStorage[".pkg93/config.json"] = JSON.stringify(config); + res(); + // can i go home now + } catch (err) { + cli.log("ERR " + err.message); + rej(err); } - localStorage[".pkg93/config.json"] = JSON.stringify(config); - callback(true); - // can i go home now - } catch (err) { - cli.log("ERR " + err.message); - callback(false, err); - } - }; - xhr2.send(); + }; + xhr2.send(); + } catch (err) { + cli.log("ERR " + err.message); + rej(err); + } + }; + try { + xhr.send(); } catch (err) { cli.log("ERR " + err.message); - callback(false, err); + rej(err); } - }; - try { - xhr.send(); - } catch (err) { - cli.log("ERR " + err.message); - callback(false, err); - } + }) } }, rm: function(pkg, cli) { @@ -286,7 +292,7 @@ If you find my software useful, consider donating ERR You're trying to modify a pre-installed Windows93 app.\n Don't do that!"); } else { - await pkg93.get(args[1], cli, function() {}); + await pkg93.get(args[1], cli); } } else if (args[0] == "rm") { if (args.length < 2) { -- cgit v1.2.3