aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/yomichan.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-05-02 17:27:26 -0700
committerAlex Yatskov <alex@foosoft.net>2016-05-02 17:27:26 -0700
commitc70aedc2ff546cb6a8ed1401e8b5e3a0ca681c28 (patch)
tree8db98a0bed94bc2cbab7c6fe27dc88506136c374 /ext/bg/js/yomichan.js
parenteaa7291683604895017c67e716f7ee44a1ea853a (diff)
WIP
Diffstat (limited to 'ext/bg/js/yomichan.js')
-rw-r--r--ext/bg/js/yomichan.js42
1 files changed, 33 insertions, 9 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js
index ac02f882..a412eef7 100644
--- a/ext/bg/js/yomichan.js
+++ b/ext/bg/js/yomichan.js
@@ -50,17 +50,14 @@ class Yomichan {
onMessage(request, sender, callback) {
const {action, data} = request, handlers = {
- findKanji: ({text}) => this.translator.findKanji(text),
- findTerm: ({text}) => this.translator.findTerm(text),
- getState: () => this.state,
- getOptions: () => this.options,
- renderText: ({data, template}) => Handlebars.templates[template](data)
+ findKanji: ({text}) => this.findKanji(text, callback),
+ findTerm: ({text}) => this.findTerm(text, callback),
+ getOptions: () => callback(this.options),
+ getState: () => callback(this.state),
+ renderText: ({data, template}) => callback(Handlebars.templates[template](data))
};
- const result = handlers[action].call(this, data);
- if (callback !== null) {
- callback(result);
- }
+ handlers[action].call(this, data);
}
onBrowserAction(tab) {
@@ -102,6 +99,33 @@ class Yomichan {
Yomichan.notifyChange('options', this.options);
}
+ findTerm(text, callback) {
+ const results = this.translator.findTerm(text);
+ this.callAnkiApi('canAddNotes', results, (resultsFull) => {
+ callback(resultsFull || results);
+ });
+ }
+
+ findKanji(text, callback) {
+ const results = this.translator.findKanji(text);
+ this.callAnkiApi('cannAddNotes', results, (resultsFull) => {
+ callback(resultsFull || results);
+ });
+ }
+
+ callAnkiApi(action, data, callback) {
+ if (this.options.enableAnkiConnect) {
+ const xhr = new XMLHttpRequest();
+ xhr.addEventListener('load', () => callback(JSON.parse(xhr.responseText)));
+ xhr.open('POST', 'http://127.0.0.1:8888');
+ xhr.withCredentials = true;
+ xhr.setRequestHeader('Content-Type', 'text/json');
+ xhr.send(JSON.stringify(data));
+ } else {
+ callback(null);
+ }
+ }
+
static notifyChange(name, value) {
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {