20

我有 chrome devtools 面板的扩展。我可以使用chrome.devtools.inspectedWindow.eval... 向页面发送消息,但是如何在开发面板中接收消息?具体来说,我需要我的开发面板连接到页面上发生的事件。我无法让它收听我的内容脚本或背景页面上的事件。

我已经chrome.extension.sendMessage在内容脚本以及chrome.extension.onMessage.addListener开发面板脚本中进行了尝试。但sendMessage抱怨Port error: Could not establish connection. Receiving end does not exist.

长期连接问题仍然存在:

在内容脚本或背景页面中:

var port = chrome.extension.connect({name: "test"});
port.postMessage({msg: "testing"});

在开发工具面板 javascript 中:

chrome.extension.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
         // never gets here
    });
 });

如何在我的开发工具面板中侦听内容脚本中触发的事件?像这样来自 Firefox 的附加 SDK 的图表会很棒:https ://addons.mozilla.org/en-US/developers/docs/sdk/latest/static-files/media/content-scripting-overview.png

4

1 回答 1

53

目标是为通信创建一个通道(“端口”)。只要正确维护连接,如何创建端口都没有关系。

devtools 脚本必须启动端口,因为后台脚本不知道何时创建了 devtools 面板。

这是一个基本示例,它显示了双向通信方法:

devtools.js

chrome.devtools.panels.create('Test', '/icon.png', '/panel.html', function(extensionPanel) {
    var _window; // Going to hold the reference to panel.html's `window`

    var data = [];
    var port = chrome.runtime.connect({name: 'devtools'});
    port.onMessage.addListener(function(msg) {
        // Write information to the panel, if exists.
        // If we don't have a panel reference (yet), queue the data.
        if (_window) {
            _window.do_something(msg);
        } else {
            data.push(msg);
        }
    });
    
    extensionPanel.onShown.addListener(function tmp(panelWindow) {
        extensionPanel.onShown.removeListener(tmp); // Run once only
        _window = panelWindow;

        // Release queued data
        var msg;
        while (msg = data.shift()) 
            _window.do_something(msg);
        // Just to show that it's easy to talk to pass a message back:
        _window.respond = function(msg) {
            port.postMessage(msg);
        };
    });
});

现在,面板能够通过端口发送/接收消息。面板的脚本(外部脚本文件,由于CSP)可能如下所示:

panel.js

function do_something(msg) {
    document.body.textContent += '\n' + msg; // Stupid example, PoC
}
document.documentElement.onclick = function() {
    // No need to check for the existence of `respond`, because
    // the panel can only be clicked when it's visible...
    respond('Another stupid example!');
};

现在,背景页面的脚本:

background.js

var ports = [];
chrome.runtime.onConnect.addListener(function(port) {
    if (port.name !== "devtools") return;
    ports.push(port);
    // Remove port when destroyed (eg when devtools instance is closed)
    port.onDisconnect.addListener(function() {
        var i = ports.indexOf(port);
        if (i !== -1) ports.splice(i, 1);
    });
    port.onMessage.addListener(function(msg) {
        // Received message from devtools. Do something:
        console.log('Received message from devtools page', msg);
    });
});
// Function to send a message to all devtools.html views:
function notifyDevtools(msg) {
    ports.forEach(function(port) {
        port.postMessage(msg);
    });
}

要进行测试,只需notifyDevtools('Foo');在后台页面上运行(例如通过控制台)。在此演示中,消息将发送到所有开发工具。收到后,devtools 面板将包含收到的消息。

使用以下方法将扩展名放在一起:

manifest.json

{
  "name": "Test",
  "manifest_version": 2,
  "version": "1",
  "devtools_page": "devtools.html",
  "background":{"scripts":["background.js"]}
}

panel.html

<script src="panel.js"></script> <!-- Doctype etc not added for conciseness-->

devtools.html

<script src="devtools.js"></script>

也可以看看

于 2012-07-26T20:44:44.637 回答