Symulowanie sekwencji mousedown, click, mouseup w Tampermonkey?

Chciałbym symulować całe kliknięcie nie tylko

document.getElementsByClassName()[0].click();
Jak to zrobić? Wyniki wyszukiwania wydają się dotyczyć obsługi takich zdarzeń, a nie ich wyzwalania.
Author: Brock Adams, 2014-06-04

3 answers

Wysyłanie zdarzeń myszy. TAK:

//--- Get the first link that has "stackoverflow" in its URL.
var targetNode = document.querySelector ("a[href*='stackoverflow']");
if (targetNode) {
    //--- Simulate a natural mouse-click sequence.
    triggerMouseEvent (targetNode, "mouseover");
    triggerMouseEvent (targetNode, "mousedown");
    triggerMouseEvent (targetNode, "mouseup");
    triggerMouseEvent (targetNode, "click");
}
else
    console.log ("*** Target node not found!");

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

To działa, jeśli strona jest załadowana statycznie. Jeśli strona jest oparta na AJAX, użyj techniki jak w:


Uwaga:
Kod pytania ma błąd. Musisz przekazać nazwę klasy do tej funkcji. TAK:

document.getElementsByClassName ("SomeClassName")[0].click();
 69
Author: Brock Adams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:23

Bit Optimized

function fireMouseEvents( query, eventNames ){
    var element = document.querySelector(query);
    if(element && eventNames && eventNames.length){
        for(var index in eventNames){
            var eventName = eventNames[index];
            if(element.fireEvent ){
                element.fireEvent( 'on' + eventName );     
            } else {   
                var eventObject = document.createEvent( 'MouseEvents' );
                eventObject.initEvent( eventName, true, false );
                element.dispatchEvent(eventObject);
            }
        }
    }
}

Strzelałbyś w ten sposób

fireMouseEvents("a[href*='stackoverflow']",['mouseover','mousedown','mouseup','click']);
 4
Author: Naveen raj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-22 10:14:49

Poprawiłem kod Brocka trochę po tym, jak zadziałał zgodnie z oczekiwaniami.

Definicja:

function simulateMouseClick(targetNode) {
    function triggerMouseEvent(targetNode, eventType) {
        var clickEvent = document.createEvent('MouseEvents');
        clickEvent.initEvent(eventType, true, true);
        targetNode.dispatchEvent(clickEvent);
    }
    ["mouseover", "mousedown", "mouseup", "click"].forEach(function(eventType) { 
        triggerMouseEvent(targetNode, eventType);
    });
}

Wywołanie przykładów:

simulateMouseClick(document);

simulateMouseClick(document.querySelector("a[href*='stackoverflow']"));
 0
Author: SNag,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-28 06:01:55