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?