TTRS HACK 2025 (WORKING)

Types the answer to the first valid math equation in a highlighted block on play.ttrockstars.com, with 0.1s delay before typing.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         TTRS HACK 2025 (WORKING)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Types the answer to the first valid math equation in a highlighted block on play.ttrockstars.com, with 0.1s delay before typing.
// @author       AJM
// @match        https://play.ttrockstars.com/*
// @grant        none
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';

    function pressKey(key) {
        const keyDown = new KeyboardEvent('keydown', {
            key: key,
            code: key === 'Enter' ? 'Enter' : `Digit${key}`,
            keyCode: key === 'Enter' ? 13 : key.charCodeAt(0),
            which: key === 'Enter' ? 13 : key.charCodeAt(0),
            bubbles: true
        });
        const keyUp = new KeyboardEvent('keyup', {
            key: key,
            code: key === 'Enter' ? 'Enter' : `Digit${key}`,
            keyCode: key === 'Enter' ? 13 : key.charCodeAt(0),
            which: key === 'Enter' ? 13 : key.charCodeAt(0),
            bubbles: true
        });
        document.activeElement.dispatchEvent(keyDown);
        document.activeElement.dispatchEvent(keyUp);
    }

    function typeAnswer(answer) {
        const str = answer.toString();
        for (let i = 0; i < str.length; i++) {
            pressKey(str[i]);
        }
        pressKey('Enter');
    }

    document.addEventListener('mouseup', () => {
        const selectedText = window.getSelection().toString().trim();
        const lines = selectedText.split('\n').map(line => line.trim());

        const equationLine = lines.find(line =>
            /^[\d\s×÷\+\-\*\/=]+$/.test(line) && /[\d]+.*[×÷\*\/\+\-]/.test(line)
        );

        if (equationLine) {
            const cleaned = equationLine.replace(/=/g, '').replace(/×/g, '*').replace(/÷/g, '/').trim();
            try {
                const result = eval(cleaned);
                if (!isNaN(result)) {
                    setTimeout(() => typeAnswer(result), 100); // 0.1s delay
                }
            } catch (e) {
                console.warn('Invalid equation:', cleaned);
            }
        }
    });
})();