pvnarula
pvnarula

Reputation: 2821

chrome extension multiple tabs and chrome.runtime.connect

I am opening a long-lived connection with background from a content script if the url is test.com. I am using chrome.runtime.connect. But if I open multiple tabs with test.com, I am getting multiple onMessage calls from the background script.

The question is that the content scripts are not shared with multiple tabs but how come the ports?

Upvotes: 1

Views: 1418

Answers (1)

Haibara Ai
Haibara Ai

Reputation: 10897

It is by design, since if you didn't call runtime.Port.disconnect, the previous connection will be maintained and every time you open a new tab with test.com, you content script is loaded and starts to establish another long-lived connection.

You could call runtime.Port.disconnect to cut the current connection when a new tab is created, or you may distinguish different connections by Port, you can access port.MessageSender on runtime.onConnect.addListener(function(port){});, which contains tab as unique info for the sender.

Upvotes: 2

Related Questions