This commit is contained in:
Alex Yatskov 2016-10-21 20:08:16 -07:00
parent 3962a9cb19
commit 25a8f4fb68
3 changed files with 22 additions and 29 deletions

View File

@ -3,6 +3,7 @@
<body> <body>
<script src="../lib/handlebars.min.js"></script> <script src="../lib/handlebars.min.js"></script>
<script src="../lib/dexie.min.js"></script> <script src="../lib/dexie.min.js"></script>
<script src="../lib/wanakana.min.js"></script>
<script src="js/ankiweb.js"></script> <script src="js/ankiweb.js"></script>
<script src="js/ankiconnect.js"></script> <script src="js/ankiconnect.js"></script>
<script src="js/ankinull.js"></script> <script src="js/ankinull.js"></script>

View File

@ -30,6 +30,19 @@ function kanjiLinks(options) {
return result; return result;
} }
function isKanji(c) {
const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
}
function promiseCallback(promise, callback) {
return promise.then(result => {
callback({result});
}).catch(error => {
callback({error});
});
}
function loadJson(url) { function loadJson(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
@ -39,11 +52,6 @@ function loadJson(url) {
}); });
} }
function isKanji(c) {
const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
}
function sortTags(tags) { function sortTags(tags) {
return tags.sort((v1, v2) => { return tags.sort((v1, v2) => {
const order1 = v1.order; const order1 = v1.order;

View File

@ -235,27 +235,15 @@ class Yomichan {
} }
api_getOptions({callback}) { api_getOptions({callback}) {
loadOptions().then(result => { promiseCallback(loadOptions(), callback);
callback({result});
}).catch(error => {
callback({error});
});
} }
api_findKanji({text, callback}) { api_findKanji({text, callback}) {
this.translator.findKanji(text).then(result => { promiseCallback(this.translator.findKanji(text), callback);
callback({result});
}).catch(error => {
callback({error});
});
} }
api_findTerm({text, callback}) { api_findTerm({text, callback}) {
this.translator.findTerm(text).then(result => { promiseCallback(this.translator.findTerm(text), callback);
callback({result});
}).catch(error => {
callback({error});
});
} }
api_renderText({template, data, callback}) { api_renderText({template, data, callback}) {
@ -264,11 +252,7 @@ class Yomichan {
api_addDefinition({definition, mode, callback}) { api_addDefinition({definition, mode, callback}) {
const note = this.formatNote(definition, mode); const note = this.formatNote(definition, mode);
this.anki.addNote(note).then(result => { promiseCallback(this.anki.addNote(note), callback);
callback({result});
}).catch(error => {
callback({error});
});
} }
api_canAddDefinitions({definitions, modes, callback}) { api_canAddDefinitions({definitions, modes, callback}) {
@ -279,7 +263,7 @@ class Yomichan {
} }
} }
this.anki.canAddNotes(notes).then(raw => { const promise = this.anki.canAddNotes(notes).then(raw => {
const states = []; const states = [];
for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) {
const state = {}; const state = {};
@ -290,10 +274,10 @@ class Yomichan {
states.push(state); states.push(state);
} }
callback({result: states}); return states;
}).catch(error => {
callback({error});
}); });
promiseCallback(promise, callback);
} }
} }