TTRS HACK

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

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 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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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         TTRS HACK
// @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);
            }
        }
    });
})();