UPDATE
It now seems like it's only happening in the first case I described, when the Extensions tab is reloaded and a new tab is opened. Possible bug?
I'm setting up a chrome extension with a background.js page and a content-script.js.
The two communicate with each other via a port. The way it works now, the port gets intialized in the background everytime a tab gets activated or updated and the content-script listens to it via chrome.runtime.onConnect.addListener()
(which returns the port).
This works fine. Except in two (similar) cases:
I'm have some logs that show that in these cases, background.js does initialize the port... but the content script isn't receiving it for some reason.
These are the basics of what i'm doing:
background.js
// When a tab gets activated
chrome.tabs.onActivated.addListener(function(tab) {
port = chrome.tabs.connect(tab.tabId, { name: "fh-ext-messenger" });
console.log(port)
initPortListener(port);
});
// When a tab is updated
chrome.tabs.onUpdated.addListener(function(tab) {
port = chrome.tabs.connect(tab, { name: "fh-ext-messenger" });
console.log(port)
initPortListener(port);
});
content.js
chrome.runtime.onConnect.addListener(function(port) {
console.log(port);
// ...
}
Any ideas on why this doesn't work on an initial tab?
Content scripts are always attached to your extension. Anytime your extension is reloaded or deactivated that content script will be removed from all the tabs that it was active on.
Reloading the extension doesn't make it become active again you need to reload that particular tab.
I have also noticed that reloading the extension page reloads all extensions and shoudl explain why your previously active content scripts are not longer active.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With