在javascript控制台中自动点击和自动按键

时间:2016-02-29 18:15:26

标签: javascript mouseevent keyevent

我需要一些帮助。我想制作一些放入控制台的东西,然后按Enter键进行2000次自动点击2000次 ,无延迟 !以及停止此操作的关键。有人可以帮帮我吗?非常感谢!

2 个答案:

答案 0 :(得分:0)

使用jQuery:

function enter_key(ctrl, alt, shift, which) {
    var e = $.Event("keydown");
    e.ctrlKey = false;
    e.altKey = false;
    e.shiftKey = false;
    e.which = e.keyCode = 13;
    $(document.documentElement || window).trigger(e);
}
var stop = false;
for (var i=0; i<2000; ++i) {
   if (!stop) {
       enter_key();
   }
}

点击更简单:

var stop = false;
for (var i=0; i<2000; ++i) {
   if (!stop) {
       $('button').click();
   }
}

您可以通过设置:

来停止迭代
stop = true;

答案 1 :(得分:0)

我对此有所了解,我找到了一个点击特定位置的代码:

var elem = document.elementFromPoint(x, y);

elem.addEventListener('click', function() {
    console.log('clicked')
}, false);

var support = true;

try {
    if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
        support = false;
    } else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
        support = false;
    }
} catch (e) {
    support = false;
}

setInterval(function() {
    if (support) {
        var event = new MouseEvent('click');
    }else{
        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
    }
    elem.dispatchEvent(event);
},1000);

我找到了这个代码来获取鼠标位置:

function FindPosition(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)     {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)    {
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }
    // posx and posy contain the mouse position relative to the document
    // Do something with this information
}

那么如何使用此代码在自动点击代码中导入鼠标位置???