Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Update audio downloading to support specifying an exact array index (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread authored Feb 16, 2021
1 parent 7027d53 commit f2a3872
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
9 changes: 3 additions & 6 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
{
Expand Down
2 changes: 1 addition & 1 deletion ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
7 changes: 5 additions & 2 deletions ext/js/media/audio-downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit f2a3872

Please sign in to comment.