summaryrefslogtreecommitdiff
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.js64
1 files changed, 35 insertions, 29 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js
index 898f2eea..f7bec615 100644
--- a/ext/bg/js/yomichan.js
+++ b/ext/bg/js/yomichan.js
@@ -17,7 +17,7 @@
*/
-class Yomichan {
+window.yomichan = new class {
constructor() {
Handlebars.partials = Handlebars.templates;
Handlebars.registerHelper('kanjiLinks', helperKanjiLinks);
@@ -28,7 +28,9 @@ class Yomichan {
this.options = null;
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
- chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
+ if (chrome.runtime.onInstalled) {
+ chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
+ }
this.translator.prepare().then(optionsLoad).then(this.setOptions.bind(this));
}
@@ -142,32 +144,12 @@ class Yomichan {
});
}
- textRender(template, data) {
- return Handlebars.templates[template](data);
- }
-
- api_optionsGet({callback}) {
- promiseCallback(optionsLoad(), callback);
- }
-
- api_kanjiFind({text, callback}) {
- promiseCallback(this.kanjiFind(text), callback);
- }
-
- api_termsFind({text, callback}) {
- promiseCallback(this.termsFind(text), callback);
- }
-
- api_textRender({template, data, callback}) {
- callback({result: this.textRender(template, data)});
- }
-
- api_definitionAdd({definition, mode, callback}) {
+ definitionAdd(definition, mode) {
const note = this.formatNote(definition, mode);
- promiseCallback(this.anki.addNote(note), callback);
+ return this.anki.addNote(note);
}
- api_definitionsAddable({definitions, modes, callback}) {
+ definitionsAddable(definitions, modes) {
const notes = [];
for (const definition of definitions) {
for (const mode of modes) {
@@ -175,7 +157,7 @@ class Yomichan {
}
}
- const promise = this.anki.canAddNotes(notes).then(raw => {
+ return this.anki.canAddNotes(notes).then(raw => {
const states = [];
for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) {
const state = {};
@@ -188,9 +170,33 @@ class Yomichan {
return states;
});
+ }
- promiseCallback(promise, callback);
+ textRender(template, data) {
+ return Promise.resolve(Handlebars.templates[template](data));
+ }
+
+ api_optionsGet({callback}) {
+ promiseCallback(optionsLoad(), callback);
}
-}
-window.yomichan = new Yomichan();
+ api_kanjiFind({text, callback}) {
+ promiseCallback(this.kanjiFind(text), callback);
+ }
+
+ api_termsFind({text, callback}) {
+ promiseCallback(this.termsFind(text), callback);
+ }
+
+ api_textRender({template, data, callback}) {
+ promiseCallback(this.textRender(template, data), callback);
+ }
+
+ api_definitionAdd({definition, mode, callback}) {
+ promiseCallback(this.definitionAdd(definition, mode), callback);
+ }
+
+ api_definitionsAddable({definitions, modes, callback}) {
+ promiseCallback(this.definitionsAddable(definitions, modes), callback);
+ }
+};