在纯javascript中模拟真实的人类鼠标点击?

在纯javascript中模拟真实的人类鼠标点击?,javascript,google-chrome-extension,mouseevent,mouseclick-event,Javascript,Google Chrome Extension,Mouseevent,Mouseclick Event,我目前正在开发一个chrome扩展,该扩展必须自动化一些过程,但在页面中,当我单击元素时,执行了一些操作,但当我使用JavaScript以编程方式单击元素时,什么也没有发生 有人知道如何像真人一样点击它吗 event.isTrusted // readonly property 但是我怎样才能使它成为一个事件呢 我认为该网站利用点击事件的不受信任特性制定了一些预防方法 来自: 试试这个代码;它通过在按钮中心快速连续触发mousedown、mouseup和click事件来模拟鼠标左键单击元素:

我目前正在开发一个chrome扩展,该扩展必须自动化一些过程,但在页面中,当我单击元素时,执行了一些操作,但当我使用JavaScript以编程方式单击元素时,什么也没有发生

有人知道如何像真人一样点击它吗

event.isTrusted // readonly property
但是我怎样才能使它成为一个事件呢

我认为该网站利用点击事件的不受信任特性制定了一些预防方法

来自:

试试这个代码;它通过在按钮中心快速连续触发mousedown、mouseup和click事件来模拟鼠标左键单击元素:

var simulateMouseEvent = function(element, eventName, coordX, coordY) {
  element.dispatchEvent(new MouseEvent(eventName, {
    view: window,
    bubbles: true,
    cancelable: true,
    clientX: coordX,
    clientY: coordY,
    button: 0
  }));
};

var theButton = document.querySelector('ul form button');

var box = theButton.getBoundingClientRect(),
        coordX = box.left + (box.right - box.left) / 2,
        coordY = box.top + (box.bottom - box.top) / 2;

simulateMouseEvent (theButton, "mousedown", coordX, coordY);
simulateMouseEvent (theButton, "mouseup", coordX, coordY);
simulateMouseEvent (theButton, "click", coordX, coordY);
发件人:

试试这个代码;它通过在按钮中心快速连续触发mousedown、mouseup和click事件来模拟鼠标左键单击元素:

var simulateMouseEvent = function(element, eventName, coordX, coordY) {
  element.dispatchEvent(new MouseEvent(eventName, {
    view: window,
    bubbles: true,
    cancelable: true,
    clientX: coordX,
    clientY: coordY,
    button: 0
  }));
};

var theButton = document.querySelector('ul form button');

var box = theButton.getBoundingClientRect(),
        coordX = box.left + (box.right - box.left) / 2,
        coordY = box.top + (box.bottom - box.top) / 2;

simulateMouseEvent (theButton, "mousedown", coordX, coordY);
simulateMouseEvent (theButton, "mouseup", coordX, coordY);
simulateMouseEvent (theButton, "click", coordX, coordY);

您不能使用任何浏览器中的API来模拟它。您可以编写/使用一个单独的实用程序,通过与您的扩展进行通信,并使用操作系统本身的API(例如Windows中的WINAPI)发送低级鼠标事件。@wOxxOm如果您不介意,您可以给我一些更多的信息吗?先生?我没有更多的信息,请尝试搜索现有的解决方案或自己实现一个解决方案。不会更改isTrusted属性,因为它是只读的,但它可能会工作。@IvánNokonoko非常感谢它的工作!您不能使用任何浏览器中的API来模拟它。您可以编写/使用一个单独的实用程序,通过与您的扩展进行通信,并使用操作系统本身的API(例如Windows中的WINAPI)发送低级鼠标事件。@wOxxOm如果您不介意,您可以给我一些更多的信息吗?先生?我没有更多的信息,请尝试搜索现有的解决方案或自己实现一个解决方案。不会更改isTrusted属性,因为它是只读的,但它可能会工作。@IvánNokonoko非常感谢它的工作!在使用木偶师的时候点击一个元素真的很有帮助。在使用木偶师的时候点击一个元素真的很有帮助。