Skip to content

Instantly share code, notes, and snippets.

@jsejcksn
Last active May 22, 2020
Embed
What would you like to do?
https://ludipe.itch.io/constancia -> Automatically persevere
// https://ludipe.itch.io/constancia
// 1. Open the JS console
// 2. Switch the JS context to the embedded iframe. More info about this at:
// https://developers.google.com/web/tools/chrome-devtools/console/reference#context
// 3. Copy and paste the code below, then press Enter to run it
// 4. Close the JS console and click on Constancia to focus it again
// 5. Press the spacebar to start/stop. Press 1 to decrease speed. Press 2 to increase speed.
(async () => {
const wait = ms => new Promise(res => setTimeout(res, ms));
const createKeyboardEvent = (type, key) => new KeyboardEvent(type, {bubbles: true, key});
const sendKeyDownAndUp = async (element, key) => {
element.dispatchEvent(createKeyboardEvent('keydown', key));
await wait(2);
element.dispatchEvent(createKeyboardEvent('keyup', key));
};
const perseveranceGenerator = (function* () {
const keys = 'perseverance'.split('');
while (true) for (const key of keys) yield key;
})();
const canvas = document.querySelector('canvas');
let active = false;
let keyDelay = 200;
const toggle = async () => {
while (active) {
await sendKeyDownAndUp(canvas, perseveranceGenerator.next().value);
await wait(keyDelay);
}
};
document.addEventListener('keydown', ev => {
switch (ev.key){
case '1': {
const newKeyDelay = keyDelay + 100;
keyDelay = newKeyDelay;
break;
}
case '2': {
const newKeyDelay = keyDelay - 100;
if (newKeyDelay >= 100) keyDelay = newKeyDelay;
break;
}
default: return;
}
});
document.addEventListener('keyup', ev => {
if (ev.key === ' ') {
active = !active;
toggle();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment