Move media injection functions into AnkiNoteBuilder
This commit is contained in:
parent
fa68a87736
commit
efcdff72a3
@ -84,6 +84,82 @@ class AnkiNoteBuilder {
|
||||
});
|
||||
}
|
||||
|
||||
async injectAudio(definition, fields, sources, audioSystem, optionsContext) {
|
||||
let usesAudio = false;
|
||||
for (const fieldValue of Object.values(fields)) {
|
||||
if (fieldValue.includes('{audio}')) {
|
||||
usesAudio = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!usesAudio) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const expressions = definition.expressions;
|
||||
const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition;
|
||||
|
||||
const {uri} = await audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext});
|
||||
const filename = this._createInjectedAudioFileName(audioSourceDefinition);
|
||||
if (filename !== null) {
|
||||
definition.audio = {url: uri, filename};
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async injectScreenshot(definition, fields, screenshot, anki) {
|
||||
let usesScreenshot = false;
|
||||
for (const fieldValue of Object.values(fields)) {
|
||||
if (fieldValue.includes('{screenshot}')) {
|
||||
usesScreenshot = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!usesScreenshot) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dateToString = (date) => {
|
||||
const year = date.getUTCFullYear();
|
||||
const month = date.getUTCMonth().toString().padStart(2, '0');
|
||||
const day = date.getUTCDate().toString().padStart(2, '0');
|
||||
const hours = date.getUTCHours().toString().padStart(2, '0');
|
||||
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
|
||||
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
|
||||
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
|
||||
};
|
||||
|
||||
const now = new Date(Date.now());
|
||||
const filename = `yomichan_browser_screenshot_${definition.reading}_${dateToString(now)}.${screenshot.format}`;
|
||||
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
|
||||
|
||||
try {
|
||||
await anki.storeMediaFile(filename, data);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
definition.screenshotFileName = filename;
|
||||
}
|
||||
|
||||
_createInjectedAudioFileName(definition) {
|
||||
const {reading, expression} = definition;
|
||||
if (!reading && !expression) { return null; }
|
||||
|
||||
let filename = 'yomichan';
|
||||
if (reading) { filename += `_${reading}`; }
|
||||
if (expression) { filename += `_${expression}`; }
|
||||
filename += '.mp3';
|
||||
return filename;
|
||||
}
|
||||
|
||||
static stringReplaceAsync(str, regex, replacer) {
|
||||
let match;
|
||||
let index = 0;
|
||||
|
@ -460,19 +460,21 @@ class Backend {
|
||||
const templates = this.defaultAnkiFieldTemplates;
|
||||
|
||||
if (mode !== 'kanji') {
|
||||
await this._audioInject(
|
||||
await this.ankiNoteBuilder.injectAudio(
|
||||
definition,
|
||||
options.anki.terms.fields,
|
||||
options.audio.sources,
|
||||
this.audioSystem,
|
||||
optionsContext
|
||||
);
|
||||
}
|
||||
|
||||
if (details && details.screenshot) {
|
||||
await this._injectScreenshot(
|
||||
await this.ankiNoteBuilder.injectScreenshot(
|
||||
definition,
|
||||
options.anki.terms.fields,
|
||||
details.screenshot
|
||||
details.screenshot,
|
||||
this.anki
|
||||
);
|
||||
}
|
||||
|
||||
@ -800,86 +802,10 @@ class Backend {
|
||||
return await this.audioUriBuilder.getUri(definition, source, options);
|
||||
}
|
||||
|
||||
async _audioInject(definition, fields, sources, optionsContext) {
|
||||
let usesAudio = false;
|
||||
for (const fieldValue of Object.values(fields)) {
|
||||
if (fieldValue.includes('{audio}')) {
|
||||
usesAudio = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!usesAudio) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const expressions = definition.expressions;
|
||||
const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition;
|
||||
|
||||
const {uri} = await this.audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext});
|
||||
const filename = this._createInjectedAudioFileName(audioSourceDefinition);
|
||||
if (filename !== null) {
|
||||
definition.audio = {url: uri, filename};
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async _injectScreenshot(definition, fields, screenshot) {
|
||||
let usesScreenshot = false;
|
||||
for (const fieldValue of Object.values(fields)) {
|
||||
if (fieldValue.includes('{screenshot}')) {
|
||||
usesScreenshot = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!usesScreenshot) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dateToString = (date) => {
|
||||
const year = date.getUTCFullYear();
|
||||
const month = date.getUTCMonth().toString().padStart(2, '0');
|
||||
const day = date.getUTCDate().toString().padStart(2, '0');
|
||||
const hours = date.getUTCHours().toString().padStart(2, '0');
|
||||
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
|
||||
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
|
||||
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
|
||||
};
|
||||
|
||||
const now = new Date(Date.now());
|
||||
const filename = `yomichan_browser_screenshot_${definition.reading}_${dateToString(now)}.${screenshot.format}`;
|
||||
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
|
||||
|
||||
try {
|
||||
await this.anki.storeMediaFile(filename, data);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
definition.screenshotFileName = filename;
|
||||
}
|
||||
|
||||
async _renderTemplate(template, data) {
|
||||
return handlebarsRenderDynamic(template, data);
|
||||
}
|
||||
|
||||
_createInjectedAudioFileName(definition) {
|
||||
const {reading, expression} = definition;
|
||||
if (!reading && !expression) { return null; }
|
||||
|
||||
let filename = 'yomichan';
|
||||
if (reading) { filename += `_${reading}`; }
|
||||
if (expression) { filename += `_${expression}`; }
|
||||
filename += '.mp3';
|
||||
return filename;
|
||||
}
|
||||
|
||||
static _getTabUrl(tab) {
|
||||
return new Promise((resolve) => {
|
||||
chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => {
|
||||
|
Loading…
Reference in New Issue
Block a user