From e2ac478cb7635cbd15d0033bd2c486689a7b4e67 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 11 Feb 2020 22:19:47 -0500 Subject: Define more globals --- ext/fg/js/float.js | 1 + 1 file changed, 1 insertion(+) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 8d61d8f6..d31b8336 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +/*global popupNestedInitialize, Display*/ class DisplayFloat extends Display { constructor() { -- cgit v1.2.3 From df37acd17f9459c17185552f11dc4cc424e01958 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 13 Feb 2020 01:59:26 +0200 Subject: rename display initialize methods to prepare --- ext/bg/js/search.js | 2 +- ext/fg/js/float.js | 30 +++++++++++++++--------------- ext/mixed/js/display.js | 24 ++++++++++++------------ 3 files changed, 28 insertions(+), 28 deletions(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 6641255f..86ed675a 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -47,7 +47,7 @@ class DisplaySearch extends Display { async prepare() { try { - await this.initialize(); + await super.prepare(); await this.queryParser.prepare(); diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 8d61d8f6..3766d5a4 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -33,6 +33,20 @@ class DisplayFloat extends Display { window.addEventListener('message', (e) => this.onMessage(e), false); } + async prepare(options, popupInfo, url, childrenSupported, scale) { + await super.prepare(options); + + const {id, depth, parentFrameId} = popupInfo; + this.optionsContext.depth = depth; + this.optionsContext.url = url; + + if (childrenSupported) { + popupNestedInitialize(id, depth, parentFrameId, url); + } + + this.setContentScale(scale); + } + onError(error) { if (this._orphaned) { this.setContent('orphaned'); @@ -92,20 +106,6 @@ class DisplayFloat extends Display { setContentScale(scale) { document.body.style.fontSize = `${scale}em`; } - - async initialize(options, popupInfo, url, childrenSupported, scale) { - await super.initialize(options); - - const {id, depth, parentFrameId} = popupInfo; - this.optionsContext.depth = depth; - this.optionsContext.url = url; - - if (childrenSupported) { - popupNestedInitialize(id, depth, parentFrameId, url); - } - - this.setContentScale(scale); - } } DisplayFloat._onKeyDownHandlers = new Map([ @@ -122,7 +122,7 @@ DisplayFloat._messageHandlers = new Map([ ['setContent', (self, {type, details}) => self.setContent(type, details)], ['clearAutoPlayTimer', (self) => self.clearAutoPlayTimer()], ['setCustomCss', (self, {css}) => self.setCustomCss(css)], - ['initialize', (self, {options, popupInfo, url, childrenSupported, scale}) => self.initialize(options, popupInfo, url, childrenSupported, scale)], + ['initialize', (self, {options, popupInfo, url, childrenSupported, scale}) => self.prepare(options, popupInfo, url, childrenSupported, scale)], ['setContentScale', (self, {scale}) => self.setContentScale(scale)] ]); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index cee63d9b..8dea625d 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -43,6 +43,16 @@ class Display { this.setInteractive(true); } + async prepare(options=null) { + await this.displayGenerator.prepare(); + await this.updateOptions(options); + yomichan.on('optionsUpdate', () => this.updateOptions(null)); + } + + isPrepared() { + return this.options !== null; + } + onError(_error) { throw new Error('Override me'); } @@ -238,16 +248,6 @@ class Display { throw new Error('Override me'); } - isInitialized() { - return this.options !== null; - } - - async initialize(options=null) { - await this.displayGenerator.prepare(); - await this.updateOptions(options); - yomichan.on('optionsUpdate', () => this.updateOptions(null)); - } - async updateOptions(options) { this.options = options ? options : await apiOptionsGet(this.getOptionsContext()); this.updateDocumentOptions(this.options); @@ -358,7 +358,7 @@ class Display { async setContentTerms(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isInitialized()) { return; } + if (!this.isPrepared()) { return; } this.setEventListenersActive(false); @@ -419,7 +419,7 @@ class Display { async setContentKanji(definitions, context, token) { if (!context) { throw new Error('Context expected'); } - if (!this.isInitialized()) { return; } + if (!this.isPrepared()) { return; } this.setEventListenersActive(false); -- cgit v1.2.3 From c0225f1f844376e5f61222a4d8186a2c914026eb Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 13 Feb 2020 13:18:54 +0200 Subject: notify popup about initialization --- ext/fg/js/float.js | 2 ++ ext/fg/js/popup.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 3766d5a4..8871160f 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -45,6 +45,8 @@ class DisplayFloat extends Display { } this.setContentScale(scale); + + window.parent.postMessage('initialized', '*'); } onError(error) { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index e7dae93e..6cfe49e5 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -45,6 +45,8 @@ class Popup { this._container.style.height = '0px'; this._updateVisibility(); + + window.addEventListener('message', (e) => this.onMessage(e), false); } // Public properties @@ -129,6 +131,18 @@ class Popup { } } + onMessage(e) { + const action = e.data; + const handler = Popup._windowMessageHandlers.get(action); + if (typeof handler !== 'function') { return; } + + handler(this); + } + + setInitialized() { + throw new Error('Override me'); + } + // Popup-only public functions setParent(parent) { @@ -237,7 +251,7 @@ class Popup { childrenSupported: this._childrenSupported, scale: this._contentScale }); - resolve(); + this.setInitialized = resolve; }); this._observeFullscreen(); this._onFullscreenChanged(); @@ -535,4 +549,8 @@ class Popup { } } +Popup._windowMessageHandlers = new Map([ + ['initialized', (self) => self.setInitialized()] +]); + Popup.outerStylesheet = null; -- cgit v1.2.3 From 810a7e7d92d15412974810702d954de60453dd31 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 14 Feb 2020 02:33:54 +0200 Subject: use sendMessage to notify about initialization --- ext/fg/js/float.js | 2 +- ext/fg/js/frontend.js | 3 ++- ext/fg/js/popup-proxy-host.js | 8 +++++++- ext/fg/js/popup-proxy.js | 5 +++++ ext/fg/js/popup.js | 18 ++---------------- 5 files changed, 17 insertions(+), 19 deletions(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 8871160f..b42ab8ee 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -46,7 +46,7 @@ class DisplayFloat extends Display { this.setContentScale(scale); - window.parent.postMessage('initialized', '*'); + apiForward('popupSetDisplayInitialized'); } onError(error) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 2286bf19..571bbf91 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -243,5 +243,6 @@ Frontend._windowMessageHandlers = new Map([ ]); Frontend._runtimeMessageHandlers = new Map([ - ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }] + ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }], + ['popupSetDisplayInitialized', (self) => { self.popup.setDisplayInitialized(); }] ]); diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index 427172c6..8ea70857 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -42,7 +42,8 @@ class PopupProxyHost { ['showContent', ({id, elementRect, writingMode, type, details}) => this._onApiShowContent(id, elementRect, writingMode, type, details)], ['setCustomCss', ({id, css}) => this._onApiSetCustomCss(id, css)], ['clearAutoPlayTimer', ({id}) => this._onApiClearAutoPlayTimer(id)], - ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)] + ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)], + ['setDisplayInitialized', ({id}) => this._onApiSetDisplayInitialized(id)] ])); } @@ -103,6 +104,11 @@ class PopupProxyHost { return popup.setContentScale(scale); } + async _onApiSetDisplayInitialized(id) { + const popup = this._getPopup(id); + return popup.setDisplayInitialized(); + } + // Private functions _createPopupInternal(parentId, depth) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index 63aa6bbe..f1743064 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -102,6 +102,11 @@ class PopupProxy { this._invokeHostApi('setContentScale', {id, scale}); } + async setDisplayInitialized() { + const id = await this._getPopupId(); + this._invokeHostApi('setDisplayInitialized', {id}); + } + // Private _getPopupId() { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index ad4e5181..b8233cc2 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -43,8 +43,6 @@ class Popup { this._container.style.height = '0px'; this._updateVisibility(); - - window.addEventListener('message', (e) => this.onMessage(e), false); } // Public properties @@ -125,15 +123,7 @@ class Popup { this._invokeApi('setContentScale', {scale}); } - onMessage(e) { - const action = e.data; - const handler = Popup._windowMessageHandlers.get(action); - if (typeof handler !== 'function') { return; } - - handler(this); - } - - setInitialized() { + setDisplayInitialized() { throw new Error('Override me'); } @@ -244,7 +234,7 @@ class Popup { childrenSupported: this._childrenSupported, scale: this._contentScale }); - this.setInitialized = resolve; + this.setDisplayInitialized = resolve; }); this._observeFullscreen(); this._onFullscreenChanged(); @@ -540,8 +530,4 @@ class Popup { } } -Popup._windowMessageHandlers = new Map([ - ['initialized', (self) => self.setInitialized()] -]); - Popup.outerStylesheet = null; -- cgit v1.2.3 From 89cb98e4d14ad4980133f8e8e44b15d56c3ebea5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 14 Feb 2020 21:50:17 -0500 Subject: Add global apiForward --- ext/fg/js/float.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 418c69b6..46d1314e 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, Display*/ +/*global popupNestedInitialize, apiForward, Display*/ class DisplayFloat extends Display { constructor() { -- cgit v1.2.3 From b5d32c73e657c882ba6f70b79d4a5e214684f563 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 16 Feb 2020 12:23:20 -0500 Subject: Simplify process to wait for iframe prepare completion --- ext/fg/js/float.js | 6 +++--- ext/fg/js/frontend.js | 3 +-- ext/fg/js/popup-proxy-host.js | 8 +------- ext/fg/js/popup-proxy.js | 5 ----- ext/fg/js/popup.js | 30 +++++++++++++++++++++++------- 5 files changed, 28 insertions(+), 24 deletions(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 46d1314e..35a78d48 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -34,7 +34,7 @@ class DisplayFloat extends Display { window.addEventListener('message', (e) => this.onMessage(e), false); } - async prepare(options, popupInfo, url, childrenSupported, scale) { + async prepare(options, popupInfo, url, childrenSupported, scale, uniqueId) { await super.prepare(options); const {id, depth, parentFrameId} = popupInfo; @@ -47,7 +47,7 @@ class DisplayFloat extends Display { this.setContentScale(scale); - apiForward('popupSetDisplayInitialized'); + apiForward('popupPrepareCompleted', {uniqueId}); } onError(error) { @@ -125,7 +125,7 @@ DisplayFloat._messageHandlers = new Map([ ['setContent', (self, {type, details}) => self.setContent(type, details)], ['clearAutoPlayTimer', (self) => self.clearAutoPlayTimer()], ['setCustomCss', (self, {css}) => self.setCustomCss(css)], - ['initialize', (self, {options, popupInfo, url, childrenSupported, scale}) => self.prepare(options, popupInfo, url, childrenSupported, scale)], + ['prepare', (self, {options, popupInfo, url, childrenSupported, scale, uniqueId}) => self.prepare(options, popupInfo, url, childrenSupported, scale, uniqueId)], ['setContentScale', (self, {scale}) => self.setContentScale(scale)] ]); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index c3050514..67045241 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -244,6 +244,5 @@ Frontend._windowMessageHandlers = new Map([ ]); Frontend._runtimeMessageHandlers = new Map([ - ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }], - ['popupSetDisplayInitialized', (self) => { self.popup.setDisplayInitialized(); }] + ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }] ]); diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index f6a3d451..98729796 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -43,8 +43,7 @@ class PopupProxyHost { ['showContent', ({id, elementRect, writingMode, type, details}) => this._onApiShowContent(id, elementRect, writingMode, type, details)], ['setCustomCss', ({id, css}) => this._onApiSetCustomCss(id, css)], ['clearAutoPlayTimer', ({id}) => this._onApiClearAutoPlayTimer(id)], - ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)], - ['setDisplayInitialized', ({id}) => this._onApiSetDisplayInitialized(id)] + ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)] ])); } @@ -105,11 +104,6 @@ class PopupProxyHost { return popup.setContentScale(scale); } - async _onApiSetDisplayInitialized(id) { - const popup = this._getPopup(id); - return popup.setDisplayInitialized(); - } - // Private functions _createPopupInternal(parentId, depth) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index 6f3c0f1c..db6dffb1 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -103,11 +103,6 @@ class PopupProxy { this._invokeHostApi('setContentScale', {id, scale}); } - async setDisplayInitialized() { - const id = await this._getPopupId(); - this._invokeHostApi('setDisplayInitialized', {id}); - } - // Private _getPopupId() { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 55f3e0aa..5f6a777b 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -123,10 +123,6 @@ class Popup { this._invokeApi('setContentScale', {scale}); } - setDisplayInitialized() { - throw new Error('Override me'); - } - // Popup-only public functions setParent(parent) { @@ -223,7 +219,10 @@ class Popup { return new Promise((resolve) => { const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); this._container.addEventListener('load', () => { - this._invokeApi('initialize', { + const uniqueId = yomichan.generateId(32); + Popup._listenForDisplayPrepareCompleted(uniqueId, resolve); + + this._invokeApi('prepare', { options: this._options, popupInfo: { id: this._id, @@ -232,9 +231,9 @@ class Popup { }, url: this.url, childrenSupported: this._childrenSupported, - scale: this._contentScale + scale: this._contentScale, + uniqueId }); - this.setDisplayInitialized = resolve; }); this._observeFullscreen(); this._onFullscreenChanged(); @@ -357,6 +356,23 @@ class Popup { } } + static _listenForDisplayPrepareCompleted(uniqueId, resolve) { + const runtimeMessageCallback = ({action, params}, sender, callback) => { + if ( + action === 'popupPrepareCompleted' && + typeof params === 'object' && + params !== null && + params.uniqueId === uniqueId + ) { + chrome.runtime.onMessage.removeListener(runtimeMessageCallback); + callback(); + resolve(); + return false; + } + }; + chrome.runtime.onMessage.addListener(runtimeMessageCallback); + } + static _getPositionForHorizontalText(elementRect, width, height, viewport, offsetScale, optionsGeneral) { const preferBelow = (optionsGeneral.popupHorizontalTextPosition === 'below'); const horizontalOffset = optionsGeneral.popupHorizontalOffset * offsetScale; -- cgit v1.2.3 From 6df5220e0249fa06865034a44c690f7c640a38ed Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 16 Feb 2020 12:23:40 -0500 Subject: Prevent multiple prepare calls --- ext/fg/js/float.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 35a78d48..440a9731 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -29,12 +29,16 @@ class DisplayFloat extends Display { }; this._orphaned = false; + this._prepareInvoked = false; yomichan.on('orphaned', () => this.onOrphaned()); window.addEventListener('message', (e) => this.onMessage(e), false); } async prepare(options, popupInfo, url, childrenSupported, scale, uniqueId) { + if (this._prepareInvoked) { return; } + this._prepareInvoked = true; + await super.prepare(options); const {id, depth, parentFrameId} = popupInfo; -- 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/fg/js/float.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