From 6c63a17d669b57fdbc1a5a71daf89c6c95e7d5ef Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 6 Feb 2020 04:00:02 +0200 Subject: query parser html templates --- ext/mixed/js/api.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ext/mixed/js/api.js') diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 5ec93b01..0b1e7e4f 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -105,6 +105,10 @@ function apiGetDisplayTemplatesHtml() { return _apiInvoke('getDisplayTemplatesHtml'); } +function apiGetQueryParserTemplatesHtml() { + return _apiInvoke('getQueryParserTemplatesHtml'); +} + function apiGetZoom() { return _apiInvoke('getZoom'); } -- cgit v1.2.3 From e3c871bc004d9ca612d908f0f3848cdcfc286cd3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 13 Feb 2020 20:18:15 -0500 Subject: Remove unused handlebarsRenderStatic --- ext/bg/js/api.js | 4 ++-- ext/bg/js/backend.js | 10 +++------- ext/bg/js/dictionary.js | 2 +- ext/bg/js/handlebars.js | 5 ----- ext/mixed/js/api.js | 4 ---- 5 files changed, 6 insertions(+), 19 deletions(-) (limited to 'ext/mixed/js/api.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index cd6a9d18..4f1595c7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,8 +17,8 @@ */ -function apiTemplateRender(template, data, dynamic) { - return _apiInvoke('templateRender', {data, template, dynamic}); +function apiTemplateRender(template, data) { + return _apiInvoke('templateRender', {data, template}); } function apiAudioGetUrl(definition, source, optionsContext) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 7b2ec46d..da50bade 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -18,7 +18,7 @@ /*global optionsSave, utilIsolate conditionsTestValue, profileConditionsDescriptor, profileOptionsGetDefaultFieldTemplates -handlebarsRenderDynamic, handlebarsRenderStatic +handlebarsRenderDynamic requestText, requestJson, optionsLoad dictConfigured, dictTermsSort, dictEnabledSet, dictNoteFormat audioGetUrl, audioInject @@ -459,12 +459,8 @@ class Backend { return this.anki.guiBrowse(`nid:${noteId}`); } - async _onApiTemplateRender({template, data, dynamic}) { - return ( - dynamic ? - handlebarsRenderDynamic(template, data) : - handlebarsRenderStatic(template, data) - ); + async _onApiTemplateRender({template, data}) { + return handlebarsRenderDynamic(template, data); } async _onApiCommandExec({command, params}) { diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index e03dece0..491632a0 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -335,7 +335,7 @@ async function dictFieldFormat(field, definition, mode, options, templates, exce } data.marker = marker; try { - return await apiTemplateRender(templates, data, true); + return await apiTemplateRender(templates, data); } catch (e) { if (exceptions) { exceptions.push(e); } return `{${marker}-render-error}`; diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index e8cb67eb..b1443447 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -135,11 +135,6 @@ function handlebarsRegisterHelpers() { } } -function handlebarsRenderStatic(name, data) { - handlebarsRegisterHelpers(); - return Handlebars.templates[name](data).trim(); -} - function handlebarsRenderDynamic(template, data) { handlebarsRegisterHelpers(); const cache = handlebarsRenderDynamic._cache; diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 0b1e7e4f..8eea0628 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -65,10 +65,6 @@ function apiNoteView(noteId) { return _apiInvoke('noteView', {noteId}); } -function apiTemplateRender(template, data, dynamic) { - return _apiInvoke('templateRender', {data, template, dynamic}); -} - function apiAudioGetUrl(definition, source, optionsContext) { return _apiInvoke('audioGetUrl', {definition, source, optionsContext}); } -- cgit v1.2.3 From 61c6a753ce964700e3ef9f6db6bb1137204caf46 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 14 Feb 2020 20:11:40 -0500 Subject: Add apiTemplateRender back to mixed api.js --- ext/mixed/js/api.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ext/mixed/js/api.js') diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 8eea0628..715c8286 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -65,6 +65,10 @@ function apiNoteView(noteId) { return _apiInvoke('noteView', {noteId}); } +function apiTemplateRender(template, data) { + return _apiInvoke('templateRender', {data, template}); +} + function apiAudioGetUrl(definition, source, optionsContext) { return _apiInvoke('audioGetUrl', {definition, source, optionsContext}); } -- cgit v1.2.3 From 496d2599ea6d9607b554641ded77cc82a6b9df42 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 15 Feb 2020 20:33:20 -0500 Subject: Move error handling for apiDefinitionsAddable --- ext/mixed/js/api.js | 2 +- ext/mixed/js/display.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'ext/mixed/js/api.js') diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 715c8286..86bdc73c 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -58,7 +58,7 @@ function apiDefinitionAdd(definition, mode, context, optionsContext) { } function apiDefinitionsAddable(definitions, modes, optionsContext) { - return _apiInvoke('definitionsAddable', {definitions, modes, optionsContext}).catch(() => null); + return _apiInvoke('definitionsAddable', {definitions, modes, optionsContext}); } function apiNoteView(noteId) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 045ce906..5c010747 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -414,7 +414,7 @@ class Display { this.setEventListenersActive(true); - const states = await apiDefinitionsAddable(definitions, ['term-kanji', 'term-kana'], this.getOptionsContext()); + const states = await this.getDefinitionsAddable(definitions, ['term-kanji', 'term-kana']); if (this.setContentToken !== token) { return; } this.updateAdderButtons(states); @@ -463,7 +463,7 @@ class Display { this.setEventListenersActive(true); - const states = await apiDefinitionsAddable(definitions, ['kanji'], this.getOptionsContext()); + const states = await this.getDefinitionsAddable(definitions, ['kanji']); if (this.setContentToken !== token) { return; } this.updateAdderButtons(states); @@ -776,6 +776,14 @@ class Display { return entry !== null ? entry.querySelector('.action-play-audio>img') : null; } + async getDefinitionsAddable(definitions, modes) { + try { + return await apiDefinitionsAddable(definitions, modes, this.getOptionsContext()); + } catch (e) { + return []; + } + } + static delay(time) { return new Promise((resolve) => setTimeout(resolve, time)); } -- cgit v1.2.3 From 2c3f510010ca2910b8c227a9888e2c3584840402 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 16 Feb 2020 13:13:04 -0500 Subject: Allow apiInjectStylesheet to inject a URL --- ext/bg/js/backend.js | 23 ++++++++++++++++------- ext/fg/js/popup.js | 2 +- ext/mixed/js/api.js | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'ext/mixed/js/api.js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d1a34f82..eeed841c 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -499,19 +499,28 @@ class Backend { return Promise.resolve({frameId}); } - _onApiInjectStylesheet({css}, sender) { + _onApiInjectStylesheet({type, value}, sender) { if (!sender.tab) { return Promise.reject(new Error('Invalid tab')); } const tabId = sender.tab.id; const frameId = sender.frameId; - const details = { - code: css, - runAt: 'document_start', - cssOrigin: 'user', - allFrames: false - }; + const details = ( + type === 'file' ? + { + file: value, + runAt: 'document_start', + cssOrigin: 'author', + allFrames: false + } : + { + code: value, + runAt: 'document_start', + cssOrigin: 'user', + allFrames: false + } + ); if (typeof frameId === 'number') { details.frameId = frameId; } diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 45203c03..970c5343 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -167,7 +167,7 @@ class Popup { } else { if (!css) { return; } try { - await apiInjectStylesheet(css); + await apiInjectStylesheet('code', css); this._stylesheetInjectedViaApi = true; } catch (e) { // NOP diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 86bdc73c..14900ecf 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -89,8 +89,8 @@ function apiFrameInformationGet() { return _apiInvoke('frameInformationGet'); } -function apiInjectStylesheet(css) { - return _apiInvoke('injectStylesheet', {css}); +function apiInjectStylesheet(type, value) { + return _apiInvoke('injectStylesheet', {type, value}); } function apiGetEnvironmentInfo() { -- cgit v1.2.3 From 0f46e3a093e7f0c07ad310d8c17e2582bdfd2741 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 11:02:21 -0500 Subject: Use a token to ensure that messages are coming from Yomichan --- ext/bg/js/backend.js | 9 ++++++++- ext/fg/js/float.js | 50 ++++++++++++++++++++++++++++++++++++++++++++------ ext/fg/js/popup.js | 15 +++++++++++---- ext/mixed/js/api.js | 4 ++++ 4 files changed, 67 insertions(+), 11 deletions(-) (limited to 'ext/mixed/js/api.js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 458ea483..a4d085e0 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -46,6 +46,8 @@ class Backend { this.popupWindow = null; this.apiForwarder = new BackendApiForwarder(); + + this.messageToken = yomichan.generateId(16); } async prepare() { @@ -614,6 +616,10 @@ class Backend { }); } + async _onApiGetMessageToken() { + return this.messageToken; + } + // Command handlers async _onCommandSearch(params) { @@ -875,7 +881,8 @@ Backend._messageHandlers = new Map([ ['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)], ['getDisplayTemplatesHtml', (self, ...args) => self._onApiGetDisplayTemplatesHtml(...args)], ['getQueryParserTemplatesHtml', (self, ...args) => self._onApiGetQueryParserTemplatesHtml(...args)], - ['getZoom', (self, ...args) => self._onApiGetZoom(...args)] + ['getZoom', (self, ...args) => self._onApiGetZoom(...args)], + ['getMessageToken', (self, ...args) => self._onApiGetMessageToken(...args)] ]); Backend._commandHandlers = new Map([ diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 440a9731..8f21a9c5 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -/*global popupNestedInitialize, apiForward, Display*/ +/*global popupNestedInitialize, apiForward, apiGetMessageToken, Display*/ class DisplayFloat extends Display { constructor() { @@ -30,6 +30,8 @@ class DisplayFloat extends Display { this._orphaned = false; this._prepareInvoked = false; + this._messageToken = null; + this._messageTokenPromise = null; yomichan.on('orphaned', () => this.onOrphaned()); window.addEventListener('message', (e) => this.onMessage(e), false); @@ -75,11 +77,23 @@ class DisplayFloat extends Display { } onMessage(e) { - const {action, params} = e.data; - const handler = DisplayFloat._messageHandlers.get(action); - if (typeof handler !== 'function') { return; } - - handler(this, params); + const data = e.data; + if (typeof data !== 'object' || data === null) { return; } // Invalid data + + const token = data.token; + if (typeof token !== 'string') { return; } // Invalid data + + if (this._messageToken === null) { + // Async + this.getMessageToken() + .then( + () => { this.handleAction(token, data); }, + () => {} + ); + } else { + // Sync + this.handleAction(token, data); + } } onKeyDown(e) { @@ -94,6 +108,30 @@ class DisplayFloat extends Display { return super.onKeyDown(e); } + async getMessageToken() { + // this._messageTokenPromise is used to ensure that only one call to apiGetMessageToken is made. + if (this._messageTokenPromise === null) { + this._messageTokenPromise = apiGetMessageToken(); + } + const messageToken = await this._messageTokenPromise; + if (this._messageToken === null) { + this._messageToken = messageToken; + } + this._messageTokenPromise = null; + } + + handleAction(token, {action, params}) { + if (token !== this._messageToken) { + // Invalid token + return; + } + + const handler = DisplayFloat._messageHandlers.get(action); + if (typeof handler !== 'function') { return; } + + handler(this, params); + } + getOptionsContext() { return this.optionsContext; } diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 900e7325..4927f4bd 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -/*global apiInjectStylesheet*/ +/*global apiInjectStylesheet, apiGetMessageToken*/ class Popup { constructor(id, depth, frameIdPromise) { @@ -34,6 +34,7 @@ class Popup { this._contentScale = 1.0; this._containerSizeContentScale = null; this._targetOrigin = chrome.runtime.getURL('/').replace(/\/$/, ''); + this._messageToken = null; this._container = document.createElement('iframe'); this._container.className = 'yomichan-float'; @@ -198,6 +199,10 @@ class Popup { // NOP } + if (this._messageToken === null) { + this._messageToken = await apiGetMessageToken(); + } + return new Promise((resolve) => { const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); @@ -349,9 +354,11 @@ class Popup { } _invokeApi(action, params={}) { - if (this._container.contentWindow) { - this._container.contentWindow.postMessage({action, params}, this._targetOrigin); - } + const token = this._messageToken; + const contentWindow = this._container.contentWindow; + if (token === null || contentWindow === null) { return; } + + contentWindow.postMessage({action, params, token}, this._targetOrigin); } static _getFullscreenElement() { diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 14900ecf..7ea68d59 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -113,6 +113,10 @@ function apiGetZoom() { return _apiInvoke('getZoom'); } +function apiGetMessageToken() { + return _apiInvoke('getMessageToken'); +} + function _apiInvoke(action, params={}) { const data = {action, params}; return new Promise((resolve, reject) => { -- cgit v1.2.3