[英]chrome.debugger.sendCommand() Input.dispatchMouseEvent error on MV3
I'm struggling with this for hours now..我现在已经为此苦苦挣扎了几个小时..
I'm currently writing a Chrome Extensions, its goal is to automate click on a website.我目前正在编写 Chrome 扩展程序,其目标是自动点击网站。 Since the website is checking for the
isTrusted
property, I have to emulate the click from chrome.debugger
(Or at least, that the only way I found).由于该网站正在检查
isTrusted
属性,我必须模拟来自chrome.debugger
的点击(或者至少,这是我找到的唯一方法)。
I have actually no one, but two problems.我其实没有一个,而是两个问题。
1st one: If I set opts.x / opts.y "dynamically", it results in this error: Uncaught (in promise) Error: {"code":-32602,"data":"Failed to deserialize params.x - BINDINGS: mandatory field missing at position 55","message":"Invalid parameters"}
第一个:如果我“动态”设置 opts.x / opts.y,则会导致此错误:
Uncaught (in promise) Error: {"code":-32602,"data":"Failed to deserialize params.x - BINDINGS: mandatory field missing at position 55","message":"Invalid parameters"}
2nd one: Tried putting the value directly, it's working, but it is not clicking on the provided coordinates, it's actually clicking lower.第二个:尝试直接输入值,它正在工作,但它不是点击提供的坐标,它实际上是点击更低的坐标。
This is my code:这是我的代码:
background.js背景.js
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.text == "click that button please") {
let infos = []
chrome.debugger.attach({tabId: sender.tab.id}, "1.2", function() {
let clicked = false
let x = Math.floor(msg.button.offsetLeft + msg.button.offsetWidth / 2)
let y = Math.floor(msg.button.offsetTop + msg.button.offsetHeight / 2)
opts = {type: "mousePressed", button: "left", x: x, y: y, clickCount: 1}
chrome.debugger.sendCommand({tabId: sender.tab.id}, "Input.dispatchMouseEvent", opts)
.then((e) => {
infos.push(e)
});
opts.type = "mouseReleased"
chrome.debugger.sendCommand({tabId: sender.tab.id}, "Input.dispatchMouseEvent", opts)
.then((e) => {
infos.push(e)
});
})
sendResponse({clicked: infos});
} else {
sendResponse({error: 404});
}
return true;
});
content.js内容.js
var buttons = {
"market": "#app > div > div.mt-3 > div.d-flex.justify-content-between.align-items-baseline > div:nth-child(2) > button"
}
}
!async function() {
window.onclick = function(e) {
console.log(e.target, e)
}
let target = document.querySelector(buttons['market']); // Working
console.log(target);
chrome.runtime.sendMessage({text: "click that button please", button: target})
.then((result) => {
console.log('Did it clicked?', result); // result = []
});
}();
manifest.json清单.json
{
"name": "xxx",
"description": "xxx",
"version": "0.0.1",
"manifest_version": 3,
"author": "Me",
"host_permissions": [
"https://*.xxx.xxx/*"
],
"permissions": [
"debugger",
"activeTab",
"tabs",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [{
"matches": ["https://*.xxx.xxx/*"],
"js": ["content.js"]
}]
}
You can't send DOM elements via messages.您不能通过消息发送 DOM 元素。 It's not serializable, so an empty object arrives and your formula results in a
NaN
, ie not a number.它不可序列化,因此出现一个空的 object 并且您的公式导致
NaN
,即不是数字。
Solution: send an object/array with the element's coordinates.解决方案:发送带有元素坐标的对象/数组。
Your use of offsetXXX props is incorrect, AFAICT, as the documentation for dispatchMouseEvent says x/y are relative to the viewport.您对 offsetXXX 道具的使用不正确,AFAICT,因为 dispatchMouseEvent 的文档说 x/y 是相对于视口的。
Solution: getBoundingClientRect().解决方案:getBoundingClientRect()。
const bb = elem.getBoundingClientRect();
chrome.runtime.sendMessage({x: bb.left, y: bb.top});
PS An alternative solution is to replace EventTarget.prototype.addEventListener or onclick
setter of Document.prototype in the MAIN world of the page , so that your listener will call the site's listener passing a Proxy (event, handler) where handler returns true for isTrusted
. PS 另一种解决方案是在页面的 MAIN 世界中替换 Document.prototype 的 EventTarget.prototype.addEventListener 或
onclick
setter,以便您的侦听器将调用站点的侦听器传递代理(事件,处理程序),其中处理程序为isTrusted
返回 true .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.