From 200b8f6b7537ff14ef17e9387dbf6dd642e3d4b9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 5 Mar 2017 11:12:48 -0800 Subject: [PATCH] fix options loading and storing code --- ext/bg/js/options.js | 4 ++-- ext/bg/js/util.js | 24 +++++++++++++++++++----- ext/bg/popup.html | 3 +-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 5f564b33..274f2e64 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -80,7 +80,7 @@ function updateVisibility(options) { } $(document).ready(() => { - Handlebars.partials = Handlebars.templates; + handlebarsRegister(); optionsLoad().then(options => { $('#audio-playback-buttons').prop('checked', options.general.audioPlayback); @@ -148,7 +148,7 @@ function populateDictionaries(options) { return instDb().getDictionaries().then(rows => { rows.forEach(row => { const dictOptions = options.dictionaries[row.title] || {enableTerms: false, enableKanji: false, priority: 0}; - const html = Handlebars.templates['dictionary.html']({ + const html = handlebarsRender('dictionary.html', { title: row.title, version: row.version, revision: row.revision, diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 6d5c030c..75847ee6 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -195,10 +195,24 @@ function optionsLoad() { resolve(0); } }).then(bytes => { - const storage = bytes === 0 ? chrome.storage.local : chrome.storage.sync; - return new Promise((resolve, reject) => { - storage.get(null, options => resolve(optionsVersion(options))); - }); + if (bytes === 0) { + return new Promise((resolve, reject) => { + chrome.storage.local.get(null, store => { + let options = {}; + try { + options = JSON.parse(store.options); + } catch (e) { + // NOP + } + + resolve(optionsVersion(options)); + }); + }); + } else { + return new Promise((resolve, reject) => { + chrome.storage.sync.get(null, options => resolve(optionsVersion(options))); + }); + } }).then(options => { if (chrome.storage.sync) { chrome.storage.sync.clear(); @@ -210,7 +224,7 @@ function optionsLoad() { function optionsSave(options) { return new Promise((resolve, reject) => { - chrome.storage.local.set(options, resolve); + chrome.storage.local.set({options: JSON.stringify(options)}, resolve); }).then(() => { instYomi().optionsSet(options); fgOptionsSet(options); diff --git a/ext/bg/popup.html b/ext/bg/popup.html index 388cff9c..22fc0d40 100644 --- a/ext/bg/popup.html +++ b/ext/bg/popup.html @@ -29,9 +29,8 @@

- + -