I am looking to make a javascript code that can be put in the google console. This code should press the left button at a really high speed. I tried with a code like this:
var event = new KeyboardEvent('keydown', {
key: 'g',
ctrlKey: true
});
setInterval(function(){
for (i = 0; i < 100; i++) {
document.dispatchEvent(event);
}
}, 0);
This code only presses G. Now my problem is what defination makes it press the left mouse button.
What should I replace G with to make it press the left mouse button? This code will be used to “cheat” in capybara clicker!
Capy bara clicker: https://www.crazygames.com/game/capybara-clicker-2
Thanks, Ivar!
I tried this code, and it didnt work. I want this code to instead of pressing g at a fast rate, it should press the left mouse button!
var event = new KeyboardEvent('keydown', {
key: 'g',
ctrlKey: true
});
setInterval(function(){
for (i = 0; i < 100; i++) {
document.dispatchEvent(event);
}
}, 0);
You want a mouse event.
let event = new MouseEvent('click', {
button: 0, // Left button is represented by 0
bubbles: true,
cancelable: true
});
const element = document.querySelector('what you want to click');
setInterval(function(){
for (i = 0; i < 100; i++) {
element.dispatchEvent(event);
}
}, 0); // execute "immediately", or more accurately, the next event cycle.
You MAY want to change the 0 to something else since the game might have a debounce on the click
I tested this code on google’s homepage – it clicks many times (but of course only one click is needed here)
let event = new MouseEvent('click', {
button: 0, // Left button is represented by 0
bubbles: true,
cancelable: true
});
const btn = document.querySelector('[aria-label="I\'m Feeling Lucky"]')
setInterval(function(){
for (i = 0; i < 10; i++) {
btn.dispatchEvent(event);
}
}, 0);
Are you sure that you need to automate a keypress and not just what should happen when that key is pressed?