diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js
index a77419ff..93755745 100644
--- a/ext/bg/js/options-form.js
+++ b/ext/bg/js/options-form.js
@@ -35,46 +35,47 @@ function formToOptions() {
});
}
-function updateVisibility() {
- if ($('#enableAnkiConnect').prop('checked')) {
+function updateAnkiFormDataVis(opts) {
+ if (opts.enableAnkiConnect) {
+ updateAnkiFormData();
$('.options-anki').show();
} else {
$('.options-anki').hide();
}
}
-function updateAnkiPage() {
+function updateAnkiFormData() {
const yomichan = chrome.extension.getBackgroundPage().yomichan;
- $('#ankiDeck').find('option').remove();
- $('#ankiModel').find('option').remove();
-
- yomichan.getDeckNames((names) => {
- names.forEach((name) => {
- $('#ankiDeck').append($('', {value: name, text: name}));
- });
+ const ankiDeck = $('#ankiDeck');
+ ankiDeck.find('option').remove();
+ yomichan.api_getDeckNames((names) => {
+ if (names !== null) {
+ names.forEach((name) => ankiDeck.append($('', {value: name, text: name})));
+ }
});
- yomichan.getModelNames((names) => {
- names.forEach((name) => {
- $('#ankiModel').append($('', {value: name, text: name}));
- });
+ const ankiModel = $('#ankiModel');
+ ankiModel.find('option').remove();
+ yomichan.api_getModelNames((names) => {
+ if (names !== null) {
+ names.forEach((name) => ankiModel.append($('', {value: name, text: name})));
+ }
});
}
function onOptionsChanged() {
- updateVisibility();
const opts = formToOptions();
saveOptions(opts, () => {
chrome.extension.getBackgroundPage().yomichan.setOptions(opts);
+ updateAnkiFormDataVis(opts);
});
}
$(document).ready(() => {
loadOptions((opts) => {
optionsToForm(opts);
- updateVisibility();
- updateAnkiPage();
+ updateAnkiFormDataVis(opts);
$('input').on('input paste change', onOptionsChanged);
});
});
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js
index 88280ab0..bceaa6ca 100644
--- a/ext/bg/js/yomichan.js
+++ b/ext/bg/js/yomichan.js
@@ -50,30 +50,16 @@ class Yomichan {
}
onMessage(request, sender, callback) {
- const {action, params} = request, handlers = {
- addNote: ({definition, mode}) => this.ankiInvoke('addNote', {definition: definition, mode: mode}, null, callback),
- canAddNotes: ({definitions, modes}) => this.ankiInvoke('canAddNotes', {definitions: definitions, modes: modes}, 'notes', callback),
- findKanji: (text) => callback(this.translator.findKanji(text)),
- findTerm: (text) => callback(this.translator.findTerm(text)),
- getDeckNames: () => this.getDeckNames(callback),
- getModelNames: () => this.getModelNames(callback),
- getOptions: () => callback(this.options),
- getState: () => callback(this.state),
- renderText: ({data, template}) => callback(Handlebars.templates[template](data))
- };
+ const {action, params} = request;
+ const method = this['api_' + action];
+
+ if (typeof(method) === 'function') {
+ method.call(this, callback, params);
+ }
- handlers[action].call(this, params);
return true;
}
- getDeckNames(callback) {
- this.ankiInvoke('deckNames', {}, null, callback);
- }
-
- getModelNames(callback) {
- this.ankiInvoke('modelNames', {}, null, callback);
- }
-
onBrowserAction(tab) {
switch (this.state) {
case 'disabled':
@@ -101,19 +87,16 @@ class Yomichan {
break;
case 'loading':
chrome.browserAction.setBadgeText({text: '...'});
- this.translator.loadData(
- {loadEnamDict: this.options.loadEnamDict},
- () => this.setState('enabled')
- );
+ this.translator.loadData({loadEnamDict: this.options.loadEnamDict}, () => this.setState('enabled'));
break;
}
- Yomichan.notifyChange('state', this.state);
+ Yomichan.notifyTabs('state', this.state);
}
setOptions(options) {
this.options = options;
- Yomichan.notifyChange('options', this.options);
+ Yomichan.notifyTabs('options', this.options);
}
ankiInvoke(action, params, pool, callback) {
@@ -141,13 +124,49 @@ class Yomichan {
}
}
- static notifyChange(name, value) {
+ static notifyTabs(name, value) {
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {name: name, value: value}, () => null);
}
});
}
+
+ api_addNote(callback, {definition, mode}) {
+ this.ankiInvoke('addNote', {definition: definition, mode: mode}, null, callback);
+ }
+
+ api_canAddNotes(callback, {definitions, modes}) {
+ this.ankiInvoke('canAddNotes', {definitions: definitions, modes: modes}, 'notes', callback);
+ }
+
+ api_findKanji(callback, text) {
+ callback(this.translator.findKanji(text));
+ }
+
+ api_findTerm(callback, text) {
+ callback(this.translator.findTerm(text));
+ }
+
+ api_getDeckNames(callback) {
+ this.ankiInvoke('deckNames', {}, null, callback);
+ }
+
+ api_getModelNames(callback) {
+ this.ankiInvoke('modelNames', {}, null, callback);
+ }
+
+ api_getOptions(callback) {
+ callback(this.options);
+ }
+
+ api_getState(callback) {
+ callback(this.state);
+ }
+
+ api_renderText(callback, {template, data}) {
+ callback(Handlebars.templates[template](data));
+ }
}
window.yomichan = new Yomichan();