首页 > 解决方案 > Chrome 扩展调试器未定义

问题描述

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

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

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

清单.json

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

我错过了什么吗?我怎样才能有效地调用 chrome.debugger?当我查看 chrome://extensions 中的权限时,它显示我有“访问页面调试器后端”

标签: javascriptgoogle-chromegoogle-chrome-extension

解决方案


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

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


推荐阅读