aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-10 20:46:30 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-10 21:58:53 -0400
commit84bd9ff93b15f419ce1076b7545aeb406917f9b5 (patch)
tree8374573a36784ce12abb7b490ccd9791aea3f2e3 /ext
parent71b700cd22f5a57a07cd2f9a6afa823793f5f95e (diff)
Update settings if a different source triggers optionsUpdate
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/api.js6
-rw-r--r--ext/bg/js/backend.js6
-rw-r--r--ext/bg/js/settings.js43
3 files changed, 44 insertions, 11 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 13c0d73a..81772d08 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -21,11 +21,11 @@ function apiOptionsGet(optionsContext) {
return utilBackend().getOptions(optionsContext);
}
-async function apiOptionsSave() {
+async function apiOptionsSave(source) {
const backend = utilBackend();
const options = await backend.getFullOptions();
await optionsSave(options);
- backend.onOptionsUpdated();
+ backend.onOptionsUpdated(source);
}
async function apiTermsFind(text, optionsContext) {
@@ -139,7 +139,7 @@ async function apiCommandExec(command) {
const optionsContext = {depth: 0};
const options = await apiOptionsGet(optionsContext);
options.general.enable = !options.general.enable;
- await apiOptionsSave();
+ await apiOptionsSave('popup');
}
};
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 4763e85d..9a300d62 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -35,7 +35,7 @@ class Backend {
async prepare() {
await this.translator.prepare();
this.options = await optionsLoad();
- this.onOptionsUpdated();
+ this.onOptionsUpdated('background');
if (chrome.commands !== null && typeof chrome.commands === 'object') {
chrome.commands.onCommand.addListener(this.onCommand.bind(this));
@@ -52,13 +52,13 @@ class Backend {
this.isPreparedPromise = null;
}
- onOptionsUpdated() {
+ onOptionsUpdated(source) {
this.applyOptions();
const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
- chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {}}, callback);
+ chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {source}}, callback);
}
});
}
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);
}