From efcdff72a3d98b90a17a0c563ef46a0a8f76bf20 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 1 Apr 2020 21:09:49 -0400 Subject: [PATCH 1/6] Move media injection functions into AnkiNoteBuilder --- ext/bg/js/anki-note-builder.js | 76 ++++++++++++++++++++++++++++++ ext/bg/js/backend.js | 84 ++-------------------------------- 2 files changed, 81 insertions(+), 79 deletions(-) diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 51022da3..d34fc66e 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -84,6 +84,82 @@ class AnkiNoteBuilder { }); } + async injectAudio(definition, fields, sources, audioSystem, optionsContext) { + let usesAudio = false; + for (const fieldValue of Object.values(fields)) { + if (fieldValue.includes('{audio}')) { + usesAudio = true; + break; + } + } + + if (!usesAudio) { + return true; + } + + try { + const expressions = definition.expressions; + const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition; + + const {uri} = await audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); + const filename = this._createInjectedAudioFileName(audioSourceDefinition); + if (filename !== null) { + definition.audio = {url: uri, filename}; + } + + return true; + } catch (e) { + return false; + } + } + + async injectScreenshot(definition, fields, screenshot, anki) { + let usesScreenshot = false; + for (const fieldValue of Object.values(fields)) { + if (fieldValue.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 anki.storeMediaFile(filename, data); + } catch (e) { + return; + } + + definition.screenshotFileName = filename; + } + + _createInjectedAudioFileName(definition) { + const {reading, expression} = definition; + if (!reading && !expression) { return null; } + + let filename = 'yomichan'; + if (reading) { filename += `_${reading}`; } + if (expression) { filename += `_${expression}`; } + filename += '.mp3'; + return filename; + } + static stringReplaceAsync(str, regex, replacer) { let match; let index = 0; diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d4c822ca..9e02cced 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -460,19 +460,21 @@ class Backend { const templates = this.defaultAnkiFieldTemplates; if (mode !== 'kanji') { - await this._audioInject( + await this.ankiNoteBuilder.injectAudio( definition, options.anki.terms.fields, options.audio.sources, + this.audioSystem, optionsContext ); } if (details && details.screenshot) { - await this._injectScreenshot( + await this.ankiNoteBuilder.injectScreenshot( definition, options.anki.terms.fields, - details.screenshot + details.screenshot, + this.anki ); } @@ -800,86 +802,10 @@ class Backend { return await this.audioUriBuilder.getUri(definition, source, options); } - async _audioInject(definition, fields, sources, optionsContext) { - let usesAudio = false; - for (const fieldValue of Object.values(fields)) { - if (fieldValue.includes('{audio}')) { - usesAudio = true; - break; - } - } - - if (!usesAudio) { - return true; - } - - try { - const expressions = definition.expressions; - const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition; - - const {uri} = await this.audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); - const filename = this._createInjectedAudioFileName(audioSourceDefinition); - if (filename !== null) { - definition.audio = {url: uri, filename}; - } - - return true; - } catch (e) { - return false; - } - } - - async _injectScreenshot(definition, fields, screenshot) { - let usesScreenshot = false; - for (const fieldValue of Object.values(fields)) { - if (fieldValue.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; - } - async _renderTemplate(template, data) { return handlebarsRenderDynamic(template, data); } - _createInjectedAudioFileName(definition) { - const {reading, expression} = definition; - if (!reading && !expression) { return null; } - - let filename = 'yomichan'; - if (reading) { filename += `_${reading}`; } - if (expression) { filename += `_${expression}`; } - filename += '.mp3'; - return filename; - } - static _getTabUrl(tab) { return new Promise((resolve) => { chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => { From a49e061545ef1d794f5f30d609168afbea9fdf6c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 1 Apr 2020 21:11:01 -0400 Subject: [PATCH 2/6] Move _dateToString into a new function --- ext/bg/js/anki-note-builder.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index d34fc66e..595d56a8 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -126,18 +126,8 @@ class AnkiNoteBuilder { 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 filename = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`; const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, ''); try { @@ -160,6 +150,16 @@ class AnkiNoteBuilder { return filename; } + _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}`; + } + static stringReplaceAsync(str, regex, replacer) { let match; let index = 0; From 97b7b521dd73251dcfb0799793929395e03c8328 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 1 Apr 2020 21:12:59 -0400 Subject: [PATCH 3/6] Create _containsMarker to reduce redundant code --- ext/bg/js/anki-note-builder.js | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 595d56a8..e396bec6 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -85,17 +85,7 @@ class AnkiNoteBuilder { } async injectAudio(definition, fields, sources, audioSystem, optionsContext) { - let usesAudio = false; - for (const fieldValue of Object.values(fields)) { - if (fieldValue.includes('{audio}')) { - usesAudio = true; - break; - } - } - - if (!usesAudio) { - return true; - } + if (!this._containsMarker(fields, 'audio')) { return; } try { const expressions = definition.expressions; @@ -114,17 +104,7 @@ class AnkiNoteBuilder { } async injectScreenshot(definition, fields, screenshot, anki) { - let usesScreenshot = false; - for (const fieldValue of Object.values(fields)) { - if (fieldValue.includes('{screenshot}')) { - usesScreenshot = true; - break; - } - } - - if (!usesScreenshot) { - return; - } + if (!this._containsMarker(fields, 'screenshot')) { return; } const now = new Date(Date.now()); const filename = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`; @@ -160,6 +140,16 @@ class AnkiNoteBuilder { return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`; } + _containsMarker(fields, marker) { + marker = `{${marker}}`; + for (const fieldValue of Object.values(fields)) { + if (fieldValue.includes(marker)) { + return true; + } + } + return false; + } + static stringReplaceAsync(str, regex, replacer) { let match; let index = 0; From 716ab99fc04e0a02e24d3de20cdf0d3a368c1bf0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 1 Apr 2020 21:13:55 -0400 Subject: [PATCH 4/6] Remove inconsistent/unused return value --- ext/bg/js/anki-note-builder.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index e396bec6..0e17783b 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -96,10 +96,8 @@ class AnkiNoteBuilder { if (filename !== null) { definition.audio = {url: uri, filename}; } - - return true; } catch (e) { - return false; + // NOP } } From 8a419dfa67b730d777fbefaf9c6ffa649bbb67d3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 5 Apr 2020 19:34:31 -0400 Subject: [PATCH 5/6] Pass AudioSystem instance to AnkiNoteBuilder constructor --- ext/bg/js/anki-note-builder.js | 7 ++++--- ext/bg/js/backend.js | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 0e17783b..1ccec20c 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -17,7 +17,8 @@ */ class AnkiNoteBuilder { - constructor({renderTemplate}) { + constructor({audioSystem, renderTemplate}) { + this._audioSystem = audioSystem; this._renderTemplate = renderTemplate; } @@ -84,14 +85,14 @@ class AnkiNoteBuilder { }); } - async injectAudio(definition, fields, sources, audioSystem, optionsContext) { + async injectAudio(definition, fields, sources, optionsContext) { if (!this._containsMarker(fields, 'audio')) { return; } try { const expressions = definition.expressions; const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition; - const {uri} = await audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); + const {uri} = await this.audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); const filename = this._createInjectedAudioFileName(audioSourceDefinition); if (filename !== null) { definition.audio = {url: uri, filename}; diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 9e02cced..1fa7ede1 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -51,12 +51,16 @@ class Backend { this.anki = new AnkiNull(); this.mecab = new Mecab(); this.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)}); - this.ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: this._renderTemplate.bind(this)}); this.options = null; this.optionsSchema = null; this.defaultAnkiFieldTemplates = null; this.audioSystem = new AudioSystem({getAudioUri: this._getAudioUri.bind(this)}); this.audioUriBuilder = new AudioUriBuilder(); + this.ankiNoteBuilder = new AnkiNoteBuilder({ + audioSystem: this.audioSystem, + renderTemplate: this._renderTemplate.bind(this) + }); + this.optionsContext = { depth: 0, url: window.location.href @@ -464,7 +468,6 @@ class Backend { definition, options.anki.terms.fields, options.audio.sources, - this.audioSystem, optionsContext ); } From a6773e0240f9cf25913d20ac75352b42d5c4e517 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 6 Apr 2020 17:34:36 -0400 Subject: [PATCH 6/6] Fix field name --- ext/bg/js/anki-note-builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 1ccec20c..244aaab8 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -92,7 +92,7 @@ class AnkiNoteBuilder { const expressions = definition.expressions; const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition; - const {uri} = await this.audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); + const {uri} = await this._audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); const filename = this._createInjectedAudioFileName(audioSourceDefinition); if (filename !== null) { definition.audio = {url: uri, filename};