I’m trying to write a cross browser extension that is using a background background script and a popup that cross-communicate:
In the background script:
class Listener{
listen(cb){
chrome.runtime.onMessage.addListener((transmission, sender, messageResponseFn) => {
cb(transmission, messageResponseFn);
return true;
});
}
}
export default new Listener();
In the popup:
class Emitter{
send(message, payload){
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ message, payload }, (responseMessage) => {
resolve(responseMessage);
});
});
}
}
export default new Emitter();
This works as expected in Chrome, yet in Firefox the sent response (the function is called correctly and does not throw) will never arrive in my Emitter. Am I using this API wrong? Are there any differences between Firefox and Chrome that I am not aware of? From what I read in the docs the runtime.sendMessage API should be fully supported.
I also found this bug report, but I’m not 100% sure if this is the root cause. Also I am wondering if this could still work if done differently.
What version of Firefox are you testing with? Bug 1202481 did fix a problem here and the fix is only in Firefox Developer Edition right now.
It’s possible there are other problems though. If the problem still occurs with Developer Edition, please file a bug at bugzilla.mozilla.org and include an example add-on that reproduces the bad behavior. It’s hard to know what might be going wrong without seeing your callback.
@billm So I tried installing the latest Developer Edition (alongside 42 that I used before) and unfortunately the issues persist. I also boiled it down to a minimal example of what I am trying to do (which is working fine in Chrome and Opera, yet does not work in Firefox).
I am still not sure if I am doing something wrong here so it would be cool if you could have a look at that before I file an actual bug. The repository for the demo extension is available at: https://github.com/m90/firefox-webextension-issue