Typhacker with Auto Typing and Toggle

Automates typing on Typewriter with toggle using "Arrow Down"

Versión del día 16/1/2025. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Typhacker with Auto Typing and Toggle
// @namespace    http://tampermonkey.net/
// @version      2
// @license MIT
// @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
// ==/UserScript==

(function() {
    'use strict';

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

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

    function hideElements() {
        const hudInfo = document.getElementById("hud_info");
        const hudTop2 = document.getElementById("hud_top2");
        const hudTop1 = document.getElementById("hud_top1");
        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 (hudTop1) hudTop1.style.display = "none";
        if (hudTop2) hudTop2.style.display = "none";
        if (keyboard) keyboard.style.display = "none";
        if (targetImage) targetImage.style.display = "none";
    }

    function showElements() {
        const hudInfo = document.getElementById("hud_info");
        const hudTop2 = document.getElementById("hud_top2");
        const hudTop1 = document.getElementById("hud_top1");
        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 = "";
        if (hudTop1) hudTop1.style.display = "";
        if (hudTop2) hudTop2.style.display = "";
        if (keyboard) keyboard.style.display = "";
        if (targetImage) targetImage.style.display = "";
    }

    function toggleElementsVisibility() {
        elementsHidden = !elementsHidden;
        if (elementsHidden) {
            hideElements();
        } else {
            showElements();
        }
    }

    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();
        }, 1);
    }

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

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

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