CS Timer Enhanced Auto-Enter

Auto-generates and enters realistic times in CS Timer with improved stability

As of 16/04/2025. See the latest version.

// ==UserScript==
// @name         CS Timer Enhanced Auto-Enter
// @namespace    http://tampermonkey.net/
// @version      2.8
// @description  Auto-generates and enters realistic times in CS Timer with improved stability
// @author       YourName
// @license      MIT
// @match        https://cstimer.net/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function simulateTyping(element, text, delay, callback) {
        let index = 0;
        element.value = "";

        function typeChar() {
            if (index < text.length) {
                element.value += text[index];
                index++;
                element.dispatchEvent(new Event('input')); // Trigger input event
                setTimeout(typeChar, delay);
            } else {
                if (callback) setTimeout(callback, 100); // Small delay before pressing Enter
            }
        }

        typeChar();
    }

    function generateTime() {
        let randomTime;
        let randomChance = Math.random();

        if (randomChance < 0.8) {
            randomTime = (Math.random() * (5.01 - 3.16) + 3.16).toFixed(2);
        } else {
            randomTime = (Math.random() * (6.06 - 1.84) + 1.84).toFixed(2);
        }

        document.getElementById("timeDisplay").innerText = `Generated Time: ${randomTime} seconds`;

        let timeInput = document.getElementById("inputTimer");
        if (timeInput) {
            timeInput.style.display = "inline-block";
            timeInput.focus();
            simulateTyping(timeInput, randomTime, 10, pressEnter);
        }
    }

    function pressEnter() {
        let timeInput = document.getElementById("inputTimer");
        if (timeInput) {
            timeInput.focus(); // Ensure focus before triggering event

            ["keydown", "keyup"].forEach(eventType => {
                let enterKeyEvent = new KeyboardEvent(eventType, { key: "Enter", code: "Enter", bubbles: true });
                timeInput.dispatchEvent(enterKeyEvent);
            });
        }
    }

    function startAutoGenerate() {
        setInterval(generateTime, 500); // Adjust time interval for better stability
    }

    function createUI() {
        let container = document.createElement("div");
        container.style = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            padding: 15px;
            background: #f9f9f9;
            border: 2px solid #ccc;
            border-radius: 5px;
            z-index: 1000;
            box-shadow: 2px 2px 10px rgba(0,0,0,0.2);
            text-align: center;
        `;

        let title = document.createElement("h3");
        title.innerText = "CS Timer Auto-Enter";
        container.appendChild(title);

        let timeDisplay = document.createElement("p");
        timeDisplay.id = "timeDisplay";
        timeDisplay.innerText = "Generated times will appear here...";
        container.appendChild(timeDisplay);

        document.body.appendChild(container);
    }

    createUI();
    startAutoGenerate();
})();