From dac33e696145ad3c2cfe076a7fadc82c05732102 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 18 Jul 2020 14:15:36 -0400 Subject: Extension unload indication fix (#662) * Remove unused function * Rename field * Change extensionUnloaded trigger function * Update how extension unloaded content is shown * Ignore certain errors caused by extension unload * Add _showExtensionUnloaded function * Wrap internals of updateOptions * Suppress errors caued by extension unload * Make the frontend trigger the popup's extensionUnloaded event --- ext/fg/js/float.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'ext/fg/js/float.js') diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 61d42fb3..690d1b9e 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -31,7 +31,7 @@ class DisplayFloat extends Display { this._nestedPopupsPrepared = false; this._ownerFrameId = null; this._frameEndpoint = new FrameEndpoint(); - this._windowMessageHandlers = new Map([ + this._messageHandlers = new Map([ ['configure', {async: true, handler: this._onMessageConfigure.bind(this)}], ['setOptionsContext', {async: false, handler: this._onMessageSetOptionsContext.bind(this)}], ['setContent', {async: false, handler: this._onMessageSetContent.bind(this)}], @@ -39,6 +39,9 @@ class DisplayFloat extends Display { ['setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}], ['setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}] ]); + this._windowMessageHandlers = new Map([ + ['extensionUnloaded', {async: false, handler: this._onMessageExtensionUnloaded.bind(this)}] + ]); this.registerActions([ ['copy-host-selection', () => this._copySelection()] @@ -54,6 +57,7 @@ class DisplayFloat extends Display { api.crossFrame.registerHandlers([ ['popupMessage', {async: 'dynamic', handler: this._onMessage.bind(this)}] ]); + window.addEventListener('message', this._onWindowMessage.bind(this), false); this._frameEndpoint.signal(); } @@ -107,7 +111,7 @@ class DisplayFloat extends Display { } const {action, params} = data.data; - const handlerInfo = this._windowMessageHandlers.get(action); + const handlerInfo = this._messageHandlers.get(action); if (typeof handlerInfo === 'undefined') { throw new Error(`Invalid action: ${action}`); } @@ -117,6 +121,18 @@ class DisplayFloat extends Display { return {async, result}; } + _onWindowMessage(e) { + const data = e.data; + if (!this._frameEndpoint.authenticate(data)) { return; } + + const {action, params} = data.data; + const messageHandler = this._windowMessageHandlers.get(action); + if (typeof messageHandler === 'undefined') { return; } + + const callback = () => {}; // NOP + yomichan.invokeMessageHandler(messageHandler, params, callback); + } + async _onMessageConfigure({frameId, ownerFrameId, popupId, optionsContext, childrenSupported, scale}) { this._ownerFrameId = ownerFrameId; this.setOptionsContext(optionsContext); @@ -152,6 +168,11 @@ class DisplayFloat extends Display { this._setContentScale(scale); } + _onMessageExtensionUnloaded() { + if (yomichan.isExtensionUnloaded) { return; } + yomichan.triggerExtensionUnloaded(); + } + // Private _copySelection() { -- cgit v1.2.3