diff --git a/main.js b/main.js index b6622d6..0f057fa 100644 --- a/main.js +++ b/main.js @@ -358,11 +358,7 @@ function pushitem(key, value) pending[key] = true; var name = metadata[key] ? metadata[key].title : key; - encryptstring(value) - .then(encrypted => - { - return queryremote({action: "push", name: key, data: encrypted}); - }) + queryremote({action: "push", name: key, data: value}) .then( () => { console.log("'" + name + "' pushed to server"); @@ -1448,53 +1444,74 @@ function init() }); } +function encryptdata(params) +{ + if (params.data) + { + return encryptstring(params.data) + .then(encrypted => + { + params.data = encrypted; + return Promise.resolve(params); + }); + } + else + { + return Promise.resolve(params); + } +} + function queryremote(params) { - return new Promise( (apply, failed) => { - - params.password = settings.password; - - var paramlist = []; - for (var i in params) + return encryptdata(params) + .then(encparams => + { + return new Promise ( (resolve, reject) => { - paramlist.push(i + "=" + encodeURIComponent(params[i])); - } + encparams.password = settings.password; - var xhr = new XMLHttpRequest(); - xhr.open("POST", "handler.php"); - xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - - xhr.onerror = function() - { - failed("XMLHttpRequest error"); - } - - xhr.onload = function() - { - if (xhr.status !== 200) + var paramlist = []; + for (var i in encparams) { - failed("Http status " + xhr.status); + paramlist.push(i + "=" + encodeURIComponent(encparams[i])); } - else + + var xhr = new XMLHttpRequest(); + xhr.open("POST", "handler.php"); + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + + xhr.onerror = function() { - decryptstring(xhr.responseText) - .then(decrypted => + return reject("XMLHttpRequest error"); + } + + xhr.onload = function() + { + if (xhr.status !== 200) { - if (decrypted.startsWith("error: ")) + return reject("Http status " + xhr.status); + } + else + { + decryptstring(xhr.responseText) + .then(decrypted => { - failed(decrypted); - } - else - { - apply(decrypted); - } - }); + if (decrypted.startsWith("error: ")) + { + return reject(decrypted); + } + else + { + return resolve(decrypted); + } + }); + } } - } - var paramstring = paramlist.join("&"); - console.log("http request length: " + formatsize(paramstring.length)); - xhr.send(paramstring); + var paramstring = paramlist.join("&"); + console.log("http request length: " + formatsize(paramstring.length)); + xhr.send(paramstring); + }); }); }