google-chrome - chrome 扩展不允许 setDiscoverTargets
问题描述
我正在尝试开发一个 chrome 扩展,在这个扩展中,我需要与目标相关的事件(targetCreated/targetInfoChanged/targetDestroyed)。
为了实现这个目标,我通过chrome.debugger API 使用devtools 协议中的setDiscoverTargets方法。这是我正在使用的伪代码:
// attach the debugger
chrome.debugger.attach(debuggeeId, version, onAttach);
// when attach is successful send setAuthAttach to make setDiscoverTargets command work
const onAttach = (debuggeeId) => {
if (chrome.runtime.lastError) {
alert(chrome.runtime.lastError.message);
return;
}
console.log(`onAttach: ${JSON.stringify(debuggeeId)}`);
chrome.debugger.sendCommand({ tabId: myTabId }, "Target.setAutoAttach", { autoAttach: false, waitForDebuggerOnStart: false, flatten: true }, setAutoAttachHandler);
}
// when auto attach handler is successful send setDiscoverTargets method
// to enable targetCreated/targetInfoChanged/targetDestroyed events
const setAutoAttachHandler = (result) => {
if (chrome.runtime.lastError) {
console.log("error in setAutoAttachHandler:" + chrome.runtime.lastError.message);
return;
}
console.log(`setAutoAttachHandler result: ${JSON.stringify(result)}`);
chrome.debugger.sendCommand({ tabId: myTabId }, 'Target.setDiscoverTargets', { discover: true }, setDiscoverTargetsHandler);
}
// see the result of command
const setDiscoverTargetsHandler = (result) => {
if (chrome.runtime.lastError) {
console.log("error in setDiscoverTargetsHandler:" + chrome.runtime.lastError.message);
return;
}
console.log(`setDiscoverTargets result: ${JSON.stringify(result)}`);
}
根据执行上面的代码,我总是收到not allowed
错误
setDiscoverTargetsHandler 中的错误:{"code":-32000,"message":"Not allowed"}
并且不会触发与目标相关的事件。我还应该做些什么来获得这些事件吗?
谢谢你。
解决方案
从api发送命令时,不允许使用Target
和域下的方法(Browser.getVersion 除外)。但是,如果您想观察附加了调试器的选项卡或打开了开发人员工具的选项卡,那么您可以定期轮询。你会得到回应,像这样:-Browser
chrome.debugger
chrome.debugger.getTargets
[{
"attached": true,
"faviconUrl": "https://www.wikipedia.org/static/favicon/wikipedia.ico",
"id": "DEA9ED6A4A3DFB9A6FC315AF7110465B",
"tabId": 88,
"title": "Wikipedia",
"type": "page",
"url": "https://www.wikipedia.org/,
}, {
"attached": false,
"faviconUrl": "https://github.githubassets.com/favicons/favicon.svg",
"id": "3314432045C3EC141D4C7D7023606122",
"tabId": 87,
"title": "GitHub",
"type": "page",
"url": "https://github.com/"
}]
推荐阅读
- python - 使用 Plotnine 按排序顺序绘图
- java - 如何解决运行runClient时eclipse给出的错误?
- oracle - ORA-01795: 列表中的最大表达式数为 1000 perl 脚本中的错误
- c - 程序不向控制台输出文本
- python - 为 SQL Alchemy 多对多关系创建 ID
- python - 如何在不使用 Jupyter Notebook 的情况下使用 SchemDraw 绘制示意图?
- mysql - MS Outlook 和 MySQL 之间的集成
- linux - 我可以依靠能够在 Linux 上的共享内存上使用 F_SETLK 吗?
- python - 一段时间后执行一个函数
- azure - 逻辑应用文件系统连接器是否适用于 DFS?