You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
browser.runtime.onMessage.addListener callback's 3rd argument sendResponse has incorrect type #299
browser.runtime.onMessage.addListener(()=>{returnPromise.resolve("greetings");});// or use an async callbackbrowser.runtime.onMessage.addListener(async()=>{return"greetings";});
Can someone explain to me why message listener can return something?
On Chrome API I clearly see that it can return only boolean or undefined but on FF it says return Promise...
@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:
On Chrome, you return true from the message listener, informing the browser a response will be sent. Then you use callbacks to call sendResponse once the async work has finished
On Firefox, you just return a promise of the response
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 webextension-polyfill-ts, but this was already taken care of, see Lusito/webextension-polyfill-ts#96. Just hasn't been released to @types/webextension-polyfill yet.
Just released an experiment feature in [email protected] to switch to @types/chrome, which has this type fixed. Try it out and let me know how it goes: #868
Activity
aklinker1 commentedon Dec 25, 2023
Thanks for the report @bnn1! This is likely an upstream issue with
@types/webextension-polyfill.In the meantime, you could use the promise API without
sendResponse.https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage#examples
nnx0r commentedon Dec 26, 2023
Can someone explain to me why message listener can return something?
On Chrome API I clearly see that it can return only
booleanorundefinedbut on FF it says return Promise...What is the return for?
aklinker1 commentedon Dec 26, 2023
@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:
return truefrom the message listener, informing the browser a response will be sent. Then you use callbacks to callsendResponseonce the async work has finishedThe 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.
aklinker1 commentedon Jan 8, 2024
Was gonna open a PR to
webextension-polyfill-ts, but this was already taken care of, see Lusito/webextension-polyfill-ts#96. Just hasn't been released to@types/webextension-polyfillyet.Pinged the maintainer and offered to help
aklinker1 commentedon Jul 27, 2024
Just released an experiment feature in
[email protected]to switch to@types/chrome, which has this type fixed. Try it out and let me know how it goes: #8680x7a7a commentedon Jul 29, 2024
No more errors in my project, finally!
Good job