aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/yomichan.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/yomichan.js')
-rw-r--r--ext/bg/js/yomichan.js73
1 files changed, 46 insertions, 27 deletions
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();