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
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.
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: