Typhacker with Auto Typing and Toggle

Automates typing on Typewriter with toggle using "Arrow Down"

As of 2025-01-16. See the latest version.

// ==UserScript==
// @name         Typhacker with Auto Typing and Toggle
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Automates typing on Typewriter with toggle using "Arrow Down"
// @author       random russian guy
// @match        https://sg.typewriter.ch/index.php?r=typewriter/runLevel
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const VALID_CHAR_REGEX = /^[a-zA-Z0-9À-ÖØ-öø-ÿ.,:;!?@#$%^&*()_+\-=\[\]{}|\\:;\'",<>./? ]$/;

    let lastTypedChar = null;
    let typingEnabled = false;
    let typingTimer = null;

    function hideElements() {
        const hudInfo = document.getElementById("hud_info");
        const hudTop2 = document.getElementById("hud_top2");
        const keyboard = document.querySelector(".keyboard");
        const targetImage = document.querySelector('img[height="325"][width="980"][src="/assets/65968696/images/tastatur_background.svg"]');

        if (hudInfo) hudInfo.style.display = "none";
        if (hudTop2) hudTop2.style.display = "none";
        if (keyboard) keyboard.style.display = "none";
        if (targetImage) targetImage.style.display = "none";
    }

    function isElementInMiddle(element) {
        const rect = element.getBoundingClientRect();
        const middleX = window.innerWidth / 2;
        const middleY = window.innerHeight / 2;
        
        const middleRegion = {
            left: middleX - window.innerWidth / 4,
            top: middleY - window.innerHeight / 4,
            right: middleX + window.innerWidth / 4,
            bottom: middleY + window.innerHeight / 4
        };

        return (
            rect.left >= middleRegion.left &&
            rect.right <= middleRegion.right &&
            rect.top >= middleRegion.top &&
            rect.bottom <= middleRegion.bottom
        );
    }

    function detectAndType() {
        const spans = document.querySelectorAll('span');

        spans.forEach(span => {
            const text = span.textContent.trim();

            if (text.length === 1 && VALID_CHAR_REGEX.test(text) && isElementInMiddle(span)) {
                typeCharacter(text);
            }
        });
    }

    function typeCharacter(char) {
        const typingArea = document.activeElement;
        if (typingArea && (typingArea.tagName === 'INPUT' || typingArea.tagName === 'TEXTAREA')) {
            const eventOptions = {
                key: char,
                code: `Key${char.toUpperCase()}`,
                char: char,
                keyCode: char.charCodeAt(0),
                which: char.charCodeAt(0),
                bubbles: true,
                cancelable: true
            };

            typingArea.dispatchEvent(new KeyboardEvent('keydown', eventOptions));
            typingArea.dispatchEvent(new KeyboardEvent('keypress', eventOptions));
            typingArea.dispatchEvent(new KeyboardEvent('keyup', eventOptions));

            lastTypedChar = char;
        }
    }

    function toggleTyping() {
        typingEnabled = !typingEnabled;
        if (typingEnabled) {
            startTypingAutomation();
        } else {
            stopTypingAutomation();
        }
    }

    function startTypingAutomation() {
        typingTimer = setInterval(() => {
            detectAndType();
        }, 100);
    }

    function stopTypingAutomation() {
        if (typingTimer) {
            clearInterval(typingTimer);
            typingTimer = null;
        }
    }

    function handleKeyPress(event) {
        if (event.key === 'ArrowDown') {
            toggleTyping();
        }
    }

    hideElements();
    document.addEventListener('keydown', handleKeyPress);
})();