Fix apiOptionsGet being used on the background page

This commit is contained in:
toasted-nutbread 2019-12-20 22:54:28 -05:00
parent ec8b805e8f
commit 7b9731e616
2 changed files with 20 additions and 22 deletions

View File

@ -17,8 +17,8 @@
*/ */
const audioUrlBuilders = { const audioUrlBuilders = new Map([
'jpod101': async (definition) => { ['jpod101', async (definition) => {
let kana = definition.reading; let kana = definition.reading;
let kanji = definition.expression; let kanji = definition.expression;
@ -36,8 +36,8 @@ const audioUrlBuilders = {
} }
return `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; 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 response = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); 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'); throw new Error('Failed to find audio URL');
}, }],
'jisho': async (definition) => { ['jisho', async (definition) => {
const response = await new Promise((resolve, reject) => { const response = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('GET', `https://jisho.org/search/${definition.expression}`); xhr.open('GET', `https://jisho.org/search/${definition.expression}`);
@ -85,37 +85,34 @@ const audioUrlBuilders = {
} }
throw new Error('Failed to find audio URL'); throw new Error('Failed to find audio URL');
}, }],
'text-to-speech': async (definition, optionsContext) => { ['text-to-speech', async (definition, options) => {
const options = await apiOptionsGet(optionsContext);
const voiceURI = options.audio.textToSpeechVoice; const voiceURI = options.audio.textToSpeechVoice;
if (!voiceURI) { if (!voiceURI) {
throw new Error('No voice'); throw new Error('No voice');
} }
return `tts:?text=${encodeURIComponent(definition.expression)}&voice=${encodeURIComponent(voiceURI)}`; return `tts:?text=${encodeURIComponent(definition.expression)}&voice=${encodeURIComponent(voiceURI)}`;
}, }],
'text-to-speech-reading': async (definition, optionsContext) => { ['text-to-speech-reading', async (definition, options) => {
const options = await apiOptionsGet(optionsContext);
const voiceURI = options.audio.textToSpeechVoice; const voiceURI = options.audio.textToSpeechVoice;
if (!voiceURI) { if (!voiceURI) {
throw new Error('No voice'); throw new Error('No voice');
} }
return `tts:?text=${encodeURIComponent(definition.reading || definition.expression)}&voice=${encodeURIComponent(voiceURI)}`; return `tts:?text=${encodeURIComponent(definition.reading || definition.expression)}&voice=${encodeURIComponent(voiceURI)}`;
}, }],
'custom': async (definition, optionsContext) => { ['custom', async (definition, options) => {
const options = await apiOptionsGet(optionsContext);
const customSourceUrl = options.audio.customSourceUrl; const customSourceUrl = options.audio.customSourceUrl;
return customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (hasOwn(definition, m1) ? `${definition[m1]}` : m0)); return customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (hasOwn(definition, m1) ? `${definition[m1]}` : m0));
} }]
}; ]);
async function audioGetUrl(definition, mode, optionsContext, download) { async function audioGetUrl(definition, mode, options, download) {
if (hasOwn(audioUrlBuilders, mode)) { const handler = audioUrlBuilders.get(mode);
const handler = audioUrlBuilders[mode]; if (typeof handler === 'function') {
try { try {
return await handler(definition, optionsContext, download); return await handler(definition, options, download);
} catch (e) { } catch (e) {
// NOP // NOP
} }

View File

@ -406,7 +406,8 @@ class Backend {
} }
async _onApiAudioGetUrl({definition, source, optionsContext}) { async _onApiAudioGetUrl({definition, source, optionsContext}) {
return audioGetUrl(definition, source, optionsContext); const options = await this.getOptions(optionsContext);
return await audioGetUrl(definition, source, options);
} }
_onApiScreenshotGet({options}, sender) { _onApiScreenshotGet({options}, sender) {