aklinker1 / webext-core

Collection of essential libraries and tools for building web extensions
https://webext-core.aklinker1.io
MIT License
96 stars 11 forks source link

`@webext-core/messaging` consider exposing `defineGenericMessanging` #56

Closed andrefgneves closed 4 months ago

andrefgneves commented 4 months ago

Maybe I missed it, but with @webext-core/messaging there seems to be no way to register the listeners on a content script. I have a use case where I want my popup to get some data from content script running on the active window/tab. Exposing defineGenericMessanging would allow me to create a custom messaging wrapper and benefit from everything else that @webext-core/messaging provides. I'm using @webext-core/proxy-service to talk to the bg script, and I'd also like to keep all the messaging APIs consistent.

aklinker1 commented 4 months ago

You can't send a message directly from a popup to a content script, if that's what you're trying to do. The messaging library is a simple wrapper around the web extension APIs, and that's not allowed.

You have to forward it through the background. popup -> background -> content-script. Same thing if it's the other way around.

Here's an example of using the background to forward messages between frames of the same tab, which is similar to what you're attempting to accomplish (frame n -> background -> frame 0).

https://github.com/search?q=repo%3Aanime-skip%2Fplayer%20getEpisodeInfoFromHelper&type=code

You can do this with defineExtensionMessaging, no need to expose the generic version.

andrefgneves commented 4 months ago

Thanks @aklinker1, that makes sense.