summaryrefslogtreecommitdiff
path: root/ext/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
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')
-rw-r--r--ext/js/background/backend.js3
-rw-r--r--ext/js/data/options-util.js16
-rw-r--r--ext/js/display/display-audio.js7
-rw-r--r--ext/js/display/display.js4
-rw-r--r--ext/js/media/audio-downloader.js31
5 files changed, 38 insertions, 23 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index 2368b5d0..ba310d93 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -1742,7 +1742,7 @@ class Backend {
return null;
}
- const {sources, preferredAudioIndex, customSourceUrl, customSourceType} = details;
+ const {sources, preferredAudioIndex, customSourceUrl} = details;
let data;
let contentType;
try {
@@ -1754,7 +1754,6 @@ class Backend {
{
textToSpeechVoice: null,
customSourceUrl,
- customSourceType,
binary: true,
disableCache: true
}
diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js
index 740afa76..89d50903 100644
--- a/ext/js/data/options-util.js
+++ b/ext/js/data/options-util.js
@@ -817,11 +817,25 @@ class OptionsUtil {
// Version 12 changes:
// Changed sentenceParsing.enableTerminationCharacters to sentenceParsing.terminationCharacterMode.
// Added {search-query} field marker.
+ // Updated audio.sources[] to change 'custom' into 'custom-json'.
+ // Removed audio.customSourceType.
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v12.handlebars');
for (const profile of options.profiles) {
- const {sentenceParsing} = profile.options;
+ const {sentenceParsing, audio} = profile.options;
+
sentenceParsing.terminationCharacterMode = sentenceParsing.enableTerminationCharacters ? 'custom' : 'newlines';
delete sentenceParsing.enableTerminationCharacters;
+
+ const {sources, customSourceType} = audio;
+ audio.sources = sources.map((type) => {
+ switch (type) {
+ case 'custom':
+ return (customSourceType === 'json' ? 'custom-json' : 'custom');
+ default:
+ return type;
+ }
+ });
+ delete audio.customSourceType;
}
return options;
}
diff --git a/ext/js/display/display-audio.js b/ext/js/display/display-audio.js
index d8553547..34e74004 100644
--- a/ext/js/display/display-audio.js
+++ b/ext/js/display/display-audio.js
@@ -116,7 +116,7 @@ class DisplayAudio {
const {term, reading} = headword;
const audioOptions = this._getAudioOptions();
- const {textToSpeechVoice, customSourceUrl, customSourceType, volume} = audioOptions;
+ const {textToSpeechVoice, customSourceUrl, volume} = audioOptions;
if (!Array.isArray(sources)) {
({sources} = audioOptions);
}
@@ -131,7 +131,7 @@ class DisplayAudio {
let audio;
let title;
let source = null;
- const info = await this._createTermAudio(sources, sourceDetailsMap, term, reading, {textToSpeechVoice, customSourceUrl, customSourceType});
+ const info = await this._createTermAudio(sources, sourceDetailsMap, term, reading, {textToSpeechVoice, customSourceUrl});
const valid = (info !== null);
if (valid) {
({audio, source} = info);
@@ -518,7 +518,8 @@ class DisplayAudio {
['jisho', 'Jisho.org', true],
['text-to-speech', 'Text-to-speech', ttsSupported],
['text-to-speech-reading', 'Text-to-speech (Kana reading)', ttsSupported],
- ['custom', 'Custom', customSupported]
+ ['custom', 'Custom URL', customSupported],
+ ['custom-json', 'Custom URL (JSON)', customSupported]
];
const results = [];
diff --git a/ext/js/display/display.js b/ext/js/display/display.js
index bb089047..1c4602c5 100644
--- a/ext/js/display/display.js
+++ b/ext/js/display/display.js
@@ -1554,7 +1554,7 @@ class Display extends EventDispatcher {
async _injectAnkiNoteMedia(dictionaryEntry, options, fields) {
const {
anki: {screenshot: {format, quality}},
- audio: {sources, customSourceUrl, customSourceType}
+ audio: {sources, customSourceUrl}
} = options;
const timestamp = Date.now();
@@ -1570,7 +1570,7 @@ class Display extends EventDispatcher {
sources2 = [primaryCardAudio.source];
preferredAudioIndex = primaryCardAudio.index;
}
- audioDetails = {sources: sources2, preferredAudioIndex, customSourceUrl, customSourceType};
+ audioDetails = {sources: sources2, preferredAudioIndex, customSourceUrl};
}
const screenshotDetails = (
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',