Skip to content

Instantly share code, notes, and snippets.

@codesandtags
Created April 23, 2020 01:16
Show Gist options
  • Save codesandtags/df425afa9c670e210c39a9b5b03791ec to your computer and use it in GitHub Desktop.
Save codesandtags/df425afa9c670e210c39a9b5b03791ec to your computer and use it in GitHub Desktop.
// Probabilities
const getRandomNumberOneToHundred = () => Math.random() * 100;
const keys = {
arrowLeft : { key : "ArrowLeft", keyCode : 37, code: 37 },
arrowUp : { key : "ArrowUp", keyCode : 38, code: 38 },
arrowRight : { key : "ArrowRight", keyCode : 39, code: 39 },
arrowDown : { key : "ArrowDown", keyCode : 40, code: 40 },
one : { key : "1", keyCode : 49, code: 49 },
two : { key : "2", keyCode : 50, code: 50 }
}
const simulateKeyEvent = (eventName, eventData) => {
var keyEvent = new KeyboardEvent(eventName, {
char : "",
shiftKey: false,
bubbles : true,
cancelable : true,
...eventData
});
document.body.dispatchEvent(keyEvent);
}
// The logic
const createLike = () => {
getRandomNumberOneToHundred() <= 70
? simulateKeyEvent('keyup', keys.one)
: simulateKeyEvent('keyup', keys.two);
}
// Here is where magic happens
setInterval(createLike, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment