-
-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
browser.runtime.onMessage.addListener
callback's 3rd argument sendResponse
has incorrect type
#299
Comments
Thanks for the report @bnn1! This is likely an upstream issue with In the meantime, you could use the promise API without browser.runtime.onMessage.addListener(() => {
return Promise.resolve("greetings");
});
// or use an async callback
browser.runtime.onMessage.addListener(async () => {
return "greetings";
}); |
Can someone explain to me why message listener can return something? What is the return for? |
@dvlden maybe you want to make a fetch request from a content script, but it's blocked by CORS. To get around this, you can send a message to the background and perform the fetch request there instead, where CORS is disabled. So the background, the message receiver, needs to be able to send the response back to the content script. If you need to do async work, like a fetch request, then:
The polyfill supports both ways of sending a response on both browsers. I think promises are simpler, so I use the Firefox approach in my code. That said, if you're doing anything complex, I would recommend using a library over doing it yourself. WXT will include a messaging library in the future, simplifying all this. |
Was gonna open a PR to Pinged the maintainer and offered to help |
Just released an experiment feature in |
No more errors in my project, finally! |
Describe the bug
The third callback argument
sendResponse
ofbrowser.runtime.onMessage.addListener
has incorrect signature:To Reproduce
Reproduction
Steps to reproduce the behavior:
entrypoints>background.ts
OR
onMessage
listener:Expected behavior
sendMessage
should acceptany
,unknown
or generic data typeScreenshots
Environment
Contribution
The text was updated successfully, but these errors were encountered: