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