aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/backend.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 20d31efc..8df4fd9d 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -163,6 +163,7 @@ class Backend {
chrome.tabs.onZoomChange.addListener(this._onZoomChange.bind(this));
}
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
+ chrome.runtime.onConnect.addListener(this._onConnect.bind(this));
const options = this.getOptions(this.optionsContext);
if (options.general.showGuide) {
@@ -236,6 +237,45 @@ class Backend {
}
}
+ _onConnect(port) {
+ try {
+ const match = /^background-cross-frame-communication-port-(\d+)$/.exec(`${port.name}`);
+ if (match === null) { return; }
+
+ const tabId = (port.sender && port.sender.tab ? port.sender.tab.id : null);
+ if (typeof tabId !== 'number') {
+ throw new Error('Port does not have an associated tab ID');
+ }
+ const senderFrameId = port.sender.frameId;
+ if (typeof tabId !== 'number') {
+ throw new Error('Port does not have an associated frame ID');
+ }
+ const targetFrameId = parseInt(match[1], 10);
+
+ let forwardPort = chrome.tabs.connect(tabId, {frameId: targetFrameId, name: `cross-frame-communication-port-${senderFrameId}`});
+
+ const cleanup = () => {
+ this.checkLastError(chrome.runtime.lastError);
+ if (forwardPort !== null) {
+ forwardPort.disconnect();
+ forwardPort = null;
+ }
+ if (port !== null) {
+ port.disconnect();
+ port = null;
+ }
+ };
+
+ port.onMessage.addListener((message) => { forwardPort.postMessage(message); });
+ forwardPort.onMessage.addListener((message) => { port.postMessage(message); });
+ port.onDisconnect.addListener(cleanup);
+ forwardPort.onDisconnect.addListener(cleanup);
+ } catch (e) {
+ port.disconnect();
+ yomichan.logError(e);
+ }
+ }
+
_onClipboardText({text}) {
this._onCommandSearch({mode: 'popup', query: text});
}