How to listen to a Port's disconnect event?

I created an entry file in background/ports/foo.ts, and I'd like to listen to onConnect/onDisconnect events (I want to know when the user has closed a certain tab). Is there a standard way to do this with plasmo?
4 Replies
MagneticSpark
MagneticSparkOP•10mo ago
To give more context, I'm using the port to send messages from content scripts to my background worker, and I read that listening to onDisconnect would be a good way to keep to know if there's any content script running without requireing the "tabs" permission. I was hoping I could just export const onDisconnect = (port) => {} or something like that
Arcane
Arcane•10mo ago
@MagneticSpark has reached level 1. GG!
Avi
Avi•10mo ago
It would've been a great way, but the issue is that Chrome will automatically kill the Service Worker when its JS stops executing for some time RIght now I can't think of a sustainable way to detect when a tab is closed
Avi
Avi•10mo ago
I ran into a similar issue in my YouTube Time Manager, such that when the ext updates I needed to know when the context becomes invalid My initial idea was to set an onDisconnected listener, but Chrome kept killing the Service Worker so it repeatedly told the user "Reload this tab to keep the watch tracking" My solution was to simply inform the user when the storage retrieval/saving failed
Chrome Web Store
Add new features to your browser and personalize your browsing experience.
Want results from more Discord servers?
Add your server