aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/api.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-08-15 19:39:58 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-08-15 19:39:58 -0400
commit0f0adf750c8cde668cdcf369cbe0ac173a9edf3f (patch)
treeac33e7efb308f06444a18feb535aab7fba6fd45f /ext/bg/js/api.js
parente23d4b9a82581f3cf1118e31d077fc9cdaff7573 (diff)
Add ability to save screenshot to anki cards
Diffstat (limited to 'ext/bg/js/api.js')
-rw-r--r--ext/bg/js/api.js68
1 files changed, 67 insertions, 1 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index de3ad64e..c33ba709 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -53,7 +53,7 @@ async function apiKanjiFind(text) {
return definitions.slice(0, options.general.maxResults);
}
-async function apiDefinitionAdd(definition, mode) {
+async function apiDefinitionAdd(definition, mode, context) {
const options = utilBackend().options;
if (mode !== 'kanji') {
@@ -64,6 +64,14 @@ async function apiDefinitionAdd(definition, mode) {
);
}
+ if (context.screenshot) {
+ await apiInjectScreenshot(
+ definition,
+ options.anki.terms.fields,
+ context.screenshot
+ );
+ }
+
const note = await dictNoteFormat(definition, mode, options);
return utilBackend().anki.addNote(note);
}
@@ -139,3 +147,61 @@ async function apiCommandExec(command) {
async function apiAudioGetUrl(definition, source) {
return audioBuildUrl(definition, source);
}
+
+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) {
+ if (!(sender && sender.tab)) {
+ return Promise.resolve();
+ }
+
+ const windowId = sender.tab.windowId;
+ return new Promise((resolve) => {
+ chrome.tabs.captureVisibleTab(windowId, options, (dataUrl) => resolve(dataUrl));
+ });
+}
+
+function apiForward(action, params, sender) {
+ if (!(sender && sender.tab)) {
+ return Promise.resolve();
+ }
+
+ const tabId = sender.tab.id;
+ return new Promise((resolve) => {
+ chrome.tabs.sendMessage(tabId, {action, params}, (response) => resolve(response));
+ });
+}