Greasy Fork

Mathletics Auto Math Solver (Updated)

Automatically solves Mathletics math problems when "A" is pressed.

目前为 2024-11-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         Mathletics Auto Math Solver (Updated)
// @namespace    http://greasyfork.org
// @version      1.0
// @description  Automatically solves Mathletics math problems when "A" is pressed.
// @author       YourName
// @match        https://*.mathletics.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Wait for the page to fully load
    window.addEventListener('load', function () {
        // Listen for the "A" key press to trigger the solving of problems
        document.addEventListener('keydown', function (event) {
            if (event.key.toLowerCase() === 'a') {
                solveMathleticsProblems();
            }
        });
    });

    // Function to solve math problems on the page
    function solveMathleticsProblems() {
        // Select the elements that could contain math problems (adjusting the selector as needed)
        const mathProblems = document.querySelectorAll('div.problem, .math-problem, span.problem-text, input.problem-input');

        mathProblems.forEach(problem => {
            let problemText = problem.textContent.trim() || problem.value.trim();

            // Regex to match basic math problems (e.g., 5 + 3)
            const mathRegex = /(\d+)\s*([\+\-\*\/])\s*(\d+)/;

            const match = problemText.match(mathRegex);
            if (match) {
                const num1 = parseFloat(match[1]);
                const operator = match[2];
                const num2 = parseFloat(match[3]);

                let result;
                switch (operator) {
                    case '+':
                        result = num1 + num2;
                        break;
                    case '-':
                        result = num1 - num2;
                        break;
                    case '*':
                        result = num1 * num2;
                        break;
                    case '/':
                        result = num1 / num2;
                        break;
                    default:
                        result = 'Error';
                }

                // Display the result next to the problem or input field
                if (problem.tagName === 'INPUT' || problem.tagName === 'TEXTAREA') {
                    // If it's an input or textarea, insert the result
                    problem.value = `${problem.value} = ${result}`;
                } else {
                    // Otherwise, insert the result next to the problem
                    const resultDiv = document.createElement('span');
                    resultDiv.style.fontWeight = 'bold';
                    resultDiv.style.color = 'green';
                    resultDiv.textContent = ` = ${result}`;
                    problem.appendChild(resultDiv);
                }
            }
        });
    }
})();