Skip to content

Instantly share code, notes, and snippets.

@gvoze32
Last active November 15, 2023 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvoze32/c4e214eee8f3f954ad29d635c5106fdd to your computer and use it in GitHub Desktop.
Save gvoze32/c4e214eee8f3f954ad29d635c5106fdd to your computer and use it in GitHub Desktop.
pixels idle
function simulateKeyPress(keyCode, eventType) {
var eventObj = document.createEventObject
? document.createEventObject()
: document.createEvent("Events");
if (eventObj.initEvent) {
eventObj.initEvent(eventType, true, true);
}
eventObj.keyCode = keyCode;
eventObj.which = keyCode;
document.dispatchEvent ? document.dispatchEvent(eventObj) : document.fireEvent("on" + eventType, eventObj);
const key = keyCode === 87 ? 'W' : keyCode === 83 ? 'S' : String.fromCharCode(keyCode);
console.log(`Simulated ${eventType} event for key ${key}`);
}
function automateMovementLoop() {
function moveLeftAndRight() {
simulateKeyPress(87, 'keydown');
setTimeout(function () {
simulateKeyPress(87, 'keyup');
setTimeout(function () {
simulateKeyPress(83, 'keydown');
setTimeout(function () {
simulateKeyPress(83, 'keyup');
setTimeout(function() {
randomNumber = getRandomNumberInRange(25000, 30000);
console.log(`Random timeout value: ${randomNumber / 1000} seconds`);
countdown(randomNumber / 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}
function getRandomNumberInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
let randomNumber = getRandomNumberInRange(25000, 30000);
console.log(`Random timeout value: ${randomNumber / 1000} seconds`);
function countdown(seconds) {
console.log(`Next movement in ${seconds} seconds...`);
if (seconds > 0) {
setTimeout(function () {
countdown(seconds - 1);
}, 1000);
} else {
moveLeftAndRight();
}
}
countdown(0);
}
automateMovementLoop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment