aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-07-18 14:16:35 -0400
committerGitHub <noreply@github.com>2020-07-18 14:16:35 -0400
commita13a68990eac548f8439050222ddf3cb97146199 (patch)
treecde32346ea25cc6fb685461ace3e1d512842f51b /ext/bg/js
parentdac33e696145ad3c2cfe076a7fadc82c05732102 (diff)
Port name details (#667)
* Use a stringified JSON details object for extension port names * Fix incorrect frame ID check * Add support for connecting to different tabs * Add function for invoking on a different tab
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/backend.js31
1 files changed, 23 insertions, 8 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index f6c2ef74..f0ed986c 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -305,20 +305,32 @@ class Backend {
_onConnect(port) {
try {
- const match = /^background-cross-frame-communication-port-(\d+)$/.exec(`${port.name}`);
- if (match === null) { return; }
+ let details;
+ try {
+ details = JSON.parse(port.name);
+ } catch (e) {
+ return;
+ }
+ if (details.name !== 'background-cross-frame-communication-port') { 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') {
+ if (typeof senderFrameId !== 'number') {
throw new Error('Port does not have an associated frame ID');
}
- const targetFrameId = parseInt(match[1], 10);
+ let {targetTabId, targetFrameId} = details;
+ if (typeof targetTabId !== 'number') {
+ targetTabId = tabId;
+ }
- let forwardPort = chrome.tabs.connect(tabId, {frameId: targetFrameId, name: `cross-frame-communication-port-${senderFrameId}`});
+ const details2 = {
+ name: 'cross-frame-communication-port',
+ sourceFrameId: senderFrameId
+ };
+ let forwardPort = chrome.tabs.connect(targetTabId, {frameId: targetFrameId, name: JSON.stringify(details2)});
const cleanup = () => {
this._checkLastError(chrome.runtime.lastError);
@@ -720,9 +732,12 @@ class Backend {
const frameId = sender.frameId;
const id = yomichan.generateId(16);
- const portName = `action-port-${id}`;
+ const details = {
+ name: 'action-port',
+ id
+ };
- const port = chrome.tabs.connect(tabId, {name: portName, frameId});
+ const port = chrome.tabs.connect(tabId, {name: JSON.stringify(details), frameId});
try {
this._createActionListenerPort(port, sender, this._messageHandlersWithProgress);
} catch (e) {
@@ -730,7 +745,7 @@ class Backend {
throw e;
}
- return portName;
+ return details;
}
async _onApiImportDictionaryArchive({archiveContent, details}, sender, onProgress) {