问题描述
我目前正在开发chrome扩展程序,该扩展程序必须自动执行某些过程,但是在页面中,当我单击该元素时,执行了一些操作,但是当我使用JavaScript以编程方式单击该元素时,没有任何反应.
I'm currently working on a chrome extension and the extension has to automate some process but in the page when I click the element some action performed but when I click the element programmatically with JavaScript nothing happens.
有人知道如何像真实人类一样点击它吗?
Does anyone know how to click it like real human ?
event.isTrusted // readonly property
但是我怎样才能使它成为一个事件.isTrusted = true?
but how can i make it as a event.isTrusted = true?
我认为网站通过点击事件的 isTrusted 属性做出了一些预防措施!
I think the website made some prevention method with the isTrusted property of the click event!
推荐答案
来自:
尝试使用此代码;它通过快速连续的mousedown,mouseup和在按钮中心触发的click事件来模拟鼠标在元素上的左击:
Try with this code; it simulates a mouse left click on the element by a quick succession of mousedown, mouseup and click events fired in the center of the button:
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);
这篇关于模拟纯JavaScript中的REAL HUMAN鼠标单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!