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.