From 0232325f960b1fbe41e03f74a46fb18a7abf33f7 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 26 May 2021 20:38:15 -0400 Subject: [PATCH] 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 --- ext/data/schemas/options-schema.json | 47 +++++++++++++--------------- ext/js/background/backend.js | 3 +- ext/js/data/options-util.js | 16 +++++++++- ext/js/display/display-audio.js | 7 +++-- ext/js/display/display.js | 4 +-- ext/js/media/audio-downloader.js | 31 +++++++++--------- ext/settings.html | 10 ++---- test/test-options-util.js | 1 - 8 files changed, 61 insertions(+), 58 deletions(-) diff --git a/ext/data/schemas/options-schema.json b/ext/data/schemas/options-schema.json index 3f5bd0c7..9afad1e3 100644 --- a/ext/data/schemas/options-schema.json +++ b/ext/data/schemas/options-schema.json @@ -341,36 +341,17 @@ "type": "object", "required": [ "enabled", - "sources", "volume", "autoPlay", "customSourceUrl", - "customSourceType", - "textToSpeechVoice" + "textToSpeechVoice", + "sources" ], "properties": { "enabled": { "type": "boolean", "default": true }, - "sources": { - "type": "array", - "items": { - "type": "string", - "enum": [ - "jpod101", - "jpod101-alternate", - "jisho", - "text-to-speech", - "text-to-speech-reading", - "custom" - ], - "default": "jpod101" - }, - "default": [ - "jpod101" - ] - }, "volume": { "type": "number", "minimum": 0, @@ -385,14 +366,28 @@ "type": "string", "default": "" }, - "customSourceType": { - "type": "string", - "enum": ["audio", "json"], - "default": "audio" - }, "textToSpeechVoice": { "type": "string", "default": "" + }, + "sources": { + "type": "array", + "items": { + "type": "string", + "enum": [ + "jpod101", + "jpod101-alternate", + "jisho", + "text-to-speech", + "text-to-speech-reading", + "custom", + "custom-json" + ], + "default": "jpod101" + }, + "default": [ + "jpod101" + ] } } }, 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', diff --git a/ext/settings.html b/ext/settings.html index aade3345..f9a2b04a 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -2398,13 +2398,6 @@
-
-
Type
- -
URL
@@ -2483,7 +2476,8 @@ - + +
diff --git a/test/test-options-util.js b/test/test-options-util.js index 7e11aad1..80f935d1 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -310,7 +310,6 @@ function createProfileOptionsUpdatedTestData1() { volume: 100, autoPlay: false, customSourceUrl: 'http://localhost/audio.mp3?term={term}&reading={reading}', - customSourceType: 'audio', textToSpeechVoice: '' }, scanning: {