diff options
author | Alex Yatskov <alex@foosoft.net> | 2020-01-04 11:54:54 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2020-01-04 11:54:54 -0800 |
commit | 2a12036ca305044291f1f4105d6a8d249848b210 (patch) | |
tree | 5cfd4a3d837bf99730233a805d72395c8c61fc07 /ext/bg/js/audio.js | |
parent | 9105cb5618cfdd14c2bc37cd22db2b360fe8cd52 (diff) | |
parent | 174b92366577b0a638003b15e2d73fdc91cd62c3 (diff) |
Merge branch 'master' into testing
Diffstat (limited to 'ext/bg/js/audio.js')
-rw-r--r-- | ext/bg/js/audio.js | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/ext/bg/js/audio.js b/ext/bg/js/audio.js index dc0ba5eb..36ac413b 100644 --- a/ext/bg/js/audio.js +++ b/ext/bg/js/audio.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 Alex Yatskov <alex@foosoft.net> + * Copyright (C) 2017-2020 Alex Yatskov <alex@foosoft.net> * Author: Alex Yatskov <alex@foosoft.net> * * This program is free software: you can redistribute it and/or modify @@ -13,12 +13,12 @@ * 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 <http://www.gnu.org/licenses/>. + * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -const audioUrlBuilders = { - 'jpod101': async (definition) => { +const audioUrlBuilders = new Map([ + ['jpod101', async (definition) => { let kana = definition.reading; let kanji = definition.expression; @@ -36,8 +36,8 @@ const audioUrlBuilders = { } return `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; - }, - 'jpod101-alternate': async (definition) => { + }], + ['jpod101-alternate', async (definition) => { const response = await new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); @@ -61,8 +61,8 @@ const audioUrlBuilders = { } throw new Error('Failed to find audio URL'); - }, - 'jisho': async (definition) => { + }], + ['jisho', async (definition) => { const response = await new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', `https://jisho.org/search/${definition.expression}`); @@ -85,37 +85,34 @@ const audioUrlBuilders = { } throw new Error('Failed to find audio URL'); - }, - 'text-to-speech': async (definition, optionsContext) => { - const options = await apiOptionsGet(optionsContext); + }], + ['text-to-speech', async (definition, options) => { const voiceURI = options.audio.textToSpeechVoice; if (!voiceURI) { throw new Error('No voice'); } return `tts:?text=${encodeURIComponent(definition.expression)}&voice=${encodeURIComponent(voiceURI)}`; - }, - 'text-to-speech-reading': async (definition, optionsContext) => { - const options = await apiOptionsGet(optionsContext); + }], + ['text-to-speech-reading', async (definition, options) => { const voiceURI = options.audio.textToSpeechVoice; if (!voiceURI) { throw new Error('No voice'); } return `tts:?text=${encodeURIComponent(definition.reading || definition.expression)}&voice=${encodeURIComponent(voiceURI)}`; - }, - 'custom': async (definition, optionsContext) => { - const options = await apiOptionsGet(optionsContext); + }], + ['custom', async (definition, options) => { const customSourceUrl = options.audio.customSourceUrl; return customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (hasOwn(definition, m1) ? `${definition[m1]}` : m0)); - } -}; + }] +]); -async function audioGetUrl(definition, mode, optionsContext, download) { - if (hasOwn(audioUrlBuilders, mode)) { - const handler = audioUrlBuilders[mode]; +async function audioGetUrl(definition, mode, options, download) { + const handler = audioUrlBuilders.get(mode); + if (typeof handler === 'function') { try { - return await handler(definition, optionsContext, download); + return await handler(definition, options, download); } catch (e) { // NOP } |