为什么简单的JS自动点击程序会中断页面?

神灵

我试图制作一个自动单击程序脚本,该脚本在鼠标左键单击时运行(例如,按住鼠标左键单击垃圾邮件的点击),并希望它不仅在特定元素上运行,例如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>

罗科·C·布尔扬(Roko C.Buljan)
  • 使用默认的浏览器侦听器可迭代的事件方法创建一个Singleton Mouse对象
    "mousedown" "mouseup" "mouseover"
  • 然后,为您的Singleton分配其他不可枚举的属性:(
    isDown布尔值)处理
    autoClick"mouseover"事件上触发的鼠标按下还是按下(功能)
  • 以一种方式可枚举键(“ EventTypes”)方法分配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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么简单的JS自动点击程序会中断页面?

来自分类Dev

当线程中断时,为什么ThreadPool会中断其工作程序?

来自分类Dev

为什么我的程序会中断?为什么我收到这个错误?

来自分类Dev

为什么uint会中断我的for循环?

来自分类Dev

shared_ptr-为什么会中断?

来自分类Dev

为什么这小段JavaScript会中断?

来自分类Dev

为什么IE上的多个className声明会中断应用程序?

来自分类Dev

为什么IE上的多个className声明会中断应用程序?

来自分类Dev

为什么我的 Rails 应用程序中的字母顺序会中断?

来自分类Dev

为什么我的简单网络程序中断了?

来自分类Dev

为什么运行Meteor应用程序会中断运行其他Meter应用程序的功能?

来自分类Dev

为什么获取上载文件的FileByte会中断StreamReader?

来自分类Dev

为什么alert()会中断代码执行?

来自分类Dev

AngularJS为什么会中断自引用锚链接?

来自分类Dev

为什么functools.lru_cache会中断此功能?

来自分类Dev

为什么这样的构造会中断参考编辑?

来自分类Dev

为什么DrawPolygon会中断?它没有完全绘制

来自分类Dev

为什么多处理Julia会中断我的模块导入?

来自分类Dev

为什么Json解析会中断While循环?

来自分类Dev

为什么将参数化后查询会中断?

来自分类Dev

训练数据时为什么GPU会中断?

来自分类Dev

为什么do while循环不会中断?

来自分类Dev

为什么MatLab会中断某些变量输入?

来自分类Dev

为什么我的python脚本在编译后会中断?

来自分类Dev

为什么在for循环的setTimeout中放置警报会中断警报?

来自分类Dev

为什么添加材质模块会中断动态组件插入?

来自分类Dev

为什么这个简单的程序会因超过 10 的值而中断

来自分类Dev

为什么此技巧使文本在动态高度div中垂直居中(以及为什么会中断)?

来自分类Dev

从函数调用时,简单代码会中断

Related 相关文章

  1. 1

    为什么简单的JS自动点击程序会中断页面?

  2. 2

    当线程中断时,为什么ThreadPool会中断其工作程序?

  3. 3

    为什么我的程序会中断?为什么我收到这个错误?

  4. 4

    为什么uint会中断我的for循环?

  5. 5

    shared_ptr-为什么会中断?

  6. 6

    为什么这小段JavaScript会中断?

  7. 7

    为什么IE上的多个className声明会中断应用程序?

  8. 8

    为什么IE上的多个className声明会中断应用程序?

  9. 9

    为什么我的 Rails 应用程序中的字母顺序会中断?

  10. 10

    为什么我的简单网络程序中断了?

  11. 11

    为什么运行Meteor应用程序会中断运行其他Meter应用程序的功能?

  12. 12

    为什么获取上载文件的FileByte会中断StreamReader?

  13. 13

    为什么alert()会中断代码执行?

  14. 14

    AngularJS为什么会中断自引用锚链接?

  15. 15

    为什么functools.lru_cache会中断此功能?

  16. 16

    为什么这样的构造会中断参考编辑?

  17. 17

    为什么DrawPolygon会中断?它没有完全绘制

  18. 18

    为什么多处理Julia会中断我的模块导入?

  19. 19

    为什么Json解析会中断While循环?

  20. 20

    为什么将参数化后查询会中断?

  21. 21

    训练数据时为什么GPU会中断?

  22. 22

    为什么do while循环不会中断?

  23. 23

    为什么MatLab会中断某些变量输入?

  24. 24

    为什么我的python脚本在编译后会中断?

  25. 25

    为什么在for循环的setTimeout中放置警报会中断警报?

  26. 26

    为什么添加材质模块会中断动态组件插入?

  27. 27

    为什么这个简单的程序会因超过 10 的值而中断

  28. 28

    为什么此技巧使文本在动态高度div中垂直居中(以及为什么会中断)?

  29. 29

    从函数调用时,简单代码会中断

热门标签

归档