Javascript Chrome扩展:devtools面板能否接收一次性消息?

Javascript Chrome扩展:devtools面板能否接收一次性消息?,javascript,google-chrome-extension,google-chrome-devtools,messages,message-passing,Javascript,Google Chrome Extension,Google Chrome Devtools,Messages,Message Passing,在我为devtool.js编写的代码中,我听到的一次性消息如下: 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.remove

在我为devtool.js编写的代码中,我听到的一次性消息如下:

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();
        });    
    });    
  }
);
在代码中,我可以发送一次性消息,但不能收听一次性消息<代码>警报()。在后台脚本中,我可以通过
chrome.runtime.onMessage.addListener()
毫无问题地侦听消息,那么为什么不在devtools中呢

我正在阅读,但没有显示一次性消息的接收,只有会话连接。这是否意味着这是不可能的


后台脚本中,我正在收听相同的消息,它在那里工作:

// 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;
   }
});

消息来自弹出窗口脚本:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.runtime.sendMessage({type: "start-tron", tabid:tabs[0].id});
});

也许我应该提到,后台和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);
    }
});

那么,为什么我不能像在background.js中那样在devtools.js中收听弹出窗口中的消息呢?

中没有直接指出,在devtools面板中不可能接收一次性消息,但是只提到了长期连接,所以我想 devtools不支持一次性消息传递

似乎可以发送一次性消息,如上面的脚本所示,但不能接收

我们只能使用长期连接:

// 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'});

警报
可能会被阻止,请使用正确的调试:首先分离devtools(dock side=“float”在三点设置图标中),通过按Ctrl-Shift-I为devtools调用devtools,现在您可以手动设置断点或在代码中使用
调试器
语句。我遇到了几个断点,但是它什么也不做——脚本不间断地运行,在devtools for devtools中,我什么也看不到,只看到我的断点。你能把你的背景页面脚本添加到文章中,让我们看看你想要实现什么吗?