From: Bill Erickson Date: Thu, 9 Dec 2021 16:20:37 +0000 (-0500) Subject: LP1954301 Extension differentiates tabs with same ID X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=07502718637de79d27f87da52014dbf9d6c3de35;p=working%2FHatch.git LP1954301 Extension differentiates tabs with same ID Apply a unique name to each browser tab port on connect. This resolves a scenario where Chrome would send a tab connect message followed immediately by a tab disconnect message using the same tab ID, causing the newly connected tab to be discarded. With a unique name, each connection is handled differently. Signed-off-by: Bill Erickson --- diff --git a/extension/app/extension.js b/extension/app/extension.js index a46a2d3299..3d8dc93834 100644 --- a/extension/app/extension.js +++ b/extension/app/extension.js @@ -69,6 +69,8 @@ function onDisconnected(port) { function onNativeMessage(message) { var tabId = message.clientid; + console.debug("Hatch responded to request on tab " + tabId); + if (tabId && browserPorts[tabId]) { message.from = 'extension'; browserPorts[tabId].postMessage(message); @@ -79,13 +81,20 @@ function onNativeMessage(message) { } + /** * Called when our content script opens connection to this extension. */ chrome.runtime.onConnect.addListener(function(port) { - var tabId = port.sender.tab.id; + + // Apply a unique name to each newly connected port. This name + // will persist for the life of the port object. Use a Number here + // because our Java back-end is expecting a Long (numeric) value. + let tabId = port.name = + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER - 1); browserPorts[tabId] = port; + console.debug('new port connected with id ' + tabId); port.onMessage.addListener(function(msg) {