I built a Chrome extension using sendRequest
to send a message from the content page to the popup, and get a callback.
content.js :
chrome.extension.sendRequest(data, function (result){
// here the result should be a boolean
if (result){
alert("ok");
}
});
popup.js :
chrome.extension.onRequest.addListener(function (request, sender, callback) {
// Here I have some asynchronous work
setTimeout(function(){
callback(true);
}, 500);
}
All this worked perfectly fine.
But the sendRequest
is deprecated, and needs to be replaced by sendMessage
(in order to support Firefox)
I did almost the same thing with sendMessage
(documentations are exactly the same for sendRequest
and sendMessage
on Mozilla doc)
content.js :
chrome.runtime.sendMessage(data, function (result) {
// here the result should be a boolean
if (result){
alert("ok");
}
});
popup.js :
chrome.runtime.onMessage.addListener(function (message, sender, callback) {
// Here I have some asynchronous work
setTimeout(function(){
callback(true);
}, 500);
}
But now, it doesn't work. As soon as the listener function ends, the callback is called with no argument, and the asynchronous function can't send its result.
Is there any way to do the same thing in Firefox ?