Typhacker with Auto Typing and Toggle

Automates typing on Typewriter with toggle using "Arrow Down"

当前为 2025-01-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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