In my code for devtool.js I am listening to one-time messages like this:
chrome.devtools.panels.create("TT's Automatron", "devtool/icon.ico", "devtool/panel.html",
function(panel) {
var panelconsole;
panel.onShown.addListener(function tmp(panel) {
panel.onShown.removeListener(tmp);
panelconsole = panel;
// this works
chrome.runtime.sendMessage({type:'get-status'}, function(response) {
panelconsole.write_queue(response.globalstatus);
});;
// this does not work - cannot listen to the same messages from popup.js
// as I do it in background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
alert();
});
});
}
);
In the code I can send one-time message but cannot listen to one-time messages. Alert()
is never triggered, even if messages are sent. In background script I can listen to the messages without problems by chrome.runtime.onMessage.addListener()
, so why not in devtools?
I was reading documentation but there is not a receiving of one-time message shown, only session connection. Does it mean it is not possible?
In background script I am listening to the same messages and there it works:
// listening to the same messages as in devtools panel
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
alert('This works');
switch(request.type) {
// recognising different messages
case "start-tron":
// ..some code..
sendResponse({globalstatus: globalstatus});
break;
}
});
The messages are coming from popup script:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.runtime.sendMessage({type: "start-tron", tabid:tabs[0].id});
});
Maybe I should mention that there is also an open long-lived session connection between background and devtools:
var port = chrome.runtime.connect({name: 'automatron_console'});
port.onMessage.addListener(function(item) {
// if reference exists - panel is open
if (panelconsole) {
panelconsole.write_log(item);
}
});
So why can't I listen to the messages from popup in devtools.js as I do it in background.js?
In documentation is not directly noted, that it is not possible to receive one-time messages in a devtools panel, however there is only long-lived connection mentioned so I suppose that one-time messaging is not supported for devtools.
It seems that sending one-time messages is possible, as shown in the script above, but not receiving.
We can only use long-lived connection:
// creating communication port in devtool script
var devtools_connection = chrome.runtime.connect({name: 'devtools_connection'});
// listening to the port messages
devtools_connection.onMessage.addListener(function(request, sender, sendResponse) {
// sending response back to what sent this message
sendResponse('some response');
});
// sending messages to the port
devtools_connection.postMessage({msg: 'some message'});