Typhacker (new UI)

!!!TOGGLE ELEMENTS BEFORE TYPING!!! (Automates typing in Typewriter with a UI)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey 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 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.

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

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

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 (new UI)
// @namespace    http://tampermonkey.net/
// @version      3.2BETA
// @license      MIT
// @description   !!!TOGGLE ELEMENTS BEFORE TYPING!!! (Automates typing in Typewriter with a UI)
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAYCAYAAADkgu3FAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGdYAABnWARjRyu0AAATISURBVEhLnVZJbBxFFH3V3dMztifeJnGwnQXkeCIlJyRAHDhgCSRAcCBhiRCIC2I5cEAEKTeLY+CCwo1IHBAcjKKwRRghEDigAEJwQYjEwVa8Rsl4X6ane3rh/arpmbHxgfDlr+qp///7a1VZPfrUy8mh+05gcXEBZT+CpRTAv/9CopY0rZr4EScJWjIWugsFTFwagb9WgnrtzPeJVxxEznWRbW9DEsdQ4uyWybijDyjLhr+xCT8IkbvyF7CyBmuhVEIumwGiKsKyR67U18i7BdY2hqsbG0A1YPA2FhYWMDc/C0vKlWvPw2ZGlm3DcoQdgN8JI4uUpTm2yLLuwCKvsgpiJ/aSnJ3JIJtv1e1YXfdhWZZCHMVMWdJucCuT7MoqFDsdHNhlo7fVRl9+Zx7osHGYeilJwERBzDYIvm1bsEQgPUn7kn5n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g4Zm9IUEVHBac1lk+K9A7/GT2l4oDkNkeOleHfkM67PXoZ4bPpfsHrofSbUKiwNgymhABMbOuPBvTsO/MQXl8LkV5NRDjSI5f1x3Fe8xMgJEfK/Ap2bxuzHal/APSD6C85Hx/ZAAAAAASUVORK5CYII=
// @author       random russian guy
// @match        https://sg.typewriter.ch/index.php?r=typewriter/runLevel
// @match        https://sg.typewriter.ch/index.php?r=typewriter/resumeLevel
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let typingEnabled = false;
    let typingTimer = null;
    let speed = 100;
    let elementsHidden = false;


    const elementsToToggle = [
        "#hud_info",
        "#hud_top1",
        "#hud_top2",
        ".keyboard",
        'img[height="325"][width="980"][src="/assets/65968696/images/tastatur_background.svg"]'
    ];


    function toggleElementsVisibility() {
        elementsHidden = !elementsHidden;
        elementsToToggle.forEach((selector) => {
            const element = document.querySelector(selector);
            if (element) element.style.display = elementsHidden ? "none" : "";
        });
    }

    // Function to simulate typing
    function detectAndType() {
        const spans = document.querySelectorAll('span');
        spans.forEach((span) => {
            const text = span.textContent.trim();
            if (text.length === 1 && isElementInMiddle(span)) {
                typeCharacter(text);
            }
        });
    }

    function isElementInMiddle(element) {
        const rect = element.getBoundingClientRect();
        const middleX = window.innerWidth / 2;
        const middleY = window.innerHeight / 2;

        return (
            rect.left >= middleX - window.innerWidth / 4 &&
            rect.right <= middleX + window.innerWidth / 4 &&
            rect.top >= middleY - window.innerHeight / 4 &&
            rect.bottom <= middleY + window.innerHeight / 4
        );
    }

    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));
        }
    }

    function startTypingAutomation() {
        if (!typingEnabled) {
            typingEnabled = true;
            typingTimer = setInterval(detectAndType, speed);
        }
    }

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

    function updateTypingSpeed(newSpeed) {
        speed = newSpeed;
        if (typingEnabled) {
            stopTypingAutomation();
            startTypingAutomation();
        }
    }


    const controlPanel = document.createElement("div");
    controlPanel.style.position = "fixed";
    controlPanel.style.bottom = "20px";
    controlPanel.style.left = "20px";
    controlPanel.style.padding = "15px";
    controlPanel.style.backgroundColor = "#f9f9f9";
    controlPanel.style.border = "1px solid #ccc";
    controlPanel.style.borderRadius = "12px";
    controlPanel.style.boxShadow = "0 4px 12px rgba(0, 0, 0, 0.1)";
    controlPanel.style.zIndex = "10000";
    controlPanel.style.fontFamily = "Arial, sans-serif";
    controlPanel.style.fontSize = "14px";


    const speedLabel = document.createElement("label");
    speedLabel.textContent = "Speed (ms): ";
    speedLabel.style.display = "block";
    speedLabel.style.marginBottom = "8px";

    const speedSlider = document.createElement("input");
    speedSlider.type = "range";
    speedSlider.min = "1";
    speedSlider.max = "1000";
    speedSlider.value = speed;
    speedSlider.style.width = "100px";
    speedSlider.style.marginRight = "10px";
    speedSlider.addEventListener("input", (e) => {
        updateTypingSpeed(parseInt(e.target.value));
        speedInput.value = e.target.value;
    });

    const speedInput = document.createElement("input");
    speedInput.type = "number";
    speedInput.value = speed;
    speedInput.style.width = "50px";
    speedInput.addEventListener("change", (e) => {
        updateTypingSpeed(parseInt(e.target.value));
        speedSlider.value = e.target.value;
    });

    controlPanel.appendChild(speedLabel);
    controlPanel.appendChild(speedSlider);
    controlPanel.appendChild(speedInput);
    controlPanel.appendChild(document.createElement("br"));


    function createButton(label, onClick) {
        const button = document.createElement("button");
        button.textContent = label;
        button.style.margin = "5px 5px 0 0";
        button.style.padding = "8px 12px";
        button.style.border = "none";
        button.style.borderRadius = "8px";
        button.style.backgroundColor = "#007bff";
        button.style.color = "#fff";
        button.style.cursor = "pointer";
        button.style.fontSize = "14px";
        button.style.fontFamily = "Arial, sans-serif";
        button.style.transition = "background-color 0.2s";

        button.addEventListener("mouseover", () => {
            button.style.backgroundColor = "#0056b3";
        });

        button.addEventListener("mouseout", () => {
            button.style.backgroundColor = "#007bff";
        });

        button.addEventListener("click", onClick);
        return button;
    }


    controlPanel.appendChild(createButton("Start Typing", startTypingAutomation));
    controlPanel.appendChild(createButton("Stop Typing", stopTypingAutomation));
    controlPanel.appendChild(createButton("Toggle Elements", toggleElementsVisibility));

    const hideButton = createButton("Hide Controls", () => {
        controlPanel.style.display = "none";
        showButton.style.display = "block";
    });
    controlPanel.appendChild(hideButton);


    const showButton = createButton("Show Controls", () => {
        controlPanel.style.display = "block";
        showButton.style.display = "none";
    });
    showButton.style.position = "fixed";
    showButton.style.bottom = "20px";
    showButton.style.left = "20px";
    showButton.style.display = "none";
    showButton.style.padding = "6px 10px"; 
    showButton.style.fontSize = "12px";

    document.body.appendChild(controlPanel);
    document.body.appendChild(showButton);
})();