Created
April 27, 2023 19:40
-
-
Save natterstefan/a64ef92063098d1887756ec0c4bd4308 to your computer and use it in GitHub Desktop.
JS Utils | Move Mouse Randomly Forever
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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