diff options
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/anki-note-builder.js | 76 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 99 | ||||
-rw-r--r-- | ext/bg/js/options.js | 9 | ||||
-rw-r--r-- | ext/bg/js/settings/anki-templates.js | 7 | ||||
-rw-r--r-- | ext/bg/js/settings/anki.js | 2 |
5 files changed, 102 insertions, 91 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index d0ff8205..244aaab8 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -17,11 +17,12 @@ */ class AnkiNoteBuilder { - constructor({renderTemplate}) { + constructor({audioSystem, renderTemplate}) { + this._audioSystem = audioSystem; this._renderTemplate = renderTemplate; } - async createNote(definition, mode, options, templates) { + async createNote(definition, mode, context, options, templates) { const isKanji = (mode === 'kanji'); const tags = options.anki.tags; const modeOptions = isKanji ? options.anki.kanji : options.anki.terms; @@ -35,7 +36,7 @@ class AnkiNoteBuilder { }; for (const [fieldName, fieldValue] of modeOptionsFieldEntries) { - note.fields[fieldName] = await this.formatField(fieldValue, definition, mode, options, templates, null); + note.fields[fieldName] = await this.formatField(fieldValue, definition, mode, context, options, templates, null); } if (!isKanji && definition.audio) { @@ -60,7 +61,7 @@ class AnkiNoteBuilder { return note; } - async formatField(field, definition, mode, options, templates, errors=null) { + async formatField(field, definition, mode, context, options, templates, errors=null) { const data = { marker: null, definition, @@ -69,7 +70,8 @@ class AnkiNoteBuilder { modeTermKanji: mode === 'term-kanji', modeTermKana: mode === 'term-kana', modeKanji: mode === 'kanji', - compactGlossaries: options.general.compactGlossaries + compactGlossaries: options.general.compactGlossaries, + context }; const pattern = /\{([\w-]+)\}/g; return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => { @@ -83,6 +85,70 @@ class AnkiNoteBuilder { }); } + 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 this._audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext}); + const filename = this._createInjectedAudioFileName(audioSourceDefinition); + if (filename !== null) { + definition.audio = {url: uri, filename}; + } + } catch (e) { + // NOP + } + } + + async injectScreenshot(definition, fields, screenshot, anki) { + if (!this._containsMarker(fields, 'screenshot')) { return; } + + const now = new Date(Date.now()); + const filename = `yomichan_browser_screenshot_${definition.reading}_${this._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; + } + + _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}`; + } + + _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; diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1e8c979f..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 @@ -455,12 +459,12 @@ class Backend { return results; } - async _onApiDefinitionAdd({definition, mode, context, optionsContext}) { + async _onApiDefinitionAdd({definition, mode, context, details, optionsContext}) { const options = this.getOptions(optionsContext); const templates = this.defaultAnkiFieldTemplates; if (mode !== 'kanji') { - await this._audioInject( + await this.ankiNoteBuilder.injectAudio( definition, options.anki.terms.fields, options.audio.sources, @@ -468,19 +472,20 @@ class Backend { ); } - if (context && context.screenshot) { - await this._injectScreenshot( + if (details && details.screenshot) { + await this.ankiNoteBuilder.injectScreenshot( definition, options.anki.terms.fields, - context.screenshot + details.screenshot, + this.anki ); } - const note = await this.ankiNoteBuilder.createNote(definition, mode, options, templates); + const note = await this.ankiNoteBuilder.createNote(definition, mode, context, options, templates); return this.anki.addNote(note); } - async _onApiDefinitionsAddable({definitions, modes, optionsContext}) { + async _onApiDefinitionsAddable({definitions, modes, context, optionsContext}) { const options = this.getOptions(optionsContext); const templates = this.defaultAnkiFieldTemplates; const states = []; @@ -489,7 +494,7 @@ class Backend { const notes = []; for (const definition of definitions) { for (const mode of modes) { - const note = await this.ankiNoteBuilder.createNote(definition, mode, options, templates); + const note = await this.ankiNoteBuilder.createNote(definition, mode, context, options, templates); notes.push(note); } } @@ -800,86 +805,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) => { diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 5c68c403..abb054d4 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -91,6 +91,15 @@ const profileOptionsVersionUpdates = [ if (utilStringHashCode(options.anki.fieldTemplates) === 1444379824) { options.anki.fieldTemplates = null; } + }, + (options) => { + // Version 13 changes: + // Default anki field tempaltes updated to include {document-title}. + let fieldTemplates = options.anki.fieldTemplates; + if (typeof fieldTemplates === 'string') { + fieldTemplates += '\n\n{{#*inline "document-title"}}\n {{~context.document.title~}}\n{{/inline}}'; + options.anki.fieldTemplates = fieldTemplates; + } } ]; diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js index c5222d30..e3852eb4 100644 --- a/ext/bg/js/settings/anki-templates.js +++ b/ext/bg/js/settings/anki-templates.js @@ -99,10 +99,15 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i const definition = await ankiTemplatesValidateGetDefinition(text, optionsContext); if (definition !== null) { const options = await apiOptionsGet(optionsContext); + const context = { + document: { + title: document.title + } + }; let templates = options.anki.fieldTemplates; if (typeof templates !== 'string') { templates = await apiGetDefaultAnkiFieldTemplates(); } const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: apiTemplateRender}); - result = await ankiNoteBuilder.formatField(field, definition, mode, options, templates, exceptions); + result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions); } } catch (e) { exceptions.push(e); diff --git a/ext/bg/js/settings/anki.js b/ext/bg/js/settings/anki.js index b706cd1b..f2e1ca76 100644 --- a/ext/bg/js/settings/anki.js +++ b/ext/bg/js/settings/anki.js @@ -243,6 +243,7 @@ function ankiGetFieldMarkers(type) { 'cloze-prefix', 'cloze-suffix', 'dictionary', + 'document-title', 'expression', 'furigana', 'furigana-plain', @@ -258,6 +259,7 @@ function ankiGetFieldMarkers(type) { return [ 'character', 'dictionary', + 'document-title', 'glossary', 'kunyomi', 'onyomi', |