diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-05-26 20:38:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-26 20:38:15 -0400 |
commit | 0232325f960b1fbe41e03f74a46fb18a7abf33f7 (patch) | |
tree | 4b9552f3b0db91daab394c4047add01d32e17d44 /ext | |
parent | 900bceda47e4c736e70a66e66cfce719b4c6d203 (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')
-rw-r--r-- | ext/data/schemas/options-schema.json | 47 | ||||
-rw-r--r-- | ext/js/background/backend.js | 3 | ||||
-rw-r--r-- | ext/js/data/options-util.js | 16 | ||||
-rw-r--r-- | ext/js/display/display-audio.js | 7 | ||||
-rw-r--r-- | ext/js/display/display.js | 4 | ||||
-rw-r--r-- | ext/js/media/audio-downloader.js | 31 | ||||
-rw-r--r-- | ext/settings.html | 10 |
7 files changed, 61 insertions, 57 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 @@ -2399,13 +2399,6 @@ <div class="settings-item-right"> <div class="settings-item-group"> <div class="settings-item-group-item"> - <div class="settings-item-group-item-label">Type</div> - <select class="short-width short-height" data-setting="audio.customSourceType"> - <option value="audio">Audio</option> - <option value="json">JSON</option> - </select> - </div> - <div class="settings-item-group-item"> <div class="settings-item-group-item-label">URL</div> <input class="short-height" type="text" spellcheck="false" autocomplete="off" data-setting="audio.customSourceUrl" placeholder="None"> </div> @@ -2483,7 +2476,8 @@ <option value="jisho">Jisho.org</option> <option value="text-to-speech">Text-to-speech</option> <option value="text-to-speech-reading">Text-to-speech (Kana reading)</option> - <option value="custom">Custom</option> + <option value="custom">Custom URL</option> + <option value="custom-json">Custom URL (JSON)</option> </select> <button class="icon-button audio-source-menu-button" data-menu="audio-source-menu" data-menu-position="below left"><span class="icon-button-inner"><span class="icon" data-icon="kebab-menu"></span></span></button> </div></template> |