From 25a8f4fb6820cbd4be9286c85a00165ce01c0704 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 21 Oct 2016 20:08:16 -0700 Subject: [PATCH] Cleanup --- ext/bg/background.html | 1 + ext/bg/js/util.js | 18 +++++++++++++----- ext/bg/js/yomichan.js | 32 ++++++++------------------------ 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/ext/bg/background.html b/ext/bg/background.html index 863d1ab4..1bb36cf8 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -3,6 +3,7 @@ + diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 1e033eef..f10e4291 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -30,6 +30,19 @@ function kanjiLinks(options) { 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) { return new Promise((resolve, reject) => { 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) { return tags.sort((v1, v2) => { const order1 = v1.order; diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 4ba44588..3091e503 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -235,27 +235,15 @@ class Yomichan { } api_getOptions({callback}) { - loadOptions().then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); + promiseCallback(loadOptions(), callback); } api_findKanji({text, callback}) { - this.translator.findKanji(text).then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); + promiseCallback(this.translator.findKanji(text), callback); } api_findTerm({text, callback}) { - this.translator.findTerm(text).then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); + promiseCallback(this.translator.findTerm(text), callback); } api_renderText({template, data, callback}) { @@ -264,11 +252,7 @@ class Yomichan { api_addDefinition({definition, mode, callback}) { const note = this.formatNote(definition, mode); - this.anki.addNote(note).then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); + promiseCallback(this.anki.addNote(note), 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 = []; for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { const state = {}; @@ -290,10 +274,10 @@ class Yomichan { states.push(state); } - callback({result: states}); - }).catch(error => { - callback({error}); + return states; }); + + promiseCallback(promise, callback); } }