Typhacker with Auto Typing and Toggle

Automates typing on Typewriter with toggle using "Arrow Down"

目前為 2025-01-16 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();