Trying to create greasemonkey to check a box on a page

25 Views Asked by At

New greasemonkey user, I have put together this code to check a box name isRegex... but the check box code is skipped

console.log("Monkey script");
var chkBox,regBox;
var sQuery = "//input[@type='checkbox']"
chkBox = document.evaluate(sQuery, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for ( var i = 0; i < chkBox.snapshotLength; i++){
    regBox = chkBox.snapshotItem(i);
    if (regBox.name == "isRegex" && regBox.checked == false){
        regBox.checked = true;
    }
}
console.log("Done");

chkBox.snapshotLength looks like a method, but all documentation I have seen uses it this way. I have tried, chkBox.snapshotLength() but nothing. Pointer please?


  • tried to add a wait till the page loads?
window.addEventListener('load', function() {
    var chkBox = document.getElementsByName('isRegex');
    const clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent('click', false, true);
    chkBox.dispatchEvent(clickEvent);
    console.log(chkBox);
}, false);
console.log("Done");

Error:

Paused on exception
TypeError: chkBox.dispatchEvent is not a function
0

There are 0 best solutions below