webextension-polyfill: runtime.onMessage is broken
Error in event handler for runtime.onMessage: TypeError: sendResponse is not a function
at runtime.onMessage.addListener (chrome-extension://oommdnlanbpnfobibbnfmnmobfddkofp/background.js:4:2)
at onMessage (chrome-extension://oommdnlanbpnfobibbnfmnmobfddkofp/browser-polyfill.js:1338:22)
https://github.com/mozilla/webextension-polyfill/blob/master/src/browser-polyfill.js#L314-L328 I’m not sure this is due to API changes or something. The wrapper of listener looks completely nonsense to me. Shouldn’t it just invoke the listener directly?
return function onMessage(message, sender, sendResponse) {
return listener(message, sender, sendResponse);
};
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 19 (3 by maintainers)
Links to this issue
Commits related to this issue
- fixes #16 — committed to corevo/webextension-polyfill by corevo 7 years ago
Just spent a good hour figuring this out for myself. Unfortunately different documentation on MDN seems to disagree how this this is supposed to work:
runtime.onMessage (which should probably be considered the source of truth) shows this working with a
sendResponsecallback, returning a boolean indicating if the callback will be invoked asynchronously.tabs.sendMessage shows this working by returning a promise.
Hi there from the future. Since this is the issue linked to from MDN, I decided to post my discovery here. For any Rust folks trying to bind to
browser.runtime.onMessage.addListenerproperly, here’s how to do it:For the
pub async fn send_message, make sure you havewasm-bindgen-futuresas a dependency. It auto-converts JS Promises into Rust’sasyncsystem.Notice that we’re only accepting two arguments to the closure passed to
add_listener. We completely ignore thesendMessageargument and force aPromiseto be returned, as per the MDN docs.Downstream in our background script, we can add the listener like so:
You’re free to do pretty much whatever you want inside the
Box’d closure, including moving in Rust structures, write log messages, etc. As long as aPromiseis the return value. The finalforgetis also critical to ensure that the closure isn’t dropped on the Rust side before it has a chance to be used on the JS side.Cheers!
So is somebody gonna edit the MDN? I will do it myself if nobody else will. The question is whether I mark the
sendResponsedeprecated or just add a NOTE about future plans of W3C and about current behavior of popularwebextension-polyfilllibrary.EDIT: One more thing, what about similar
browser.runtime.onMessageExternal(MDN )? Does it work with polyfill and does it share the same destiny?EDIT 2: MDN is now updated with warning block.
My apologies for the late reply, I deferred closing this issue in the last bug triage meeting because I wanted to discuss it again with @kmaglione (mainly to collect additional details and then be able to provide a more complete comment here).
While discussing this issue with @kmaglione it did come out that the polyfill implementation of the
runtime.onMessageAPI doesn’t provide thesendResponsecallback on purpose: the reason is that thesendResponsecallback is going to be removed from the W3C specs (https://browserext.github.io/browserext/#runtime, the callback is still listed in the current spec draft, but as far as I know, mainly what I did learn from the discussion with Kris, its removal has been discussed and it is going to be removed).We agreed that this “incompatibility”, between the current Firefox implementation and the W3C spec for this API event, should be better highlighted and explained in the API documentation on MDN to prevent confusion about the implementation in the polyfill.
@6a68 With the polyfill, you can only send the response by returning a promise.
Here is what I do now:
sendResponsefunction when using the polyfill.false. Never returntrueor Firefox would expect you to callsendResponsefunction.undefined.I think the proper “fix” to this issue is to remove/deprecate (since it is going to be removed)
sendResponseon MDN and add a warning about the current state of polyfilled environment. Currently, the polyfill disagrees with MDN, which is confusing.Also, I found that the document had been updated and the usage of the promise is now included (which was not when the issue was filed): https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onMessage$compare?to=1204311&from=1178671
Currently I patch it myself. https://github.com/eight04/ansi-viewer/blob/8f6d33f6ee8713ca29d00075c94e9fff89ba04e9/lib/browser-polyfill.js#L828
So… is someone going to merge the PR?