【发布时间】:2021-04-08 23:11:17
【问题描述】:
我正在尝试开发一个 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 允许"}
并且与目标相关的事件不会被触发。我还应该做些什么来获得这些事件吗?
谢谢。
【问题讨论】:
-
这是因为默认情况下启用了自动附加模式,因此您不需要 setDiscoverTargets。如果你真的认为你需要它,那么首先使用 Target.setAutoAttach 禁用自动附加模式。
-
我也试过了,但没有运气:我从 setDiscoverTargets 方法 @wOxxOm 得到相同的
not allowed响应。其他原因可能是什么? -
没有看到代码,我不得不假设它不正确。
-
嗨@wOxxOm,我按照您的建议编辑了有关我如何使用
setAutoAttach的详细信息的问题。你认为它不正确吗? -
代码看起来不错,但我自己没有使用这些命令,所以我真的无法提供进一步的帮助。尝试更改参数,例如waitForDebuggerOnStart:
true
标签: google-chrome google-chrome-extension google-chrome-devtools chromium