diff options
author | Darius Jahandarie <djahandarie@gmail.com> | 2023-10-01 09:14:48 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-01 09:14:48 +0000 |
commit | 8430fce21912ad08ddeaa3714c6feb0c2cc01c10 (patch) | |
tree | 7d9726e0941ad4360b7e8f70cd40f5076c101ed5 /ext/js/comm/cross-frame-api.js | |
parent | a7f119e5e70b2b9d61c5dca351ec2e36c9b6d00e (diff) | |
parent | 841d49c7f97678b4873ea198b91419687d137849 (diff) |
Merge pull request #259 from praschke/chrome-death-fix
Fix Chrome death, by creating both cross-frame ports in the background
Diffstat (limited to 'ext/js/comm/cross-frame-api.js')
-rw-r--r-- | ext/js/comm/cross-frame-api.js | 31 |
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) { |