aboutsummaryrefslogtreecommitdiff
path: root/ext/js/comm/cross-frame-api.js
diff options
context:
space:
mode:
authorpraschke <stel@comfy.monster>2023-09-30 17:17:31 +0100
committerpraschke <stel@comfy.monster>2023-09-30 17:56:52 +0100
commit6769ff501c8b5d20e9056de57eff5a62a92339d3 (patch)
treeee22b630a52fb802d357bb85c170c366e3f81ed7 /ext/js/comm/cross-frame-api.js
parent0fa8d441a269b13474e6ad4108f49497d4d3abfd (diff)
create both cross-frame ports in the background
on Chrome (currently 117), the port created in the content script with runtime.connect does not properly receive an onDisconnect event when the service worker sleeps. the port created in the background with tabs.connect does receive the event, so create both ports with tabs.connect. fixes #241.
Diffstat (limited to 'ext/js/comm/cross-frame-api.js')
-rw-r--r--ext/js/comm/cross-frame-api.js31
1 files changed, 18 insertions, 13 deletions
diff --git a/ext/js/comm/cross-frame-api.js b/ext/js/comm/cross-frame-api.js
index 7892eb4c..fb2a1718 100644
--- a/ext/js/comm/cross-frame-api.js
+++ b/ext/js/comm/cross-frame-api.js
@@ -224,10 +224,13 @@ class CrossFrameAPI {
this._commPorts = new Map();
this._messageHandlers = new Map();
this._onDisconnectBind = this._onDisconnect.bind(this);
+ this._tabId = null;
+ this._frameId = null;
}
- prepare() {
+ async prepare() {
chrome.runtime.onConnect.addListener(this._onConnect.bind(this));
+ ({tabId: this._tabId, frameId: this._frameId} = await yomichan.api.frameInformationGet());
}
invoke(targetFrameId, action, params={}) {
@@ -235,8 +238,8 @@ class CrossFrameAPI {
}
async invokeTab(targetTabId, targetFrameId, action, params={}) {
- if (typeof targetTabId !== 'number') { targetTabId = null; }
- const commPort = this._getOrCreateCommPort(targetTabId, targetFrameId);
+ if (typeof targetTabId !== 'number') { targetTabId = this._tabId; }
+ const commPort = await this._getOrCreateCommPort(targetTabId, targetFrameId);
return await commPort.invoke(action, params, this._ackTimeout, this._responseTimeout);
}
@@ -265,8 +268,8 @@ class CrossFrameAPI {
}
if (details.name !== 'cross-frame-communication-port') { return; }
- const otherTabId = details.sourceTabId;
- const otherFrameId = details.sourceFrameId;
+ const otherTabId = details.otherTabId;
+ const otherFrameId = details.otherFrameId;
this._setupCommPort(otherTabId, otherFrameId, port);
} catch (e) {
port.disconnect();
@@ -297,14 +300,16 @@ class CrossFrameAPI {
return this._createCommPort(otherTabId, otherFrameId);
}
- _createCommPort(otherTabId, otherFrameId) {
- const details = {
- name: 'background-cross-frame-communication-port',
- targetTabId: otherTabId,
- targetFrameId: otherFrameId
- };
- const port = yomichan.connect(null, {name: JSON.stringify(details)});
- return this._setupCommPort(otherTabId, otherFrameId, port);
+ async _createCommPort(otherTabId, otherFrameId) {
+ await yomichan.api.openCrossFramePort(otherTabId, otherFrameId);
+
+ const tabPorts = this._commPorts.get(otherTabId);
+ if (typeof tabPorts !== 'undefined') {
+ const commPort = tabPorts.get(otherFrameId);
+ if (typeof commPort !== 'undefined') {
+ return commPort;
+ }
+ }
}
_setupCommPort(otherTabId, otherFrameId, port) {