summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/options-form.js35
-rw-r--r--ext/bg/js/yomichan.js73
2 files changed, 64 insertions, 44 deletions
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($('<option/>', {value: name, text: name}));
- });
+ const ankiDeck = $('#ankiDeck');
+ ankiDeck.find('option').remove();
+ yomichan.api_getDeckNames((names) => {
+ if (names !== null) {
+ names.forEach((name) => ankiDeck.append($('<option/>', {value: name, text: name})));
+ }
});
- yomichan.getModelNames((names) => {
- names.forEach((name) => {
- $('#ankiModel').append($('<option/>', {value: name, text: name}));
- });
+ const ankiModel = $('#ankiModel');
+ ankiModel.find('option').remove();
+ yomichan.api_getModelNames((names) => {
+ if (names !== null) {
+ names.forEach((name) => ankiModel.append($('<option/>', {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,28 +50,14 @@ 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))
- };
-
- handlers[action].call(this, params);
- return true;
- }
+ const {action, params} = request;
+ const method = this['api_' + action];
- getDeckNames(callback) {
- this.ankiInvoke('deckNames', {}, null, callback);
- }
+ if (typeof(method) === 'function') {
+ method.call(this, callback, params);
+ }
- getModelNames(callback) {
- this.ankiInvoke('modelNames', {}, null, callback);
+ return true;
}
onBrowserAction(tab) {
@@ -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();