Chrome扩展调试器未定义

时间:2018-12-08 21:10:56

标签: javascript google-chrome google-chrome-extension

我想使用在here中看到的调试器api模拟chrome扩展的可信点击事件。

但是,chrome.debugger是未定义的。

chrome.debugger.attach(target, "1.2", function() {
    chrome.debugger.sendCommand(target, "Input.dispatchMouseEvent", arguments)
})

manifest.json

"permissions": [
    "debugger", "storage"
 ]

我想念什么吗?如何有效调用chrome.debugger?当我查看chrome:// extensions中的权限时,它表明我具有“访问页面调试器后端”

1 个答案:

答案 0 :(得分:0)

您需要通过后台脚本将调试器界面附加到选项卡:

let tabId = tab.id;
let debuggeeId = { tabId };
chrome.debugger.attach(debuggeeId, version, onAttach.bind(null, debuggeeId));

然后

const onDebuggerEnabled = (debuggeeId) => {
  debuggerEnabled = true
}

const onAttach = (debuggeeId) => {
chrome.debugger.sendCommand(
    debuggeeId, "Debugger.enable", {},
    onDebuggerEnabled.bind(null, debuggeeId));
}

然后,您可以使用消息传递将内容脚本中的请求发送到后台脚本:https://developer.chrome.com/extensions/messaging

还有

if (debuggerEnabled) {
chrome.debugger.sendCommand(debuggeeId, "Input.dispatchMouseEvent", { type: "mousePressed", x: xC, y: yC, button: "left" }, function (e) { console.log('mousedown', e) });

chrome.debugger.sendCommand(debuggeeId, "Input.dispatchMouseEvent", { type: "mouseReleased", x: xC, y: yC, button: "left" }, function (e) { console.log('mouseup', e) });
} 
// xC, yC are your coordinates

这是一个有效的示例:https://github.com/Sentero-esp12/IsTrusted-event-Debugger-API

相关问题