From f2a387237bac02d93d1664ed7acb6a10108915b6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 15 Feb 2021 20:47:35 -0500 Subject: [PATCH] Update audio downloading to support specifying an exact array index (#1405) --- ext/js/background/backend.js | 9 +++------ ext/js/display/display.js | 2 +- ext/js/media/audio-downloader.js | 7 +++++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index d88c5853..2a3ccbc2 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1636,10 +1636,6 @@ class Backend { } } - async _downloadDefinitionAudio(sources, expression, reading, details) { - return await this._audioDownloader.downloadExpressionAudio(sources, expression, reading, details); - } - async _injectAnkNoteMedia(ankiConnect, timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails) { let screenshotFileName = null; let clipboardImageFileName = null; @@ -1701,12 +1697,13 @@ class Backend { return null; } - const {sources, customSourceUrl, customSourceType} = details; + const {sources, preferredAudioIndex, customSourceUrl, customSourceType} = details; let data; let contentType; try { - ({data, contentType} = await this._downloadDefinitionAudio( + ({data, contentType} = await this._audioDownloader.downloadExpressionAudio( sources, + preferredAudioIndex, expression, reading, { diff --git a/ext/js/display/display.js b/ext/js/display/display.js index a7c45f19..6a2a3766 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -1487,7 +1487,7 @@ class Display extends EventDispatcher { const timestamp = Date.now(); const definitionDetails = this._getDefinitionDetailsForNote(definition); - const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl, customSourceType} : null); + const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, preferredAudioIndex: null, customSourceUrl, customSourceType} : null); const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {tabId: this._contentOriginTabId, frameId: this._contentOriginFrameId, format, quality} : null); const clipboardDetails = { image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'), diff --git a/ext/js/media/audio-downloader.js b/ext/js/media/audio-downloader.js index 4e77419b..bb8d40a8 100644 --- a/ext/js/media/audio-downloader.js +++ b/ext/js/media/audio-downloader.js @@ -49,9 +49,12 @@ class AudioDownloader { return []; } - async downloadExpressionAudio(sources, expression, reading, details) { + async downloadExpressionAudio(sources, preferredAudioIndex, expression, reading, details) { for (const source of sources) { - const infoList = await this.getExpressionAudioInfoList(source, expression, reading, details); + let infoList = await this.getExpressionAudioInfoList(source, expression, reading, details); + if (typeof preferredAudioIndex === 'number') { + infoList = (preferredAudioIndex >= 0 && preferredAudioIndex < infoList.length ? [infoList[preferredAudioIndex]] : []); + } for (const info of infoList) { switch (info.type) { case 'url':