summaryrefslogtreecommitdiff
path: root/ext/js/media/audio-downloader.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-05-26 20:38:15 -0400
committerGitHub <noreply@github.com>2021-05-26 20:38:15 -0400
commit0232325f960b1fbe41e03f74a46fb18a7abf33f7 (patch)
tree4b9552f3b0db91daab394c4047add01d32e17d44 /ext/js/media/audio-downloader.js
parent900bceda47e4c736e70a66e66cfce719b4c6d203 (diff)
Refactor json audio source (#1711)
* Move sources to the end of audio options object * Add custom-json audio source type * Add support for downloading custom-json * Remove customSourceType
Diffstat (limited to 'ext/js/media/audio-downloader.js')
-rw-r--r--ext/js/media/audio-downloader.js31
1 files changed, 16 insertions, 15 deletions
diff --git a/ext/js/media/audio-downloader.js b/ext/js/media/audio-downloader.js
index 577d1c1b..70e99f11 100644
--- a/ext/js/media/audio-downloader.js
+++ b/ext/js/media/audio-downloader.js
@@ -33,7 +33,8 @@ class AudioDownloader {
['jisho', this._getInfoJisho.bind(this)],
['text-to-speech', this._getInfoTextToSpeech.bind(this)],
['text-to-speech-reading', this._getInfoTextToSpeechReading.bind(this)],
- ['custom', this._getInfoCustom.bind(this)]
+ ['custom', this._getInfoCustom.bind(this)],
+ ['custom-json', this._getInfoCustomJson.bind(this)]
]);
}
@@ -191,22 +192,14 @@ class AudioDownloader {
return [{type: 'tts', text: reading, voice: textToSpeechVoice}];
}
- async _getInfoCustom(term, reading, {customSourceUrl, customSourceType}) {
- if (typeof customSourceUrl !== 'string') {
- throw new Error('No custom URL defined');
- }
- const data = {term, reading};
- const url = customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (Object.prototype.hasOwnProperty.call(data, m1) ? `${data[m1]}` : m0));
-
- switch (customSourceType) {
- case 'json':
- return await this._getInfoCustomJson(url);
- default:
- return [{type: 'url', url}];
- }
+ async _getInfoCustom(term, reading, {customSourceUrl}) {
+ const url = this._getCustomUrl(term, reading, customSourceUrl);
+ return [{type: 'url', url}];
}
- async _getInfoCustomJson(url) {
+ async _getInfoCustomJson(term, reading, {customSourceUrl}) {
+ const url = this._getCustomUrl(term, reading, customSourceUrl);
+
const response = await this._requestBuilder.fetchAnonymous(url, {
method: 'GET',
mode: 'cors',
@@ -237,6 +230,14 @@ class AudioDownloader {
return results;
}
+ _getCustomUrl(term, reading, customSourceUrl) {
+ if (typeof customSourceUrl !== 'string') {
+ throw new Error('No custom URL defined');
+ }
+ const data = {term, reading};
+ return customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (Object.prototype.hasOwnProperty.call(data, m1) ? `${data[m1]}` : m0));
+ }
+
async _downloadAudioFromUrl(url, source) {
const response = await this._requestBuilder.fetchAnonymous(url, {
method: 'GET',