-
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
Support sendResponse callback in onMessage #22
Conversation
Now onMessage behaves as documented in MDN, and like Firefox.
I futzed around for a bit before finding a solution to this same problem. The above solution to the problem works well for me, too. So, I've just started using this branch. I look forward to seeing it merged. |
Updated, I now use the I used to implement message passing like this: chrome.tabs.sendMessage(id, { event : "foo" }, {}, (response) => {
// do something
});
// handler
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse("bar");
}); This worked fine. However, when I now follow the approach from MDN as shown here: // handler in content script
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
return Promise.resolve("bar");
}); When I call this from the background script console, I don't get any result: browser.tabs.sendMessage(418, { event: "foo" }).then(response => { console.log(response) }, error => { console.error(error) }).catch(error => console.error(error)) Returns:
Shouldn't this print the value "bar" on the console? When I break in the polyfill code that you changed, this is where it returns: I'm not calling |
@nklein How did you implement this? Could you perhaps post a sample so I can double-check what's going wrong with my code. Thanks! |
As I understand the documentation on the Mozilla Developer Network site, the listener is supposed to return 'true' if it has called or will call
Or, if it needed to be asynchronous, then:
Or if you are doing something explicitly asynchronous, then:
And, actually, it appears that I changed the |
Thanks! Actually, using the patch from @Rob--W – without changing his code – I just do this: return Promise.resolve("bar"); Which gives me the correct response, but a few seconds (like 10–20?) later, it shows errors in my console: When I instead do: // background page
let ret = browser.tabs.sendMessage(id, "foo");
ret.then((response) => {
// handle response
}, (error) => {
// handle error
}).catch((error) => {
// handle other errors
});
// content script
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse("bar");
return(true);
} I get an error inside |
Hi @Rob--W, Based on the above discussion, we agreed to mark the above issue as needs: docs (to fix the ambiguity in our MDN docs) and state: wontfix (to state that the deprecated |
Now onMessage behaves as documented in MDN, and like Firefox.
The only thing I was not completely sure about is what to do when
return true
is used. It can be resolved in two ways:sendResponse
withtrue
.true
in the realonMessage
handler and allowingsendResponse
to be called asynchronously.I follow Firefox's implementation and do the latter - http://searchfox.org/mozilla-central/rev/848c29538ab007fb95dc6cff194f0e3d3809613d/toolkit/components/extensions/ExtensionChild.jsm#363
This fixes #16.