gpt4 book ai didi

google-chrome - Chrome Devpanel 扩展与后台页面通信

转载 作者:行者123 更新时间:2023-12-03 13:14:29 25 4
gpt4 key购买 nike

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

我试过chrome.extension.sendMessage在内容脚本中,以及 chrome.extension.onMessage.addListener在开发面板脚本中。但是sendMessagePort 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

最佳答案

目标是为通信创建一个 channel (“端口”)。只要正确维护连接,如何创建端口都没有关系。
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');在背景页面上(例如 via the console )。在此演示中,消息将发送到所有开发工具。收到后,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>
也可以看看
  • How to modify content under a devtools panel in a Chrome extension?
  • chrome.devtools API
  • Message passing: Long-lived connections
  • Content Security Policy in Chrome extensions (“内联 JavaScript (...) 将不会被执行。此限制禁止内联 <script> block 和内联事件处理程序。”)
  • 关于google-chrome - Chrome Devpanel 扩展与后台页面通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661613/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com