How do I message from background to my content script?
background.tsx
content.tsx
I want to send the tab details from my background to the content script. Any help is really appreciated. I'm so tired of trying to do this.
12 Replies
ive seen docs on how to send feom content to background. But Id really like it if someone helped me with background to content
Plasmo Docs
Messaging API – Plasmo
The Plasmo messaging API is a powerful tool for sending real-time messages between different parts of your extension.
look at Ports
@filthytone I'm trying to do as it says in the docs for ports.
I'm facing this error when using const mailPort = usePort("mail"). It says
with a red underline below "mail".
So when I Ctrl+left clicked usePort and when inside node_modules/@plasmohq/messaging/dist/hook.d.ts and checked the definition for usePort, it looks like this
Thats it? But on github it looks completely different.
Please help me solve this. I'd really appreciate it
it's because you also need to define
background/ports/mail.ts
and then do a dev build which will generate the types for that mail
port
once you have that, you'll see the typescript never
error go away@filthytone yo dude. Thanks a lot!! That solved my error. I thought I had put it in
but instead I did
and I ran the pnpm build and loading the dev build works perfectly fine now. I'm receiving the message on both sides. So relieved!
Gave +1 Rep to @filthytone
hello @AB7zz can you share how you solve that?
@AB7zz sure. Native messaging is not working here. Plasmo messaging system is working fine. If i use this in my content script or anywhere
chrome.runtime.onMessage.addListener(function (message) {
console.log("details message", message)
})
I found this error
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
Can you help me to use native messaging systemWhen I was seeing that erorr, I just removed my extension and restarted Chrome. Added the extension back and everything worked fine.
@banshee has reached level 1. GG!
@Asif , @banshee I am also running into the same problem - very similar to the issue described here: https://discord.com/channels/946290204443025438/1268435446942273556
any guidance?
@filthytone I am trying to use the messaging flow so that a content script button triggers background scripts, and depending on response the content script is updated. Would I use ports for this? I keep getting the issue referenced here: https://discord.com/channels/946290204443025438/1268435446942273556
it depends on how long the content script will wait for some response from the backend .. if it's a small amount of time, you can probably just send a message to the background and await its response ... but if you need to do something more long running then you could use ports ... as for what issue you're getting, can you be more specific?