javascript - 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 中的权限时,它显示我有“访问页面调试器后端”
解决方案
您需要通过后台脚本将调试器界面附加到选项卡:
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
推荐阅读
- java - 如何确定浮点数数组的分数形式和最小公分母?
- vue.js - 有没有办法在使用 VueJs 时禁用默认光标位置?
- rest - REST API 资源按路径命名
- python-3.x - Tensorflow,使用 unique_with_counts 函数时出错
- r - 从r中的数据框中删除unicode字符
- python - 如何在 python 文件中使用二进制数据
- excel - 如何根据文本值在工作表之间复制行
- xmlhttprequest - 从 Google AMP 缓存调用时,使用“action-xhr”提交“AMP 表单”会导致“混合内容”错误,但在它之外可以正常工作
- file - 如何将自定义路径中的 Jenkins 控制台输出保存为文本文件
- c# - 删除 EPPlus 中的 excel 列