diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-09-09 12:54:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-09 12:54:59 -0400 |
commit | e3a767876944467b09086501a8b2ef308716090a (patch) | |
tree | ccaca3fac375d975354648d227299e6b162a3db6 /ext/bg/js/anki-note-builder.js | |
parent | 2aa86cc5f8cda022076f7fa047f17fdcca4a0f5e (diff) |
Anki screenshot refactor (#791)
* Use more consistent style for injectScreenshot
* Move screenshot generation into AnkiNoteBuilder/Backend
* Get optionsContext before await
Diffstat (limited to 'ext/bg/js/anki-note-builder.js')
-rw-r--r-- | ext/bg/js/anki-note-builder.js | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 4afb2d40..19e352f1 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -20,11 +20,12 @@ */ class AnkiNoteBuilder { - constructor({anki, audioSystem, renderTemplate, getClipboardImage=null}) { + constructor({anki, audioSystem, renderTemplate, getClipboardImage=null, getScreenshot=null}) { this._anki = anki; this._audioSystem = audioSystem; this._renderTemplate = renderTemplate; this._getClipboardImage = getClipboardImage; + this._getScreenshot = getScreenshot; } async createNote({ @@ -130,18 +131,23 @@ class AnkiNoteBuilder { async injectScreenshot(definition, fields, screenshot) { if (!this._containsMarker(fields, 'screenshot')) { return; } + const reading = definition.reading; const now = new Date(Date.now()); - let fileName = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`; - fileName = AnkiNoteBuilder.replaceInvalidFileNameCharacters(fileName); - const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, ''); try { + const {windowId, tabId, ownerFrameId, format, quality} = screenshot; + const dataUrl = await this._getScreenshot(windowId, tabId, ownerFrameId, format, quality); + + let fileName = `yomichan_browser_screenshot_${reading}_${this._dateToString(now)}.${format}`; + fileName = AnkiNoteBuilder.replaceInvalidFileNameCharacters(fileName); + const data = dataUrl.replace(/^data:[\w\W]*?,/, ''); + await this._anki.storeMediaFile(fileName, data); + + definition.screenshotFileName = fileName; } catch (e) { - return; + // NOP } - - definition.screenshotFileName = fileName; } async injectClipboardImage(definition, fields) { |