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);
}
+
/**
* 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) {