aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-10 11:57:38 -0400
committerGitHub <noreply@github.com>2020-09-10 11:57:38 -0400
commit35abd517b933036b95bb58b463a48edd1c985bb1 (patch)
tree34a31589727c6e04659cc7195b5c27b61df5036e /ext/bg
parent17ebe6a754d53fad97ab607f17e2bba8d8565361 (diff)
AudioUriBuilder simplification (#799)
* Rename variables to disambiguate * Update handler argument convention * Update getUri argument convention * Change _getAudioUri argument convention * Change getDefinitionAudio argument convention * Add api.getDefinitionAudio definition
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/anki-note-builder.js10
-rw-r--r--ext/bg/js/audio-uri-builder.js41
-rw-r--r--ext/bg/js/backend.js12
3 files changed, 32 insertions, 31 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index 72e3b337..a558db92 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -124,15 +124,16 @@ class AnkiNoteBuilder {
try {
const {sources, customSourceUrl} = details;
const expressions = definition.expressions;
- const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition;
+ const {expression, reading} = Array.isArray(expressions) ? expressions[0] : definition;
- let fileName = this._createInjectedAudioFileName(audioSourceDefinition);
+ let fileName = this._createInjectedAudioFileName(expression, reading);
if (fileName === null) { return; }
fileName = this._replaceInvalidFileNameCharacters(fileName);
const {audio: data} = await this._getDefinitionAudio(
- audioSourceDefinition,
sources,
+ expression,
+ reading,
{
textToSpeechVoice: null,
customSourceUrl,
@@ -199,8 +200,7 @@ class AnkiNoteBuilder {
}
}
- _createInjectedAudioFileName(definition) {
- const {reading, expression} = definition;
+ _createInjectedAudioFileName(expression, reading) {
if (!reading && !expression) { return null; }
let fileName = 'yomichan';
diff --git a/ext/bg/js/audio-uri-builder.js b/ext/bg/js/audio-uri-builder.js
index 1c64e008..78ea9caa 100644
--- a/ext/bg/js/audio-uri-builder.js
+++ b/ext/bg/js/audio-uri-builder.js
@@ -51,11 +51,11 @@ class AudioUriBuilder {
return url;
}
- async getUri(definition, source, details) {
+ async getUri(source, expression, reading, details) {
const handler = this._getUrlHandlers.get(source);
if (typeof handler === 'function') {
try {
- return await handler(definition, details);
+ return await handler(expression, reading, details);
} catch (e) {
// NOP
}
@@ -63,9 +63,9 @@ class AudioUriBuilder {
return null;
}
- async _getUriJpod101(definition) {
- let kana = definition.reading;
- let kanji = definition.expression;
+ async _getUriJpod101(expression, reading) {
+ let kana = reading;
+ let kanji = expression;
if (!kana && jp.isStringEntirelyKana(kanji)) {
kana = kanji;
@@ -83,9 +83,9 @@ class AudioUriBuilder {
return `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
}
- async _getUriJpod101Alternate(definition) {
+ async _getUriJpod101Alternate(expression, reading) {
const fetchUrl = 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post';
- const data = `post=dictionary_reference&match_type=exact&search_query=${encodeURIComponent(definition.expression)}&vulgar=true`;
+ const data = `post=dictionary_reference&match_type=exact&search_query=${encodeURIComponent(expression)}&vulgar=true`;
const response = await this._requestBuilder.fetchAnonymous(fetchUrl, {
method: 'POST',
mode: 'cors',
@@ -112,11 +112,11 @@ class AudioUriBuilder {
const url = dom.getAttribute(source, 'src');
if (url === null) { continue; }
- const readings = dom.getElementsByClassName('dc-vocab_kana');
- if (readings.length === 0) { continue; }
+ const htmlReadings = dom.getElementsByClassName('dc-vocab_kana');
+ if (htmlReadings.length === 0) { continue; }
- const reading = dom.getTextContent(readings[0]);
- if (reading && (!definition.reading || definition.reading === reading)) {
+ const htmlReading = dom.getTextContent(htmlReadings[0]);
+ if (htmlReading && (!reading || reading === htmlReading)) {
return this.normalizeUrl(url, 'https://www.japanesepod101.com', '/learningcenter/reference/');
}
} catch (e) {
@@ -127,8 +127,8 @@ class AudioUriBuilder {
throw new Error('Failed to find audio URL');
}
- async _getUriJisho(definition) {
- const fetchUrl = `https://jisho.org/search/${definition.expression}`;
+ async _getUriJisho(expression, reading) {
+ const fetchUrl = `https://jisho.org/search/${expression}`;
const response = await this._requestBuilder.fetchAnonymous(fetchUrl, {
method: 'GET',
mode: 'cors',
@@ -141,7 +141,7 @@ class AudioUriBuilder {
const dom = new SimpleDOMParser(responseText);
try {
- const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`);
+ const audio = dom.getElementById(`audio_${expression}:${reading}`);
if (audio !== null) {
const source = dom.getElementByTagName('source', audio);
if (source !== null) {
@@ -158,24 +158,25 @@ class AudioUriBuilder {
throw new Error('Failed to find audio URL');
}
- async _getUriTextToSpeech(definition, {textToSpeechVoice}) {
+ async _getUriTextToSpeech(expression, reading, {textToSpeechVoice}) {
if (!textToSpeechVoice) {
throw new Error('No voice');
}
- return `tts:?text=${encodeURIComponent(definition.expression)}&voice=${encodeURIComponent(textToSpeechVoice)}`;
+ return `tts:?text=${encodeURIComponent(expression)}&voice=${encodeURIComponent(textToSpeechVoice)}`;
}
- async _getUriTextToSpeechReading(definition, {textToSpeechVoice}) {
+ async _getUriTextToSpeechReading(expression, reading, {textToSpeechVoice}) {
if (!textToSpeechVoice) {
throw new Error('No voice');
}
- return `tts:?text=${encodeURIComponent(definition.reading || definition.expression)}&voice=${encodeURIComponent(textToSpeechVoice)}`;
+ return `tts:?text=${encodeURIComponent(reading || expression)}&voice=${encodeURIComponent(textToSpeechVoice)}`;
}
- async _getUriCustom(definition, {customSourceUrl}) {
+ async _getUriCustom(expression, reading, {customSourceUrl}) {
if (typeof customSourceUrl !== 'string') {
throw new Error('No custom URL defined');
}
- return customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (hasOwn(definition, m1) ? `${definition[m1]}` : m0));
+ const data = {expression, reading};
+ return customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (hasOwn(data, m1) ? `${data[m1]}` : m0));
}
}
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 1bc831e9..37a5c77e 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -508,8 +508,8 @@ class Backend {
return this._runCommand(command, params);
}
- async _onApiAudioGetUri({definition, source, details}) {
- return await this._audioUriBuilder.getUri(definition, source, details);
+ async _onApiAudioGetUri({source, expression, reading, details}) {
+ return await this._audioUriBuilder.getUri(source, expression, reading, details);
}
_onApiScreenshotGet({options}, sender) {
@@ -828,8 +828,8 @@ class Backend {
return (tab !== null);
}
- async _onApiGetDefinitionAudio({definition, sources, details}) {
- return this._getDefinitionAudio(definition, sources, details);
+ async _onApiGetDefinitionAudio({sources, expression, reading, details}) {
+ return this._getDefinitionAudio(sources, expression, reading, details);
}
// Command handlers
@@ -1637,7 +1637,7 @@ class Backend {
}
}
- async _getDefinitionAudio(definition, sources, details) {
- return await this._audioSystem.getDefinitionAudio(definition, sources, details);
+ async _getDefinitionAudio(sources, expression, reading, details) {
+ return await this._audioSystem.getDefinitionAudio(sources, expression, reading, details);
}
}