dimanche 28 juillet 2019

Simulate MouseEvents in Tampermonkey to select checkboxes

I need to simulate a whole click to fill in a list of check boxes.

not just

checkThem([].slice.call(document.querySelectorAll('input[type="checkbox"]')));


function checkThem(nodes) {
    nodes.forEach(function(n) { n.checked = true });```

}

or

checkThem([].slice.call(document.querySelectorAll('input[type="checkbox"]')));


function checkThem(nodes) {
    nodes.forEach(function(n) { n.click()});
    // document.getElementsById ("SyncInventory")[0].click();```
}

I tried using an entire MouseEvents but nothing works. The check boxes are not even checked as in the previous examples.

simulateMouseClick([].slice.call(document.querySelectorAll('input[type="checkbox"]')));


    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);
        });
    }




Aucun commentaire:

Enregistrer un commentaire