diff options
author | Alex Yatskov <FooSoft@users.noreply.github.com> | 2019-08-17 09:05:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-17 09:05:33 -0700 |
commit | 8ebac935e8c0ee167514d5726b1e2e16921e4957 (patch) | |
tree | 905897bbe19b754597b31b89332e09d45a163c5e /ext/bg/js/api.js | |
parent | 1d25c8a744837b02b5d4364d93df65dd4d0b7ed6 (diff) | |
parent | 3a5b06bd1078d39cf9b29cbc05a0cde13c96ca94 (diff) |
Merge pull request #182 from toasted-nutbread/anki-screenshot
Anki screenshot
Diffstat (limited to 'ext/bg/js/api.js')
-rw-r--r-- | ext/bg/js/api.js | 68 |
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)); + }); +} |