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.js109
1 files changed, 42 insertions, 67 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js
index 46a240a3..04f29f42 100644
--- a/ext/bg/js/yomichan.js
+++ b/ext/bg/js/yomichan.js
@@ -23,10 +23,9 @@ class Yomichan {
Handlebars.registerHelper('kanjiLinks', kanjiLinks);
this.translator = new Translator();
+ this.anki = new AnkiNull();
this.options = null;
this.importTabId = null;
- this.asyncPools = {};
- this.ankiConnectVer = 0;
this.setState('disabled');
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
@@ -101,11 +100,20 @@ class Yomichan {
setOptions(options) {
this.options = options;
- this.tabInvokeAll('setOptions', this.options);
- }
- getApiVersion() {
- return 1;
+ switch (options.ankiMethod) {
+ case 'ankiweb':
+ this.anki = new AnkiWeb(options.ankiUsername, options.ankiPassword);
+ break;
+ case 'ankiconnect':
+ this.anki = new AnkiConnect();
+ break;
+ default:
+ this.anki = new AnkiNull();
+ break;
+ }
+
+ this.tabInvokeAll('setOptions', this.options);
}
tabInvokeAll(action, params) {
@@ -120,49 +128,12 @@ class Yomichan {
chrome.tabs.sendMessage(tabId, {action, params}, () => null);
}
- ankiInvokeSafe(action, params, pool, callback) {
- if (this.ankiConnectVer === this.getApiVersion()) {
- this.ankiInvoke(action, params, pool, callback);
- } else {
- this.api_getVersion({callback: version => {
- if (version === this.getApiVersion()) {
- this.ankiConnectVer = version;
- this.ankiInvoke(action, params, pool, callback);
- } else {
- callback(null);
- }
- }});
- }
- }
-
- ankiInvoke(action, params, pool, callback) {
- if (this.options.enableAnkiConnect) {
- if (pool !== null && this.asyncPools.hasOwnProperty(pool)) {
- this.asyncPools[pool].abort();
- }
-
- const xhr = new XMLHttpRequest();
- xhr.addEventListener('loadend', () => {
- if (pool !== null) {
- delete this.asyncPools[pool];
- }
-
- const resp = xhr.responseText;
- callback(resp ? JSON.parse(resp) : null);
- });
-
- xhr.open('POST', 'http://127.0.0.1:8765');
- xhr.send(JSON.stringify({action, params}));
- } else {
- callback(null);
- }
- }
-
formatField(field, definition, mode) {
const markers = [
'audio',
'character',
'expression',
+ 'expression-furigana',
'glossary',
'glossary-list',
'kunyomi',
@@ -184,6 +155,13 @@ class Yomichan {
value = definition.reading;
}
break;
+ case 'expression-furigana':
+ if (mode === 'term_kana' && definition.reading) {
+ value = definition.reading;
+ } else {
+ value = `<ruby>${definition.expression}<rt>${definition.reading}</rt></ruby>`;
+ }
+ break;
case 'reading':
if (mode === 'term_kana') {
value = null;
@@ -257,12 +235,24 @@ class Yomichan {
}
api_getOptions({callback}) {
- loadOptions().then(opts => callback(opts));
+ loadOptions().then(opts => callback(opts)).catch(() => callback(null));
+ }
+
+ api_findKanji({text, callback}) {
+ this.translator.findKanji(text).then(result => callback(result)).catch(() => callback(null));
+ }
+
+ api_findTerm({text, callback}) {
+ this.translator.findTerm(text).then(result => callback(result)).catch(() => callback(null));
+ }
+
+ api_renderText({template, data, callback}) {
+ callback(Handlebars.templates[template](data));
}
api_addDefinition({definition, mode, callback}) {
const note = this.formatNote(definition, mode);
- this.ankiInvokeSafe('addNote', {note}, null, callback);
+ this.anki.addNote(note).then(callback).catch(() => callback(null));
}
api_canAddDefinitions({definitions, modes, callback}) {
@@ -273,9 +263,8 @@ class Yomichan {
}
}
- this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', results => {
+ this.anki.canAddNotes(notes).then(results => {
const states = [];
-
if (results !== null) {
for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) {
const state = {};
@@ -288,35 +277,21 @@ class Yomichan {
}
callback(states);
+ }).catch(() => {
+ callback(null);
});
}
- api_findKanji({text, callback}) {
- this.translator.findKanji(text).then(result => callback(result));
- }
-
- api_findTerm({text, callback}) {
- this.translator.findTerm(text).then(result => callback(result));
- }
-
api_getDeckNames({callback}) {
- this.ankiInvokeSafe('deckNames', {}, null, callback);
+ this.anki.getDeckNames().then(callback).catch(() => callback(null));
}
api_getModelNames({callback}) {
- this.ankiInvokeSafe('modelNames', {}, null, callback);
+ this.anki.getModelNames().then(callback).catch(() => callback(null));
}
api_getModelFieldNames({modelName, callback}) {
- this.ankiInvokeSafe('modelFieldNames', {modelName}, null, callback);
- }
-
- api_getVersion({callback}) {
- this.ankiInvoke('version', {}, null, callback);
- }
-
- api_renderText({template, data, callback}) {
- callback(Handlebars.templates[template](data));
+ this.anki.getModelFieldNames(modelName).then(callback).catch(() => callback(null));
}
}