TTRS Auto Answerer - Updated for 30/05/2025

Automatically answers questions in Times Tables Rock Stars, including multiplication and division, based on current webpage structure.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         TTRS Auto Answerer - Updated for 30/05/2025
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically answers questions in Times Tables Rock Stars, including multiplication and division, based on current webpage structure.
// @author       YourName
// @match        https://play.timestables.co.uk/*  // Adjust if the site URL changed
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // **Step 1: Customize selectors based on current webpage**
    /**
     * Use browser inspect element to find:
     * - The element that contains the question (e.g., class '.question-text')
     * - The input box for answers (e.g., id '#answerBox')
     * - The submit button (e.g., class '.submit-btn')
     */

    const selectors = {
        question: '.question-text',      // Example: replace with actual selector
        answerInput: '#answerBox',       // Example: replace with actual selector
        submitButton: '.submit-btn'       // Example: replace with actual selector
    };

    // **Step 2: Generate answer map for multiplication and division**
    const answerMap = {};

    // Generate multiplication tables from 1x1 to 12x12
    for (let i = 1; i <= 12; i++) {
        for (let j = 1; j <= 12; j++) {
            answerMap[`${i}x${j}`.toLowerCase()] = i * j;
            answerMap[`${j}x${i}`.toLowerCase()] = i * j; // symmetric
        }
    }

    // Generate division answers (for questions like "36 ÷ 6")
    for (let i = 1; i <= 12; i++) {
        for (let j = 1; j <= 12; j++) {
            // Handle division questions
            const dividend = i * j;
            answerMap[`${dividend}÷${j}`.toLowerCase()] = i; // e.g., 36 ÷ 6 = 6
            answerMap[`${dividend}/${j}`.toLowerCase()] = i; // also handle "/"
        }
    }

    // **Step 3: Define function to get the current question text**
    function getQuestion() {
        const questionEl = document.querySelector(selectors.question);
        if (questionEl) {
            return questionEl.innerText.trim().toLowerCase();
        }
        return null;
    }

    // **Step 4: Function to parse and answer questions**
    function answerQuestion() {
        const questionText = getQuestion();
        if (!questionText) return;

        // Normalize question for matching
        let answer = null;

        // Try to find answer in answerMap
        for (const key in answerMap) {
            if (questionText.includes(key)) {
                answer = answerMap[key];
                break;
            }
        }

        if (answer === null) {
            // Could not find answer, possibly question format is different
            console.log('Question not recognized:', questionText);
            return;
        }

        // Fill the answer input and submit
        const inputBox = document.querySelector(selectors.answerInput);
        const submitBtn = document.querySelector(selectors.submitButton);
        if (inputBox && submitBtn) {
            inputBox.value = answer;
            submitBtn.click();
        }
    }

    // **Step 5: Run the answer function periodically (e.g., every 0.33 seconds)**
    setInterval(answerQuestion, 333);

})();