我试图制作一个自动单击程序脚本,该脚本在鼠标左键单击时运行(例如,按住鼠标左键单击垃圾邮件的点击),并希望它不仅在特定元素上运行,例如button-2
,无论鼠标在哪里。我想出了这个小脚本:
var mouseDown = 0;
document.onmouseup = function() {
--mouseDown;
}
function printMousePos(event) {
++mouseDown;
while(mouseDown){
document.elementFromPoint(event.clientX, event.clientY).click();
}
}
document.addEventListener("mousedown", printMousePos);
let clicks = 0;
document.body.onclick = function(){
clicks++;
document.body.innerText = clicks;
}
但是,如您运行代码段所示,它似乎会破坏页面(使其无响应)。为什么会这样?
谢谢!
感谢@VLAZ的评论,我尝试使用来修复无法逃避的while
循环setInterval
,但是,它似乎仍然无法正常工作。为什么会这样呢?
var mouseDown = 0;
let removeMouse = function() {
console.log(mouseDown)
--mouseDown;
}
function click(x,y){
var ev = document.createEvent("MouseEvent");
var el = document.elementFromPoint(x,y);
ev.initMouseEvent(
"click",
true, true,
window, null,
x, y, 0, 0,
false, false, false, false,
0, null
);
el.dispatchEvent(ev);
}
function printMousePos(event) {
++mouseDown;
setInterval(()=> {
if(mouseDown){
console.log(mouseDown)
click(event.clientX, event.clientY);
} else {
clearInterval()
}
}, 1)
}
document.addEventListener("mousedown", printMousePos);
document.addEventListener("mouseup", removeMouse);
var count = 0;
document.documentElement.addEventListener("mousedown", function() {
count++;
document.body.innerText = count.toString();
})
我设法拼凑一个很酷的小脚本基础的@ ROKO的答案了,但是,它似乎没有工作,像网站https://clickspeedtest.com/或https://www.clickspeedtester.com/,还是真的任何与此有关的网站。是他们的原因吗?谢谢!
这是代码:
let intervalHandler = null;
const Mouse = {
mousedown(ev){
this.isDown = true;
this.autoClick(ev)
},
mouseup(){
this.isDown = false;
clearInterval(intervalHandler)
},
mouseover(ev) {
if(intervalHandler) clearInterval(intervalHandler)
if (this.isDown) this.autoClick(ev);
},
};
Object.defineProperties(Mouse, {
isDown: {
value: false,
writable: true,
},
autoClick: {
value: (ev) => {
const ELFP = document.elementFromPoint(ev.clientX, ev.clientY);
intervalHandler = setInterval(() => {
ELFP.click();
}, 100)
}
}
});
Object.keys(Mouse).forEach(t => document.addEventListener(t, Mouse[t].bind(Mouse)));
// Demo with buttons
document.querySelectorAll("button").forEach(EL =>
EL.addEventListener("click", () => console.log(EL.id))
);
<button id="a" type="button">BUTTON 1</button>
<button id="b" type="button">BUTTON 2</button>
Mouse
对象"mousedown"
"mouseup"
"mouseover"
。isDown
布尔值)处理autoClick
在"mouseover"
事件上触发的鼠标按下还是按下(功能)。forEach
给该Window.document
对象const Mouse = {
mousedown() { this.isDown = true; },
mouseup() { this.isDown = false; },
mouseover(ev) { if (this.isDown) this.autoClick(ev); },
};
Object.defineProperties(Mouse, {
isDown: {
value: false,
writable: true,
},
autoClick: {
value: (ev) => {
const ELFP = document.elementFromPoint(ev.clientX, ev.clientY);
ELFP.click();
}
}
});
Object.keys(Mouse).forEach(t => document.addEventListener(t, Mouse[t].bind(Mouse)));
// DEMO TIME:
document.querySelectorAll("button").forEach(EL =>
EL.addEventListener("click", () => console.log(EL.id))
);
<button id="a" type="button">BUTTON 1</button>
<button id="b" type="button">BUTTON 2</button>
要持续向"click"
事件发送垃圾邮件(尽可能快),您可以执行以下操作:
const spamClick = (ev) => {
const ms = 1; // Repeat every 1 millisecond
const EL = ev.currentTarget;
ev.type === "mousedown" ? EL._itv = setInterval(() => EL.click(), ms) : clearInterval(EL._itv) ;
};
["mousedown", "mouseup", "mouseleave"].forEach(evtName =>
document.querySelectorAll("button").forEach(EL =>
EL.addEventListener(evtName, spamClick)
)
);
// FOR THIS DEMO ONLY:
document.querySelectorAll("button").forEach(EL =>
EL.addEventListener("click", () => console.log(EL.id))
);
<button id="a" type="button">BUTTON 1</button>
<button id="b" type="button">BUTTON 2</button>
附加阅读:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句