aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js/float.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg/js/float.js')
-rw-r--r--ext/fg/js/float.js25
1 files changed, 23 insertions, 2 deletions
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() {