简体   繁体   English

MV3 上的 chrome.debugger.sendCommand() Input.dispatchMouseEvent 错误

[英]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.

相关问题 为什么 Input.dispatchMouseEvent 不调度事件? - Why is Input.dispatchMouseEvent not dispatching an event? Socket.io 和 Chrome 扩展 MV3 - Socket.io and Chrome Extension MV3 如何从另一个带有 MV3 chrome 扩展名的 URL 下载文件? - How to download file from another URL with MV3 chrome extension? Chrome 扩展 mv3 - 在锚点单击时捕获 zip 文件 - Chrome extension mv3 - catch zip files on anchor click 如何将全局变量从 MV2 重构为在 MV3 service worker 中使用 chrome.storage? - How to refactor global variables from MV2 to using chrome.storage in MV3 service worker? 为什么 MV3 Chrome 扩展(使用 Service Workers)必须“在事件循环的第一轮注册监听器”? - Why must MV3 Chrome extensions (using Service Workers) "register listeners in the first turn of the event loop"? 如何从 DOM 中获取数据,对其进行处理,然后在 Chrome Extension MV3 中将其发回 - How to fetch data from DOM, process it and then send it back in Chrome Extension MV3 如何在 Chrome MV3 扩展中使用 declarativeNetRequest API 处理 chrome.webRequest.onHeadersReceived 事件? - How to handle chrome.webRequest.onHeadersReceived event using declarativeNetRequest API in Chrome MV3 extension? 如何获取 iframe 的帧 ID 以在 executeScript chrome 扩展 mv3 中使用? - How to get frame id of iframe for using at executeScript chrome extension mv3? chrome.scripting.executeScript MV3 | 如何从回调范围设置本地范围属性值 - chrome.scripting.executeScript MV3 | How to set local scope property value from the callback scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM