From 6e986bf1f5957be12ad610c627060f4d86eca98c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 9 Jul 2017 16:29:52 -0700 Subject: cleanup --- ext/bg/js/translator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 8d65a0cd..f1858247 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -53,7 +53,7 @@ class Translator { } return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => { - const textHiragana = wanakana._katakanaToHiragana(text); + const textHiragana = jpKatakanaToHiragana(text); if (text === textHiragana) { return deinfLiteral; } else { -- cgit v1.2.3 From b6f3919ef63f969769fbf030e8fd8b14b0e1c214 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 14:10:58 -0700 Subject: move translator to async --- ext/bg/js/database.js | 28 ++++----- ext/bg/js/translator.js | 160 ++++++++++++++++++++---------------------------- 2 files changed, 80 insertions(+), 108 deletions(-) (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 06312438..9eed8ea3 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -69,14 +69,14 @@ class Database { await this.prepare(); } - async findTerms(term, dictionaries) { + async findTerms(term, titles) { if (!this.db) { throw 'database not initialized'; } const results = []; await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { - if (dictionaries.includes(row.dictionary)) { + if (titles.includes(row.dictionary)) { results.push({ expression: row.expression, reading: row.reading, @@ -90,7 +90,7 @@ class Database { } }); - await this.cacheTagMeta(dictionaries); + await this.cacheTagMeta(titles); for (const result of results) { result.tagMeta = this.tagCache[result.dictionary] || {}; } @@ -98,14 +98,14 @@ class Database { return results; } - async findKanji(kanji, dictionaries) { + async findKanji(kanji, titles) { if (!this.db) { return Promise.reject('database not initialized'); } const results = []; await this.db.kanji.where('character').equals(kanji).each(row => { - if (dictionaries.includes(row.dictionary)) { + if (titles.includes(row.dictionary)) { results.push({ character: row.character, onyomi: dictFieldSplit(row.onyomi), @@ -117,7 +117,7 @@ class Database { } }); - await this.cacheTagMeta(dictionaries); + await this.cacheTagMeta(titles); for (const result of results) { result.tagMeta = this.tagCache[result.dictionary] || {}; } @@ -125,29 +125,29 @@ class Database { return results; } - async cacheTagMeta(dictionaries) { + async cacheTagMeta(titles) { if (!this.db) { throw 'database not initialized'; } - for (const dictionary of dictionaries) { - if (!this.tagCache[dictionary]) { + for (const title of titles) { + if (!this.tagCache[title]) { const tagMeta = {}; - await this.db.tagMeta.where('dictionary').equals(dictionary).each(row => { + await this.db.tagMeta.where('dictionary').equals(title).each(row => { tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order}; }); - this.tagCache[dictionary] = tagMeta; + this.tagCache[title] = tagMeta; } } } async getDictionaries() { - if (!this.db) { + if (this.db) { + return this.db.dictionaries.toArray(); + } else { throw 'database not initialized'; } - - return this.db.dictionaries.toArray(); } async importDictionary(archive, callback) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f1858247..35b49608 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -19,135 +19,107 @@ class Translator { constructor() { - this.loaded = false; - this.ruleMeta = null; this.database = new Database(); this.deinflector = new Deinflector(); + this.loaded = false; } - prepare() { - if (this.loaded) { - return Promise.resolve(); - } - - const promises = [ - jsonLoadInt('/bg/lang/deinflect.json'), - this.database.prepare() - ]; - - return Promise.all(promises).then(([reasons]) => { + async prepare() { + if (!this.loaded) { + const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); this.deinflector.setReasons(reasons); + await this.database.prepare(); this.loaded = true; - }); + } } - findTerms(text, dictionaries, alphanumeric) { - const titles = Object.keys(dictionaries); - const cache = {}; + async findTermsGrouped(text, dictionaries, alphanumeric) { + const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); + return {length, definitions: dictTermsGroup(definitions, dictionaries)}; + } + async findTerms(text, dictionaries, alphanumeric) { if (!alphanumeric && text.length > 0) { const c = text[0]; if (!jpIsKana(c) && !jpIsKanji(c)) { - return Promise.resolve({length: 0, definitions: []}); + return {length: 0, definitions: []}; } } - return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => { - const textHiragana = jpKatakanaToHiragana(text); - if (text === textHiragana) { - return deinfLiteral; - } else { - return this.findTermsDeinflected(textHiragana, titles, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana)); - } - }).then(deinflections => { - let definitions = []; - for (const deinflection of deinflections) { - for (const definition of deinflection.definitions) { - const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); - tags.push(dictTagBuildSource(definition.dictionary)); - definitions.push({ - source: deinflection.source, - reasons: deinflection.reasons, - score: definition.score, - id: definition.id, - dictionary: definition.dictionary, - expression: definition.expression, - reading: definition.reading, - glossary: definition.glossary, - tags: dictTagsSort(tags) - }); - } - } - - definitions = dictTermsUndupe(definitions); - definitions = dictTermsSort(definitions, dictionaries); - - let length = 0; - for (const definition of definitions) { - length = Math.max(length, definition.source.length); - } - - return {length, definitions}; - }); - } - - findTermsGrouped(text, dictionaries, alphanumeric) { - return this.findTerms(text, dictionaries, alphanumeric).then(({length, definitions}) => { - return {length, definitions: dictTermsGroup(definitions, dictionaries)}; - }); - } - - findKanji(text, dictionaries) { + const cache = {}; const titles = Object.keys(dictionaries); - const processed = {}; - const promises = []; - - for (const c of text) { - if (!processed[c]) { - promises.push(this.database.findKanji(c, titles)); - processed[c] = true; - } + let deinflections = await this.findTermsDeinflected(text, titles, cache); + const textHiragana = jpKatakanaToHiragana(text); + if (text !== textHiragana) { + deinflections = deinflections.concat(await this.findTermsDeinflected(textHiragana, titles, cache)); } - return Promise.all(promises).then(defSets => { - const definitions = defSets.reduce((a, b) => a.concat(b), []); - for (const definition of definitions) { + let definitions = []; + for (const deinflection of deinflections) { + for (const definition of deinflection.definitions) { const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); tags.push(dictTagBuildSource(definition.dictionary)); - definition.tags = dictTagsSort(tags); + definitions.push({ + source: deinflection.source, + reasons: deinflection.reasons, + score: definition.score, + id: definition.id, + dictionary: definition.dictionary, + expression: definition.expression, + reading: definition.reading, + glossary: definition.glossary, + tags: dictTagsSort(tags) + }); } + } + + definitions = dictTermsUndupe(definitions); + definitions = dictTermsSort(definitions, dictionaries); - return definitions; - }); + let length = 0; + for (const definition of definitions) { + length = Math.max(length, definition.source.length); + } + + return {length, definitions}; } - findTermsDeinflected(text, dictionaries, cache) { - const definer = term => { + async findTermsDeinflected(text, dictionaries, cache) { + await this.prepare(); + + const definer = async term => { if (cache.hasOwnProperty(term)) { - return Promise.resolve(cache[term]); + return cache[term]; + } else { + return cache[term] = await this.database.findTerms(term, dictionaries); } - - return this.database.findTerms(term, dictionaries).then(definitions => cache[term] = definitions); }; - const promises = []; + let deinflections = []; for (let i = text.length; i > 0; --i) { - promises.push(this.deinflector.deinflect(text.slice(0, i), definer)); + const textSlice = text.slice(0, i); + deinflections = deinflections.concat(await this.deinflector.deinflect(textSlice, definer)); } - return Promise.all(promises).then(results => { - let deinflections = []; - for (const result of results) { - deinflections = deinflections.concat(result); - } - - return deinflections; - }); + return deinflections; } - processKanji(definitions) { + async findKanji(text, dictionaries) { + await this.prepare(); + + let definitions = []; + const processed = {}; + const titles = Object.keys(dictionaries); + for (const c of text) { + if (!processed[c]) { + definitions = definitions.concat(await this.database.findKanji(c, titles)); + processed[c] = true; + } + } + for (const definition of definitions) { const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); + tags.push(dictTagBuildSource(definition.dictionary)); definition.tags = dictTagsSort(tags); } -- cgit v1.2.3 From 49352c5fa1baea4a6ed7d71d1353c13a56b00bca Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 14:30:34 -0700 Subject: move deinflector to async --- ext/bg/js/deinflector.js | 64 +++++++++++++++++++++--------------------------- ext/bg/js/translator.js | 16 ++++++------ 2 files changed, 37 insertions(+), 43 deletions(-) (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 6484f953..8b67761b 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -26,26 +26,7 @@ class Deinflection { this.children = []; } - deinflect(definer, reasons) { - const define = () => { - return definer(this.term).then(definitions => { - if (this.rules.length === 0) { - this.definitions = definitions; - } else { - for (const rule of this.rules) { - for (const definition of definitions) { - if (definition.rules.includes(rule)) { - this.definitions.push(definition); - } - } - } - } - - return this.definitions.length > 0; - }); - }; - - const promises = []; + async deinflect(definer, reasons) { for (const reason in reasons) { for (const variant of reasons[reason]) { let accept = this.rules.length === 0; @@ -68,20 +49,31 @@ class Deinflection { } const child = new Deinflection(term, {reason, rules: variant.rulesOut}); - promises.push( - child.deinflect(definer, reasons).then(valid => valid && this.children.push(child)) - ); + if (await child.deinflect(definer, reasons)) { + this.children.push(child); + } } } - return Promise.all(promises).then(define).then(valid => { - if (valid && this.children.length > 0) { - const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions}); - this.children.push(child); + const definitions = await definer(this.term); + if (this.rules.length === 0) { + this.definitions = definitions; + } else { + for (const rule of this.rules) { + for (const definition of definitions) { + if (definition.rules.includes(rule)) { + this.definitions.push(definition); + } + } } + } + + if (this.definitions.length > 0 && this.children.length > 0) { + const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions}); + this.children.push(child); + } - return valid || this.children.length > 0; - }); + return this.definitions.length > 0 || this.children.length > 0; } gather() { @@ -112,16 +104,16 @@ class Deinflection { class Deinflector { - constructor() { - this.reasons = {}; - } - - setReasons(reasons) { + constructor(reasons) { this.reasons = reasons; } - deinflect(term, definer) { + async deinflect(term, definer) { const node = new Deinflection(term); - return node.deinflect(definer, this.reasons).then(success => success ? node.gather() : []); + if (await node.deinflect(definer, this.reasons)) { + return node.gather(); + } else { + return []; + } } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 35b49608..84a6e1d7 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -19,17 +19,19 @@ class Translator { constructor() { - this.database = new Database(); - this.deinflector = new Deinflector(); - this.loaded = false; + this.database = null; + this.deinflector = null; } async prepare() { - if (!this.loaded) { - const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); - this.deinflector.setReasons(reasons); + if (!this.database) { + this.database = new Database(); await this.database.prepare(); - this.loaded = true; + } + + if (!this.deinflector) { + const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); + this.deinflector = new Deinflector(reasons); } } -- cgit v1.2.3 From 28bc1449d1b2260df2970318982385b0d8456c54 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 15:00:38 -0700 Subject: cleanup --- ext/bg/js/translator.js | 22 +++++++++++++++++++++- ext/bg/js/util.js | 26 -------------------------- 2 files changed, 21 insertions(+), 27 deletions(-) (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 84a6e1d7..dfe54623 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -30,7 +30,8 @@ class Translator { } if (!this.deinflector) { - const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); + const url = chrome.extension.getURL('/bg/lang/deinflect.json'); + const reasons = await Translator.loadRules(url); this.deinflector = new Deinflector(reasons); } } @@ -127,4 +128,23 @@ class Translator { return definitions; } + + + static loadRules(url) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.open('GET', url); + xhr.send(); + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } + }); + } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 1954e83b..b8a60217 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -429,32 +429,6 @@ function dictFieldFormat(field, definition, mode, options) { } -/* - * Json - */ - -function jsonLoad(url) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.overrideMimeType('application/json'); - xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); - xhr.open('GET', url); - xhr.send(); - }).then(responseText => { - try { - return JSON.parse(responseText); - } - catch (e) { - return Promise.reject('invalid JSON response'); - } - }); -} - -function jsonLoadInt(url) { - return jsonLoad(chrome.extension.getURL(url)); -} - /* * Helpers */ -- cgit v1.2.3 From a73b8fbab747fc5d7d2802b88d4007421cda4d87 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 15:20:07 -0700 Subject: cleanup --- ext/bg/js/translator.js | 9 ++------- ext/bg/js/yomichan.js | 5 ++++- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index dfe54623..aa11ea63 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -87,14 +87,12 @@ class Translator { return {length, definitions}; } - async findTermsDeinflected(text, dictionaries, cache) { - await this.prepare(); - + async findTermsDeinflected(text, titles, cache) { const definer = async term => { if (cache.hasOwnProperty(term)) { return cache[term]; } else { - return cache[term] = await this.database.findTerms(term, dictionaries); + return cache[term] = await this.database.findTerms(term, titles); } }; @@ -108,8 +106,6 @@ class Translator { } async findKanji(text, dictionaries) { - await this.prepare(); - let definitions = []; const processed = {}; const titles = Object.keys(dictionaries); @@ -129,7 +125,6 @@ class Translator { return definitions; } - static loadRules(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index e0bdabc3..acc560ce 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -25,9 +25,12 @@ window.yomichan = new class { this.anki = new AnkiNull(); this.options = null; - this.translator.prepare().then(optionsLoad).then(this.optionsSet.bind(this)).then(() => { + this.translator.prepare().then(optionsLoad).then(options => { + this.optionsSet(options); + chrome.commands.onCommand.addListener(this.onCommand.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); + if (this.options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); } -- cgit v1.2.3 From b0cdf59bd8dae8f44362b14bdf19b243514e31d3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 16:24:31 -0700 Subject: move anki to async --- ext/bg/js/anki-connect.js | 69 +++++++++++++++++------------------------------ ext/bg/js/anki-null.js | 24 ++++++++--------- ext/bg/js/translator.js | 20 +------------- ext/bg/js/util.js | 25 +++++++++++++++++ 4 files changed, 63 insertions(+), 75 deletions(-) (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/js/anki-connect.js b/ext/bg/js/anki-connect.js index 173feefd..a4d8ba3f 100644 --- a/ext/bg/js/anki-connect.js +++ b/ext/bg/js/anki-connect.js @@ -20,69 +20,50 @@ class AnkiConnect { constructor(server) { this.server = server; - this.asyncPools = {}; this.localVersion = 2; - this.remoteVersion = null; + this.remoteVersion = 0; } - addNote(note) { - return this.checkVersion().then(() => this.ankiInvoke('addNote', {note})); + async addNote(note) { + await this.checkVersion(); + return await this.ankiInvoke('addNote', {note}); } - canAddNotes(notes) { - return this.checkVersion().then(() => this.ankiInvoke('canAddNotes', {notes}, 'notes')); + async canAddNotes(notes) { + await this.checkVersion(); + return await this.ankiInvoke('canAddNotes', {notes}); } - getDeckNames() { - return this.checkVersion().then(() => this.ankiInvoke('deckNames', {})); + async getDeckNames() { + await this.checkVersion(); + return await this.ankiInvoke('deckNames'); } - getModelNames() { - return this.checkVersion().then(() => this.ankiInvoke('modelNames', {})); + async getModelNames() { + await this.checkVersion(); + return await this.ankiInvoke('modelNames'); } - getModelFieldNames(modelName) { - return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName})); + async getModelFieldNames(modelName) { + await this.checkVersion(); + return await this.ankiInvoke('modelFieldNames', {modelName}); } - guiBrowse(query) { - return this.checkVersion().then(() => this.ankiInvoke('guiBrowse', {query})); + async guiBrowse(query) { + await this.checkVersion(); + return await this.ankiInvoke('guiBrowse', {query}); } - checkVersion() { - if (this.localVersion === this.remoteVersion) { - return Promise.resolve(true); - } - - return this.ankiInvoke('version', {}, null).then(version => { - this.remoteVersion = version; + async checkVersion() { + if (this.remoteVersion < this.localVersion) { + this.remoteVersion = await this.ankiInvoke('version'); if (this.remoteVersion < this.localVersion) { return Promise.reject('extension and plugin versions incompatible'); } - }); + } } - ankiInvoke(action, params, pool) { - return new Promise((resolve, reject) => { - if (pool && this.asyncPools.hasOwnProperty(pool)) { - this.asyncPools[pool].abort(); - } - - const xhr = new XMLHttpRequest(); - xhr.addEventListener('loadend', () => { - if (pool) { - delete this.asyncPools[pool]; - } - - if (xhr.responseText) { - resolve(JSON.parse(xhr.responseText)); - } else { - reject('unable to connect to plugin'); - } - }); - - xhr.open('POST', this.server); - xhr.send(JSON.stringify({action, params})); - }); + ankiInvoke(action, params) { + return jsonRequest(this.server, 'POST', {action, params, version: this.localVersion}); } } diff --git a/ext/bg/js/anki-null.js b/ext/bg/js/anki-null.js index 8dad6915..d82f0e68 100644 --- a/ext/bg/js/anki-null.js +++ b/ext/bg/js/anki-null.js @@ -18,27 +18,27 @@ class AnkiNull { - addNote(note) { - return Promise.reject('unsupported action'); + async addNote(note) { + return null; } - canAddNotes(notes) { - return Promise.resolve([]); + async canAddNotes(notes) { + return []; } - getDeckNames() { - return Promise.resolve([]); + async getDeckNames() { + return []; } - getModelNames() { - return Promise.resolve([]); + async getModelNames() { + return []; } - getModelFieldNames(modelName) { - return Promise.resolve([]); + async getModelFieldNames(modelName) { + return []; } - guiBrowse(query) { - return Promise.resolve([]); + async guiBrowse(query) { + return []; } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index aa11ea63..9232e529 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -31,7 +31,7 @@ class Translator { if (!this.deinflector) { const url = chrome.extension.getURL('/bg/lang/deinflect.json'); - const reasons = await Translator.loadRules(url); + const reasons = await jsonRequest(url, 'GET'); this.deinflector = new Deinflector(reasons); } } @@ -124,22 +124,4 @@ class Translator { return definitions; } - - static loadRules(url) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.overrideMimeType('application/json'); - xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); - xhr.open('GET', url); - xhr.send(); - }).then(responseText => { - try { - return JSON.parse(responseText); - } - catch (e) { - return Promise.reject('invalid JSON response'); - } - }); - } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index b8a60217..4f907923 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -428,6 +428,31 @@ function dictFieldFormat(field, definition, mode, options) { return field; } +/* + * JSON + */ + +function jsonRequest(url, action, params) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.open(action, url); + if (params) { + xhr.send(JSON.stringify(params)); + } else { + xhr.send(); + } + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } + }); +} /* * Helpers -- cgit v1.2.3 From fe137e94c92cf0366735936b6ac82dc147b1ad33 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 19 Jul 2017 21:28:09 -0700 Subject: cleanup --- ext/bg/background.html | 1 + ext/bg/js/anki-connect.js | 2 +- ext/bg/js/translator.js | 2 +- ext/bg/js/util.js | 40 ---------------------------------------- ext/bg/js/yomichan.js | 8 ++++++++ ext/bg/popup.html | 1 + ext/bg/search.html | 1 + ext/bg/settings.html | 1 + ext/mixed/js/request.js | 40 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 54 insertions(+), 42 deletions(-) create mode 100644 ext/mixed/js/request.js (limited to 'ext/bg/js/translator.js') diff --git a/ext/bg/background.html b/ext/bg/background.html index 61bc17a0..7d352561 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -14,6 +14,7 @@ + diff --git a/ext/bg/js/anki-connect.js b/ext/bg/js/anki-connect.js index a4d8ba3f..567e8d3f 100644 --- a/ext/bg/js/anki-connect.js +++ b/ext/bg/js/anki-connect.js @@ -64,6 +64,6 @@ class AnkiConnect { } ankiInvoke(action, params) { - return jsonRequest(this.server, 'POST', {action, params, version: this.localVersion}); + return requestJson(this.server, 'POST', {action, params, version: this.localVersion}); } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 9232e529..1be485c7 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -31,7 +31,7 @@ class Translator { if (!this.deinflector) { const url = chrome.extension.getURL('/bg/lang/deinflect.json'); - const reasons = await jsonRequest(url, 'GET'); + const reasons = await requestJson(url, 'GET'); this.deinflector = new Deinflector(reasons); } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 6e86c2a6..c7ebbb0e 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -17,19 +17,6 @@ */ -/* - * Promise - */ - -function promiseCallback(promise, callback) { - return promise.then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); -} - - /* * Commands */ @@ -71,30 +58,3 @@ function fgBroadcast(action, params) { function fgOptionsSet(options) { fgBroadcast('optionsSet', options); } - - -/* - * JSON - */ - -function jsonRequest(url, action, params) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.overrideMimeType('application/json'); - xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); - xhr.open(action, url); - if (params) { - xhr.send(JSON.stringify(params)); - } else { - xhr.send(); - } - }).then(responseText => { - try { - return JSON.parse(responseText); - } - catch (e) { - return Promise.reject('invalid JSON response'); - } - }); -} diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index acc560ce..eb083396 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -195,6 +195,14 @@ window.yomichan = new class { } onMessage({action, params}, sender, callback) { + const promiseCallback = (promise, callback) => { + return promise.then(result => { + callback({result}); + }).catch(error => { + callback({error}); + }); + }; + const handlers = { optionsGet: ({callback}) => { promiseCallback(optionsLoad(), callback); diff --git a/ext/bg/popup.html b/ext/bg/popup.html index db9097bd..b3d38533 100644 --- a/ext/bg/popup.html +++ b/ext/bg/popup.html @@ -32,6 +32,7 @@ + diff --git a/ext/bg/search.html b/ext/bg/search.html index b30b8910..45603f17 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -35,6 +35,7 @@ + diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 9b21b4d8..4c7198c3 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -280,6 +280,7 @@ + diff --git a/ext/mixed/js/request.js b/ext/mixed/js/request.js new file mode 100644 index 00000000..94fd135a --- /dev/null +++ b/ext/mixed/js/request.js @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2017 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function requestJson(url, action, params) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.open(action, url); + if (params) { + xhr.send(JSON.stringify(params)); + } else { + xhr.send(); + } + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } + }); +} -- cgit v1.2.3