First of all I'm not looking for Long Lived Connections. I'm specifically looking for a way to send messages and send direct responses to these messages.
When you send messages between a content-script and a background-script it's pretty straight forward, as you use the chrome.tabs API to send/receive messages from/to the content-script. And to send/receive messages from/to the background-script you use the chrome.runtime API.
But with browser-action-popups it is a bit different because both are running in a background-context. So I suppose they both have to use the chrome.runtime API.
But that would mean I'd have to listen to chrome.runtime.onMessage
in both my browser-action-popup and in my background-script. So basically I would receive message sent from the popup in the background-script, but also in the popup itself. And the other way around it would be the same.
So yeah, this wouldn't really work:
/////////////background-script//////////////
//Send message from background-script to browser-action-popup
chrome.runtime.sendMessage({msg:"This is a message sent from the background-script to the browser-action-popup"})
.then(response => { //Receive response from the browser-action-popup
console.log(response.msg)
})
//Receive messages from browser-action-popup
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
sendResponse({msg:"This is a response message sent from the background-script"})
return true
})
...
///////////browser-action-popup/////////////
//Send message from browser-action-popup to background-script
chrome.runtime.sendMessage({msg:"This is a message sent from the browser-action-popup to the background-script"})
.then(response => { //Receive response from the background-script
console.log(response.msg)
})
//Receive message from background-script
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
sendResponse({msg:"This is a response message sent from the browser-action-popup"})
return true
})
But since they both run in a background context I was also wondering is there isn't a simpler way than sending messages: Would it be possible to share variables between the two or are they running completely isolated?
runtime.sendMessage()
is triggering its ownruntime.onMessage
listener. This bug exists in Firefox 50.1.0 (now old). In my testing, this does not happen in Firefox 51.0.1 (now the current release version of Firefox). – Moritzruntime.onMessage
to include a note regarding this bug and the fact that it can result in locking up Firefox (if you unconditionally callruntime.sendMessage()
from within aruntime.onMessage
listener). You may need to reload the page using Ctrl-F5 to see the change. – Moritzconnect
/onConnect
as one of the various methods of communicating, and mostly concentrated on the multiple other methods using exactly the same methods described in this question/answer (sendMessage()
/onMessage
). In other words, that question/answer is a superset of what you are asking here. It covers all the methods of communicating in the background context, not the limited subset discussed here. – Moritz