Try new JSON settings format

pull/981/head
Andrew Gregory 2021-12-01 22:15:55 +08:00 committed by GitHub
parent 60a8c487ad
commit d4ddf22399
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 12 deletions

View File

@ -35,8 +35,9 @@ const otpAuthUrl = 'otpauth://';
const tokentypes = ['TOTP (Timed)', 'HOTP (Counter)']; const tokentypes = ['TOTP (Timed)', 'HOTP (Counter)'];
/* Array of TOTP tokens */ /* Settings */
var tokens=[]; var settings = {tokens:[], misc:{}};
var tokens = settings.tokens;
/* Remove any non-base-32 characters from the given string and collapses /* Remove any non-base-32 characters from the given string and collapses
* whitespace to a single space. Optionally removes all whitespace from * whitespace to a single space. Optionally removes all whitespace from
@ -319,23 +320,21 @@ function doScan() {
*/ */
function loadTokens() { function loadTokens() {
Util.showModal('Loading...'); Util.showModal('Loading...');
Puck.eval(`require('Storage').read(${JSON.stringify('authentiwatch.json')})`,data=>{ Puck.eval(`require('Storage').readJSON(${JSON.stringify('authentiwatch.json')})`,data=>{
Util.hideModal(); Util.hideModal();
try { if (data.data ) settings.tokens = data.data ; /* v0.02 settings */
let load = JSON.parse(data); if (data.tokens) settings.tokens = data.tokens; /* v0.03+ settings */
tokens = load.data; if (data.misc ) settings.misc = data.misc ; /* v0.03+ settings */
updateTokens(); tokens = settings.tokens;
} catch { updateTokens();
tokens = [];
}
}); });
} }
/* Save settings as a JSON file on the watch. /* Save settings as a JSON file on the watch.
*/ */
function saveTokens() { function saveTokens() {
Util.showModal('Saving...'); Util.showModal('Saving...');
let save={data:tokens,count:tokens.length}; let newsettings={tokens:tokens,misc:settings.misc};
Puck.write(`\x10require('Storage').write(${JSON.stringify('authentiwatch.json')},${JSON.stringify(save)})\n`,()=>{ Puck.write(`\x10require('Storage').writeJSON(${JSON.stringify('authentiwatch.json')},${JSON.stringify(newsettings)})\n`,()=>{
Util.hideModal(); Util.hideModal();
}); });
} }