Move apiDefinitionAdd implementation into Backend
This commit is contained in:
parent
7091c8c5c0
commit
c9cd29889d
@ -49,28 +49,8 @@ function apiKanjiFind(text, optionsContext) {
|
|||||||
return utilBackend()._onApiKanjiFind({text, optionsContext});
|
return utilBackend()._onApiKanjiFind({text, optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiDefinitionAdd(definition, mode, context, optionsContext) {
|
function apiDefinitionAdd(definition, mode, context, optionsContext) {
|
||||||
const options = await apiOptionsGet(optionsContext);
|
return utilBackend()._onApiDefinitionAdd({definition, mode, context, optionsContext});
|
||||||
|
|
||||||
if (mode !== 'kanji') {
|
|
||||||
await audioInject(
|
|
||||||
definition,
|
|
||||||
options.anki.terms.fields,
|
|
||||||
options.audio.sources,
|
|
||||||
optionsContext
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context && context.screenshot) {
|
|
||||||
await _apiInjectScreenshot(
|
|
||||||
definition,
|
|
||||||
options.anki.terms.fields,
|
|
||||||
context.screenshot
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const note = await dictNoteFormat(definition, mode, options);
|
|
||||||
return utilBackend().anki.addNote(note);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiDefinitionsAddable(definitions, modes, optionsContext) {
|
async function apiDefinitionsAddable(definitions, modes, optionsContext) {
|
||||||
@ -189,42 +169,6 @@ async function apiAudioGetUrl(definition, source, optionsContext) {
|
|||||||
return audioGetUrl(definition, source, optionsContext);
|
return audioGetUrl(definition, source, optionsContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _apiInjectScreenshot(definition, fields, screenshot) {
|
|
||||||
let usesScreenshot = false;
|
|
||||||
for (const name in fields) {
|
|
||||||
if (fields[name].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 utilBackend().anki.storeMediaFile(filename, data);
|
|
||||||
} catch (e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
definition.screenshotFileName = filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
function apiScreenshotGet(options, sender) {
|
function apiScreenshotGet(options, sender) {
|
||||||
if (!(sender && sender.tab)) {
|
if (!(sender && sender.tab)) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
|
@ -315,8 +315,28 @@ class Backend {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onApiDefinitionAdd({definition, mode, context, optionsContext}) {
|
async _onApiDefinitionAdd({definition, mode, context, optionsContext}) {
|
||||||
return apiDefinitionAdd(definition, mode, context, optionsContext);
|
const options = await this.getOptions(optionsContext);
|
||||||
|
|
||||||
|
if (mode !== 'kanji') {
|
||||||
|
await audioInject(
|
||||||
|
definition,
|
||||||
|
options.anki.terms.fields,
|
||||||
|
options.audio.sources,
|
||||||
|
optionsContext
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context && context.screenshot) {
|
||||||
|
await this._injectScreenshot(
|
||||||
|
definition,
|
||||||
|
options.anki.terms.fields,
|
||||||
|
context.screenshot
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const note = await dictNoteFormat(definition, mode, options);
|
||||||
|
return this.anki.addNote(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onApiDefinitionsAddable({definitions, modes, optionsContext}) {
|
_onApiDefinitionsAddable({definitions, modes, optionsContext}) {
|
||||||
@ -362,6 +382,44 @@ class Backend {
|
|||||||
_onApiClipboardGet() {
|
_onApiClipboardGet() {
|
||||||
return apiClipboardGet();
|
return apiClipboardGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Utilities
|
||||||
|
|
||||||
|
async _injectScreenshot(definition, fields, screenshot) {
|
||||||
|
let usesScreenshot = false;
|
||||||
|
for (const name in fields) {
|
||||||
|
if (fields[name].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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Backend._messageHandlers = new Map([
|
Backend._messageHandlers = new Map([
|
||||||
|
Loading…
Reference in New Issue
Block a user