diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-09 21:25:34 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-13 23:11:56 -0500 |
commit | c9cd29889dc72ce4b35ab16bfc7294ec3d008697 (patch) | |
tree | dc3db106464f456d43202321df72567a1364f07e /ext/bg/js | |
parent | 7091c8c5c0a48630b1ca3b2ab500c131cec19a14 (diff) |
Move apiDefinitionAdd implementation into Backend
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/api.js | 60 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 62 |
2 files changed, 62 insertions, 60 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 3be6ac56..2ce90b5c 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -49,28 +49,8 @@ function apiKanjiFind(text, optionsContext) { return utilBackend()._onApiKanjiFind({text, optionsContext}); } -async function apiDefinitionAdd(definition, mode, context, optionsContext) { - const options = await apiOptionsGet(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); +function apiDefinitionAdd(definition, mode, context, optionsContext) { + return utilBackend()._onApiDefinitionAdd({definition, mode, context, optionsContext}); } async function apiDefinitionsAddable(definitions, modes, optionsContext) { @@ -189,42 +169,6 @@ async function apiAudioGetUrl(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) { if (!(sender && sender.tab)) { return Promise.resolve(); 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([ |