PlasmoHQ / docs

📘 Documentation site
https://docs.plasmo.com
10 stars 49 forks source link

Feedback for “Messaging API” relayMessage does not work #58

Open brucelwayne opened 1 year ago

brucelwayne commented 1 year ago

I'm facing an issue while trying to invoke the relayMessage function from a webpage. Regardless of the methods I have tried, I'm unable to successfully call the function. I would appreciate some assistance in resolving this problem.

Steps to Reproduce:

In my web project, I have included the necessary dependencies and initialized the required configurations as per the documentation. I have written the code to call relayMessage at an appropriate location in the webpage, as shown below:

import type { PlasmoCSConfig } from "plasmo"

import { relayMessage} from "@plasmohq/messaging"

export const config: PlasmoCSConfig = {
    matches: [
        "*://*.localhost/*",
    ]
}

relayMessage({
    name: "node"
})

And on our website page:

import { sendToBackgroundViaRelay } from "@plasmohq/messaging"
async function test() {
    const resp = await sendToBackgroundViaRelay({
        name: "node",
        body: {
            id: 12345,
        }
    })
    console.log(resp)
}
test();

Expected Result:

I expect that after invoking the relayMessage function, the message should be sent successfully and trigger the corresponding logic.

Actual Result:

However, despite my attempts, the function fails to be called successfully. I have checked the console for any error messages, but I haven't come across any helpful hints.

Troubleshooting Steps Taken:

I have ensured that all the required dependencies and configurations are correctly loaded before invoking relayMessage. I tried adding some debug logs before calling relayMessage to check for any errors or exceptions, but I still couldn't identify the root cause of the issue. Possible Causes and Solutions:

Thank you!

brucelwayne commented 1 year ago

I've fixed this. It should be called after document ready.

louisgv commented 1 year ago

Might be good to add this to the docs somewhere, or perhaps we should just add a document ready listener for the relay API. What do you think @brucetod ?