How to simulate a long left click

I am making a small jquery script that helps me do something when I'm not on my computer. Something like a bot.

This bot should perform some left-clicks on specific points on the X, Y page.

I use this code and works great: document.elementFromPoint (X, Y) .click ();

But I also need to make a long click on another point of the page. I need to simulate this: Left-click on (x, y) ... Wait 3 seconds ... Release the left-click.

Is there a way to do something like this?

Thank.

+4
source share
2 answers

Using pure JavaScript, this may not be possible. At least not for all circumstances.

, , , mousedown mouseup, " ", :

document.elementFromPoint(X, Y).mousedown();

setTimeout(() => document.elementFromPoint(X, Y).mouseup(), 3000);

, , , .

+3

mousedown mouseup

, - :

function listenForLongClick (domElement, timeout = 3000) {
    let mouseDownStarted = false;
    let timeoutCheck = null;
    domElement.addEventListener('mousedown', () => {
        mouseDownStarted = true;

        timeoutCheck = setTimeout(() => mouseDownStarted = false, timeout);
    }, false);
    domElement.addEventListener('mouseup', () => {
        clearTimeout(timeoutCheck);

        if (mouseDownStarted) { console.log('within the 3 seconds'); }

        mouseDownStarted = false;
    }, false);
}
0

Source: https://habr.com/ru/post/1684681/


All Articles