Last active
August 23, 2026 17:52
-
-
Save Xunnamius/819ac6ec90aa7b95d8854da3390c69b8 to your computer and use it in GitHub Desktop.
snek.js
This file contains hidden or 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
| // ==UserScript== | |
| // @name Snek | |
| // @namespace http://tampermonkey.net/ | |
| // @version 2026-08-22 | |
| // @description Cheat at snek | |
| // @author Xunnamius | |
| // @match https://milanandevan.wedding/ | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=milanandevan.wedding | |
| // @grant none | |
| // ==/UserScript== | |
| // Usage: copy this script, open a console on the correct web page, paste, press enter, and follow console prompts. | |
| /** | |
| * The number of milliseconds it takes for the specimen to travel one cell. | |
| * Originally derived from using `window.snekTools.startMeasureResolution()`. | |
| * | |
| * However, after cheating and taking a peek at the game's source code, it's | |
| * literally 150 so... the statistical approach worked! But, to handle the | |
| * linear speedup, we'll just spy on `setInterval`. | |
| */ | |
| const INITIAL_CONTROL_CYCLE_RESOLUTION_MS = 1500 / 10; | |
| const GRID_CELL_ROWS = 20; | |
| const GRID_CELL_COLUMNS = 20; | |
| const PREAMBLE = '[🐍]'; | |
| (function () { | |
| 'use strict'; | |
| let controlCycleIntervalMs = INITIAL_CONTROL_CYCLE_RESOLUTION_MS; | |
| let activated = false; | |
| //let scored = false; | |
| let cheatIntervalId = 0; | |
| let specimen = makeSpecimen(); | |
| const lastGridCellRowIndex = GRID_CELL_ROWS - 1; | |
| const lastGridCellColumnIndex = GRID_CELL_COLUMNS - 1; | |
| const setIntervalActual = window.setInterval; | |
| const defaultStopMeasureResolution = () => | |
| warn('nothing stopped: not currently measuring'); | |
| window.snekTools = { | |
| toggleCheating, | |
| stopMeasureResolution: defaultStopMeasureResolution, | |
| startMeasureResolution() { | |
| let starting = false; | |
| let start = 0; | |
| let end = 0; | |
| let averageResolution = 0; | |
| let highestResolution = 0; | |
| let lowestResolution = 0; | |
| const measurementIntervalId = setIntervalActual(async () => { | |
| if (checkGameIsRunning()) { | |
| starting = false; | |
| if (!start) { | |
| start = performance.now(); | |
| log('start:', start); | |
| } | |
| } else { | |
| if (start) { | |
| end = performance.now(); | |
| const latestResolution = end - start; | |
| averageResolution = !averageResolution | |
| ? latestResolution | |
| : (averageResolution + latestResolution) / 2; | |
| highestResolution = | |
| !highestResolution || latestResolution > highestResolution | |
| ? latestResolution | |
| : highestResolution; | |
| lowestResolution = | |
| !lowestResolution || latestResolution < lowestResolution | |
| ? latestResolution | |
| : lowestResolution; | |
| log('end:', end); | |
| info('resolution (latest) :', latestResolution); | |
| info('resolution (average):', averageResolution); | |
| info('resolution (highest):', highestResolution); | |
| info('resolution (lowest) :', lowestResolution); | |
| start = 0; | |
| end = 0; | |
| } | |
| if (!starting) { | |
| starting = true; | |
| startOrRestartGame(); | |
| } | |
| } | |
| }, 1); | |
| window.snekTools.stopMeasureResolution = () => { | |
| clearInterval(measurementIntervalId); | |
| window.snekTools.stopMeasureResolution = defaultStopMeasureResolution; | |
| }; | |
| log( | |
| `measuring tool ready (measurement interval id: ${measurementIntervalId})` | |
| ); | |
| info( | |
| `usage: measures how long it takes for the specimen to move across a cell. The resolution result from this function is calculated per 10 cells moved, so divide it by 10 to obtain the per-cell resolution` | |
| ); | |
| info( | |
| `usage: to stop measuring, refresh the page or execute window.snekTools.stopMeasureResolution()` | |
| ); | |
| log('started gathering measurements'); | |
| } | |
| }; | |
| window.addEventListener( | |
| 'keydown', | |
| (event) => { | |
| const isHoldingCtrlKey = event.ctrlKey; | |
| if (isHoldingCtrlKey && event.key.toLowerCase() === 's') { | |
| event.preventDefault(); | |
| event.stopPropagation(); | |
| toggleCheating(); | |
| } | |
| }, | |
| { capture: true } | |
| ); | |
| window.setInterval = (fn, ms) => { | |
| if (activated && ms !== INITIAL_CONTROL_CYCLE_RESOLUTION_MS) { | |
| //scored = true; | |
| controlCycleIntervalMs = ms; | |
| warn(`updated control cycle resolution: ${controlCycleIntervalMs}ms`); | |
| clearInterval(cheatIntervalId); | |
| cheatIntervalId = setIntervalActual(cheat, controlCycleIntervalMs); | |
| } | |
| return setIntervalActual(fn, ms); | |
| }; | |
| log('snek cheat mode loading...'); | |
| info(`theoretical maximum score: ${GRID_CELL_ROWS * GRID_CELL_COLUMNS - 1}`); | |
| info(`initial control cycle resolution: ${controlCycleIntervalMs}ms`); | |
| info(`tools available: ${Object.keys(window.snekTools).join(', ')}`); | |
| log('snek cheat mode ready'); | |
| log( | |
| 'Press ctrl+s to toggle cheat mode activation (it will start/restart the game for you)' | |
| ); | |
| function log(...args) { | |
| console.log(PREAMBLE, ...args); | |
| } | |
| function info(...args) { | |
| console.info(PREAMBLE, ...args); | |
| } | |
| function warn(...args) { | |
| console.warn(PREAMBLE, ...args); | |
| } | |
| function makeSpecimen() { | |
| return { | |
| /** | |
| * Represents what the specimen's next move(s) will be for each cycle. | |
| * | |
| * NOTE: this array must always have at least one element. | |
| * | |
| * @type {('right' | 'left' | 'up' | 'down')[]} | |
| */ | |
| nextMoves: ['right'], | |
| /** | |
| * Represents current horizontal location of the specimen (from the left). | |
| */ | |
| x: 10, | |
| /** | |
| * Represents current vertical location of the specimen (from the top). | |
| */ | |
| y: 10 | |
| }; | |
| } | |
| function checkGameIsRunning() { | |
| return !Object.values(getInterestingElements()).some((el) => | |
| el.checkVisibility() | |
| ); | |
| } | |
| function startOrRestartGame({ onlyStart = false } = {}) { | |
| if (checkGameIsRunning()) { | |
| warn( | |
| '[startOrRestartGame] ignored attempt to start/restart game that has already been started' | |
| ); | |
| } else { | |
| const { start, restart } = getInterestingElements(); | |
| const isStartVisible = start.checkVisibility(); | |
| if (isStartVisible) { | |
| log('starting game'); | |
| pressKey('w'); | |
| log('game started successfully'); | |
| } else { | |
| log('restarting game'); | |
| document.getElementById('restart-btn').click(); | |
| log('game restarted successfully'); | |
| } | |
| } | |
| } | |
| function toggleCheating() { | |
| if (activated) { | |
| clearInterval(cheatIntervalId); | |
| } else { | |
| if (checkGameIsRunning()) { | |
| warn( | |
| 'activating cheat mode in the middle of a running game is not supported!' | |
| ); | |
| return; | |
| } | |
| specimen = makeSpecimen(); | |
| startOrRestartGame(); | |
| cheat(); | |
| cheatIntervalId = setIntervalActual(cheat, controlCycleIntervalMs); | |
| } | |
| activated = !activated; | |
| log(`snek cheat mode ${activated ? 'activated' : 'deactivated'}`); | |
| } | |
| function cheat() { | |
| if (activated) { | |
| if (checkGameIsRunning()) { | |
| // if (scored) { | |
| // scored = false; | |
| // warn( | |
| // 'score detected: maintaining sync by skipping one control cycle...' | |
| // ); | |
| // return; | |
| // } | |
| const nextMove = specimen.nextMoves[0]; | |
| // ? Figure out next moves | |
| if (specimen.nextMoves.length === 1) { | |
| switch (nextMove) { | |
| case 'right': { | |
| if (specimen.x === lastGridCellColumnIndex - 1) { | |
| specimen.nextMoves.push('down', 'left'); | |
| } | |
| break; | |
| } | |
| case 'left': { | |
| // ? Leave space for vertical movement | |
| if (specimen.x === 2) { | |
| if (specimen.y === lastGridCellRowIndex) { | |
| specimen.nextMoves.push('left', 'up'); | |
| } else { | |
| specimen.nextMoves.push('down', 'right'); | |
| } | |
| } | |
| break; | |
| } | |
| case 'up': { | |
| if (specimen.y === 1) { | |
| specimen.nextMoves.push('right'); | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| if (specimen.nextMoves.length > 1) { | |
| specimen.nextMoves.shift(); | |
| } | |
| const oldXY = getSpecimenCoordString(); | |
| // ? Move once for this cycle | |
| switch (nextMove) { | |
| case 'right': { | |
| pressKey('d'); | |
| specimen.x += 1; | |
| break; | |
| } | |
| case 'left': { | |
| pressKey('a'); | |
| specimen.x -= 1; | |
| break; | |
| } | |
| case 'up': { | |
| pressKey('w'); | |
| specimen.y -= 1; | |
| break; | |
| } | |
| case 'down': { | |
| pressKey('s'); | |
| specimen.y += 1; | |
| break; | |
| } | |
| default: { | |
| throw new Error( | |
| `assertion failed: unknown specimen next move "${nextMove}"` | |
| ); | |
| } | |
| } | |
| info( | |
| `move: ${nextMove} from ${oldXY} to ${getSpecimenCoordString()} [next move(s): ${specimen.nextMoves.join( | |
| ', ' | |
| )}]` | |
| ); | |
| } else { | |
| info('game seems to have ended: deactivating cheat mode...'); | |
| toggleCheating(); | |
| } | |
| } | |
| } | |
| function getInterestingElements() { | |
| const elements = { | |
| start: null, | |
| restart: null | |
| }; | |
| Array.from(document.querySelectorAll('p')).forEach((el) => { | |
| const innerText = el.innerText.toLowerCase(); | |
| if (innerText.includes('specimen expired')) { | |
| elements.restart = el; | |
| } else if (innerText.includes('tap to begin')) { | |
| elements.start = el; | |
| } | |
| }); | |
| if (Object.values(elements).some((el) => !el)) { | |
| throw new Error( | |
| 'assertion failed: one or more expected elements not found', | |
| { cause: elements } | |
| ); | |
| } | |
| return elements; | |
| } | |
| /** | |
| * @param {'w' | 'a' | 's' | 'd'} key | |
| */ | |
| function pressKey(key) { | |
| const code = `Key${key.toUpperCase()}`; | |
| const keyCode = | |
| key === 'w' | |
| ? 87 | |
| : key === 'a' | |
| ? 65 | |
| : key === 's' | |
| ? 83 | |
| : key === 'd' | |
| ? 68 | |
| : null; | |
| if (!keyCode) { | |
| throw new Error('assertion failed: derived invalid keyCode', { | |
| cause: { key, code, keyCode } | |
| }); | |
| } | |
| document.body.dispatchEvent( | |
| new KeyboardEvent('keydown', { | |
| key, | |
| code, | |
| keyCode, | |
| which: keyCode, | |
| bubbles: true, | |
| cancelable: true | |
| }) | |
| ); | |
| document.body.dispatchEvent( | |
| new KeyboardEvent('keyup', { | |
| key, | |
| code, | |
| keyCode, | |
| which: keyCode, | |
| bubbles: true, | |
| cancelable: true | |
| }) | |
| ); | |
| } | |
| function getSpecimenCoordString() { | |
| return `(${String(specimen.x).padStart(2, '0')}, ${String( | |
| specimen.y | |
| ).padStart(2, '0')})`; | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment