summaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-18 22:01:08 -0500
committerGitHub <noreply@github.com>2021-01-18 22:01:08 -0500
commit21fce9f3d98e4381f8813cf9c63410ca1dbd7f91 (patch)
tree124a510e4f208bbd7516b2eae98454400e9b4c54 /ext/bg
parent85c723b85f47ff3048ba7aca46a532aa1bc44064 (diff)
Audio system refactoring (#1275)
* Simplify details * Simplify audio creation * Return an array of sources instead of a single item * Use sourceIndex instead of index * Rename APIs * Return more info about the source * Return source instead of sourceIndex
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/audio-downloader.js41
-rw-r--r--ext/bg/js/backend.js8
2 files changed, 24 insertions, 25 deletions
diff --git a/ext/bg/js/audio-downloader.js b/ext/bg/js/audio-downloader.js
index 19e9c368..171f5944 100644
--- a/ext/bg/js/audio-downloader.js
+++ b/ext/bg/js/audio-downloader.js
@@ -34,7 +34,7 @@ class AudioDownloader {
]);
}
- async getInfo(source, expression, reading, details) {
+ async getExpressionAudioInfoList(source, expression, reading, details) {
const handler = this._getInfoHandlers.get(source);
if (typeof handler === 'function') {
try {
@@ -43,23 +43,22 @@ class AudioDownloader {
// NOP
}
}
- return null;
+ return [];
}
- async downloadAudio(sources, expression, reading, details) {
+ async downloadExpressionAudio(sources, expression, reading, details) {
for (const source of sources) {
- const info = await this.getInfo(source, expression, reading, details);
- if (info === null) { continue; }
-
- switch (info.type) {
- case 'url':
- try {
- const {details: {url}} = info;
- return await this._downloadAudioFromUrl(url);
- } catch (e) {
- // NOP
- }
- break;
+ const infoList = await this.getExpressionAudioInfoList(source, expression, reading, details);
+ for (const info of infoList) {
+ switch (info.type) {
+ case 'url':
+ try {
+ return await this._downloadAudioFromUrl(info.url);
+ } catch (e) {
+ // NOP
+ }
+ break;
+ }
}
}
@@ -90,7 +89,7 @@ class AudioDownloader {
}
const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
- return {type: 'url', details: {url}};
+ return [{type: 'url', url}];
}
async _getInfoJpod101Alternate(expression, reading) {
@@ -128,7 +127,7 @@ class AudioDownloader {
const htmlReading = dom.getTextContent(htmlReadings[0]);
if (htmlReading && (!reading || reading === htmlReading)) {
url = this._normalizeUrl(url, response.url);
- return {type: 'url', details: {url}};
+ return [{type: 'url', url}];
}
} catch (e) {
// NOP
@@ -159,7 +158,7 @@ class AudioDownloader {
let url = dom.getAttribute(source, 'src');
if (url !== null) {
url = this._normalizeUrl(url, response.url);
- return {type: 'url', details: {url}};
+ return [{type: 'url', url}];
}
}
}
@@ -174,14 +173,14 @@ class AudioDownloader {
if (!textToSpeechVoice) {
throw new Error('No voice');
}
- return {type: 'tts', details: {text: expression, voice: textToSpeechVoice}};
+ return [{type: 'tts', text: expression, voice: textToSpeechVoice}];
}
async _getInfoTextToSpeechReading(expression, reading, {textToSpeechVoice}) {
if (!textToSpeechVoice) {
throw new Error('No voice');
}
- return {type: 'tts', details: {text: reading || expression, voice: textToSpeechVoice}};
+ return [{type: 'tts', text: reading || expression, voice: textToSpeechVoice}];
}
async _getInfoCustom(expression, reading, {customSourceUrl}) {
@@ -190,7 +189,7 @@ class AudioDownloader {
}
const data = {expression, reading};
const url = customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (Object.prototype.hasOwnProperty.call(data, m1) ? `${data[m1]}` : m0));
- return {type: 'url', details: {url}};
+ return [{type: 'url', url}];
}
async _downloadAudioFromUrl(url) {
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 433f5a93..2949cbed 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -99,7 +99,7 @@ class Backend {
['noteView', {async: true, contentScript: true, handler: this._onApiNoteView.bind(this)}],
['suspendAnkiCardsForNote', {async: true, contentScript: true, handler: this._onApiSuspendAnkiCardsForNote.bind(this)}],
['commandExec', {async: false, contentScript: true, handler: this._onApiCommandExec.bind(this)}],
- ['getDefinitionAudioInfo', {async: true, contentScript: true, handler: this._onApiGetDefinitionAudioInfo.bind(this)}],
+ ['getExpressionAudioInfoList', {async: true, contentScript: true, handler: this._onApiGetExpressionAudioInfoList.bind(this)}],
['downloadDefinitionAudio', {async: true, contentScript: true, handler: this._onApiDownloadDefinitionAudio.bind(this)}],
['screenshotGet', {async: true, contentScript: true, handler: this._onApiScreenshotGet.bind(this)}],
['sendMessageToFrame', {async: false, contentScript: true, handler: this._onApiSendMessageToFrame.bind(this)}],
@@ -500,8 +500,8 @@ class Backend {
return this._runCommand(command, params);
}
- async _onApiGetDefinitionAudioInfo({source, expression, reading, details}) {
- return await this._audioDownloader.getInfo(source, expression, reading, details);
+ async _onApiGetExpressionAudioInfoList({source, expression, reading, details}) {
+ return await this._audioDownloader.getExpressionAudioInfoList(source, expression, reading, details);
}
async _onApiDownloadDefinitionAudio({sources, expression, reading, details}) {
@@ -1528,7 +1528,7 @@ class Backend {
}
async _downloadDefinitionAudio(sources, expression, reading, details) {
- return await this._audioDownloader.downloadAudio(sources, expression, reading, details);
+ return await this._audioDownloader.downloadExpressionAudio(sources, expression, reading, details);
}
async _injectAnkNoteMedia(ankiConnect, timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails) {