-
Notifications
You must be signed in to change notification settings - Fork 215
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
runtime.onMessage is broken #16
Comments
Are you using the compiled source? |
https://github.com/eight04/ansi-viewer/blob/chrome/polyfill/browser-polyfill.js#L826 |
ok, just checking. This has created confusion: #5 |
I'm seeing this problem too. Is there a fix on the way? |
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
tabs.sendMessage shows this working by returning a promise.
|
@kmaglione please see my above note about MDN discrepancy. It looks like you wrote this code and added the example in the MDN tabs.sendMessage. Can you clarify if this should be a callback or a promise? |
FYI I submitted a PR at #22. |
@eight04 's fix works, however, I slightly modified his fix so that it doesn't touch the Firefox side (retains isThenable part)
|
So... is someone going to merge the PR? |
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 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. |
So is |
@rpl can you expand on what the expected usage in WebExtensions be? I have some guesses as to what you might end up with, is the following correct?
This means that:
|
@rpl thank you for the explanation. Is it still true? Looking in the draft I noticed that it's still: Though nothing in the draft for BrowserExtRuntimeSendResponseCallback. Edit: Therefore, it's not just a documentation issue, but an inconsistent behavior of the polyfill script. |
@rpl Hi, I'm confused by how this issue has been handled. Could we revisit this decision? As I understand it, this polyfill exists to make W3C-compatible (i.e. Firefox) webextensions compatible with Chrome. Oddly, the decision here was to deliberately break the polyfilled behavior in Chrome, because Chrome's implementation didn't match planned changes to the W3C spec. That's the opposite of what this library is supposed to do. There's even a working PR that was closed. Meanwhile, close to a year later, this is still a bug with this library in the latest version of Chrome (I ran into it today). How about, instead, landing the PR to fix this bug, then filing a followup bug to update the polyfill if/when Chrome updates its implementation? |
@6a68 With the polyfill, you can only send the response by returning a promise. Here is what I do now:
I think the proper "fix" to this issue is to remove/deprecate (since it is going to be removed) 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): |
So is somebody gonna edit the MDN? EDIT: EDIT 2: |
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 #[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ["browser", "runtime"], js_name = "sendMessage")]
pub async fn send_message(message: JsValue) -> JsValue;
#[wasm_bindgen(js_namespace = ["browser", "runtime", "onMessage"], js_name = "addListener")]
pub fn add_listener(handle: &Closure<dyn Fn(JsValue, JsValue) -> Promise>);
} For the Notice that we're only accepting two arguments to the closure passed to Downstream in our background script, we can add the listener like so: let handle_msg = Closure::wrap(Box::new(move |msg, sender| {
Promise::resolve(&JsValue::from_str("From background!"))
}) as Box<dyn Fn(JsValue, JsValue) -> Promise>);
add_listener(&handle_msg); // The binding we just wrote.
handle_msg.forget(); You're free to do pretty much whatever you want inside the Cheers! |
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?
The text was updated successfully, but these errors were encountered: