How to send messages from content to background

I tried to do send a message to the background but it doesn't seem to be called and I don't have any error. Just nothing happen. Am I doing this right?
// in content.tsx
const resp = await sendToBackground({
name: "upload-from-url",
body: {
url: "https://superlink.com"
}
})

console.log("resp", resp)
// in content.tsx
const resp = await sendToBackground({
name: "upload-from-url",
body: {
url: "https://superlink.com"
}
})

console.log("resp", resp)
// in background/upload-from-url.ts
import type { PlasmoMessaging } from "@plasmohq/messaging"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { url } = req.body

console.log(url)

res.send("hello world")
}

export default handler
// in background/upload-from-url.ts
import type { PlasmoMessaging } from "@plasmohq/messaging"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { url } = req.body

console.log(url)

res.send("hello world")
}

export default handler
9 Replies
Charles de Dreuille
Charles de DreuilleOP•2y ago
Also, in content.tsx, I have a typescript error on name Type 'string' is not assignable to type 'never'. Not sure if this is related.
No description
lab
lab•2y ago
The message handles must be placed in background/messages
Charles de Dreuille
Charles de DreuilleOP•2y ago
Ah I missed that part. I just moved it from background/upload-from-url.ts to background/messages/upload-from-url.ts. Thanks @lab
YAGPDB.xyz
YAGPDB.xyz•2y ago
Gave +1 Rep to @lab
Charles de Dreuille
Charles de DreuilleOP•2y ago
What about the typescript error? Any idea what I'm having this?
lab
lab•2y ago
that should be resolved after restart the ts server or running the dev server
Charles de Dreuille
Charles de DreuilleOP•2y ago
Ah you're right. Thanks a lot.
lab
lab•2y ago
yw!
Arcane
Arcane•3mo ago
@N1rvana has reached level 1. GG!
Want results from more Discord servers?
Add your server