Skip to content

Instantly share code, notes, and snippets.

@natterstefan
Created April 27, 2023 19:40
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 natterstefan/a64ef92063098d1887756ec0c4bd4308 to your computer and use it in GitHub Desktop.
Save natterstefan/a64ef92063098d1887756ec0c4bd4308 to your computer and use it in GitHub Desktop.
JS Utils | Move Mouse Randomly Forever
// function to generate a random number within a range
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// function to generate a random mouse coordinate within the viewport
function getRandomCoordinate() {
const x = getRandomNumber(0, window.innerWidth);
const y = getRandomNumber(0, window.innerHeight);
return { x, y };
}
// function to trigger a mousemove event at a given coordinate
function triggerMouseEvent(element, eventType, coord) {
const event = new MouseEvent(eventType, {
view: window,
bubbles: true,
cancelable: true,
clientX: coord.x,
clientY: coord.y,
});
element.dispatchEvent(event);
}
// Log the mouse movement to the console
window.addEventListener('mousemove', (event) => {
console.log(`Mouse moved to (${event.clientX}, ${event.clientY})`);
});
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
setInterval(() => {
const randomCoord = getRandomCoordinate();
const element = document.elementFromPoint(randomCoord.x, randomCoord.y);
triggerMouseEvent(element, 'mousemove', randomCoord);
}, getRandomInt(30000, 90000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment