fix options loading and storing code

This commit is contained in:
Alex Yatskov 2017-03-05 11:12:48 -08:00
parent 974b103409
commit 200b8f6b75
3 changed files with 22 additions and 9 deletions

View File

@ -80,7 +80,7 @@ function updateVisibility(options) {
} }
$(document).ready(() => { $(document).ready(() => {
Handlebars.partials = Handlebars.templates; handlebarsRegister();
optionsLoad().then(options => { optionsLoad().then(options => {
$('#audio-playback-buttons').prop('checked', options.general.audioPlayback); $('#audio-playback-buttons').prop('checked', options.general.audioPlayback);
@ -148,7 +148,7 @@ function populateDictionaries(options) {
return instDb().getDictionaries().then(rows => { return instDb().getDictionaries().then(rows => {
rows.forEach(row => { rows.forEach(row => {
const dictOptions = options.dictionaries[row.title] || {enableTerms: false, enableKanji: false, priority: 0}; 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, title: row.title,
version: row.version, version: row.version,
revision: row.revision, revision: row.revision,

View File

@ -195,10 +195,24 @@ function optionsLoad() {
resolve(0); resolve(0);
} }
}).then(bytes => { }).then(bytes => {
const storage = bytes === 0 ? chrome.storage.local : chrome.storage.sync; if (bytes === 0) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
storage.get(null, options => resolve(optionsVersion(options))); 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 => { }).then(options => {
if (chrome.storage.sync) { if (chrome.storage.sync) {
chrome.storage.sync.clear(); chrome.storage.sync.clear();
@ -210,7 +224,7 @@ function optionsLoad() {
function optionsSave(options) { function optionsSave(options) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.storage.local.set(options, resolve); chrome.storage.local.set({options: JSON.stringify(options)}, resolve);
}).then(() => { }).then(() => {
instYomi().optionsSet(options); instYomi().optionsSet(options);
fgOptionsSet(options); fgOptionsSet(options);

View File

@ -29,9 +29,8 @@
</p> </p>
<script src="/mixed/lib/jquery-3.1.1.min.js"></script> <script src="/mixed/lib/jquery-3.1.1.min.js"></script>
<script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script> <script src="/mixed/lib/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script src="/bg/js/gecko.js"></script> <script src="/mixed/lib/handlebars.min.js"></script>
<script src="/bg/js/util.js"></script> <script src="/bg/js/util.js"></script>
<script src="/bg/js/options.js"></script>
<script src="/bg/js/popup.js"></script> <script src="/bg/js/popup.js"></script>
</body> </body>
</html> </html>