No Response from sendToBackground()

Hi there, plasmo noob here. Trying to call a firebase function on the click of a button from a content-script. Read docs on using the messaging api with a background worker. Think I set it all up correctly but not getting any response at all from the sendToBackground() method. Here's some snippets.
// Adding the event listener to my new injected button to call generate
newButton.addEventListener("click", async () => {
console.log("I have been clicked")

const response = await sendToBackground({
name: "generate"
})

console.log("Response " + response)
}
// Adding the event listener to my new injected button to call generate
newButton.addEventListener("click", async () => {
console.log("I have been clicked")

const response = await sendToBackground({
name: "generate"
})

console.log("Response " + response)
}

// background/test/generate.ts

import type { PlasmoMessaging } from "@plasmohq/messaging"
import { httpsCallable } from "firebase/functions"

import { functions } from "~firebase"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const generateMessage = httpsCallable<any, any>(
functions,
"generateMessage"
)

console.log("Calling the method")

const response = await generateMessage()

console.log("Response " + JSON.stringify(response))

res.send({
response
})
}

export default handler

// background/test/generate.ts

import type { PlasmoMessaging } from "@plasmohq/messaging"
import { httpsCallable } from "firebase/functions"

import { functions } from "~firebase"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const generateMessage = httpsCallable<any, any>(
functions,
"generateMessage"
)

console.log("Calling the method")

const response = await generateMessage()

console.log("Response " + JSON.stringify(response))

res.send({
response
})
}

export default handler
Again, I don't see any logs, when I execute this. Any ideas what I messed up?
5 Replies
Sleigh
Sleigh•2y ago
So I figured this one out, turns out generate needed to be in a messages folder. But now having an issue accessing the currently logged in user (in the popup) from the background worker. I assume it must be possible to authenticate requests in a background worker so hoping to find out what I need to do to accomplish that
lab
lab•2y ago
you will need to send the JWT (or whatever auth token you can get from popups to bgsw), which can then be used as an access token to invoke the server
Sleigh
Sleigh•2y ago
@louis I was able to find another message where you said the same thing haha, thanks for the help!
Arcane
Arcane•2y ago
@Sleigh has reached level 1. GG!
YAGPDB.xyz
YAGPDB.xyz•2y ago
Gave +1 Rep to @louis
Want results from more Discord servers?
Add your server