diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-10 20:46:30 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-10 21:58:53 -0400 |
commit | 84bd9ff93b15f419ce1076b7545aeb406917f9b5 (patch) | |
tree | 8374573a36784ce12abb7b490ccd9791aea3f2e3 /ext/bg/js/settings.js | |
parent | 71b700cd22f5a57a07cd2f9a6afa823793f5f95e (diff) |
Update settings if a different source triggers optionsUpdate
Diffstat (limited to 'ext/bg/js/settings.js')
-rw-r--r-- | ext/bg/js/settings.js | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f2f08b6e..7f3e5c69 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -218,7 +218,7 @@ async function onFormOptionsChanged(e) { const optionsAnkiServerOld = options.anki.server; await formRead(options); - await apiOptionsSave(); + await settingsSaveOptions(); formUpdateVisibility(options); try { @@ -246,12 +246,45 @@ async function onReady() { await formWrite(options); storageInfoInitialize(); + + chrome.runtime.onMessage.addListener(onMessage); } $(document).ready(utilAsync(onReady)); /* + * Remote options updates + */ + +function settingsGetSource() { + return new Promise((resolve) => { + chrome.tabs.getCurrent((tab) => resolve(`settings${tab ? tab.id : ''}`)); + }); +} + +async function settingsSaveOptions() { + const source = await settingsGetSource(); + await apiOptionsSave(source); +} + +async function onOptionsUpdate({source}) { + const thisSource = await settingsGetSource(); + if (source === thisSource) { return; } + + const optionsContext = getOptionsContext(); + const options = await apiOptionsGet(optionsContext); + await formWrite(options); +} + +function onMessage({action, params}) { + if (action === 'optionsUpdate') { + onOptionsUpdate(params); + } +} + + +/* * Dictionary */ @@ -395,7 +428,7 @@ async function onDictionaryPurge(e) { const options = await apiOptionsGet(optionsContext); options.dictionaries = {}; options.general.mainDictionary = ''; - await apiOptionsSave(); + await settingsSaveOptions(); await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); @@ -445,7 +478,7 @@ async function onDictionaryImport(e) { dictionaryErrorsShow(exceptions); } - await apiOptionsSave(); + await settingsSaveOptions(); await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); @@ -589,7 +622,7 @@ async function onAnkiModelChanged(e) { const options = await apiOptionsGet(optionsContext); await formRead(options); options.anki[tabId].fields = {}; - await apiOptionsSave(); + await settingsSaveOptions(); ankiSpinnerShow(true); await ankiFieldsPopulate(element, options); @@ -609,7 +642,7 @@ async function onAnkiFieldTemplatesReset(e) { const fieldTemplates = optionsFieldTemplates(); options.anki.fieldTemplates = fieldTemplates; $('#field-templates').val(fieldTemplates); - await apiOptionsSave(); + await settingsSaveOptions(); } catch (e) { ankiErrorShow(e); } |