aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/yomichan.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-05-21 17:44:02 -0700
committerAlex Yatskov <alex@foosoft.net>2016-05-21 17:44:02 -0700
commit5f97e8d16a18873f2a4f3cac47d295280764a142 (patch)
tree5f64b4e0d96655936dd2a6e9efd84430e5e73611 /ext/bg/js/yomichan.js
parentad54735a80ede1980eca4ab51273256c5fd8d3bd (diff)
WIP
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();