From a13a68990eac548f8439050222ddf3cb97146199 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 18 Jul 2020 14:16:35 -0400 Subject: 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 --- ext/bg/js/backend.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'ext/bg') 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) { -- cgit v1.2.3