aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-09 21:25:34 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-13 23:11:56 -0500
commitc9cd29889dc72ce4b35ab16bfc7294ec3d008697 (patch)
treedc3db106464f456d43202321df72567a1364f07e /ext/bg/js/backend.js
parent7091c8c5c0a48630b1ca3b2ab500c131cec19a14 (diff)
Move apiDefinitionAdd implementation into Backend
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js62
1 files changed, 60 insertions, 2 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 67197cf7..726a1714 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -315,8 +315,28 @@ class Backend {
return results;
}
- _onApiDefinitionAdd({definition, mode, context, optionsContext}) {
- return apiDefinitionAdd(definition, mode, context, optionsContext);
+ async _onApiDefinitionAdd({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}) {
@@ -362,6 +382,44 @@ class Backend {
_onApiClipboardGet() {
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([