Typhacker with Auto Typing and Toggle

Automates typing on Typewriter with toggle using "Arrow Down"

Version vom 16.01.2025. Aktuellste Version

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
})();