Greasy Fork

Mathletics Auto Math Solver

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

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

// ==UserScript==
// @name         Mathletics Auto Math Solver 
// @namespace    / http://tampermonkey.net
// @version      3.0
// @description  Automatically solves Mathletics math problems when "A" is pressed.
// @author       nukerboss
// @match        https://*.mathletics.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to solve the equation (addition, subtraction, multiplication)
    function solveEquation() {
        // Wait for the question text to be visible
        const questionElement = document.getElementsByClassName('questions-text-alignment whiteTextWithShadow question-size-v4')[0];
        
        if (!questionElement) {
            console.log("Math problem element not found!");
            return;
        }
        
        // Get the equation text
        let equation = questionElement.innerText.trim();
        console.log("Detected equation:", equation);  // For debugging

        let result;

        // Check if the equation is in the format: "X + Y = Z"
        if (equation.includes("=")) {
            let parts = equation.split('=');
            let leftPart = parts[0].trim();
            let rightSide = parseInt(parts[1].trim());
            
            // Handle cases where the equation has a blank (like X + _ = Z or X × _ = Z)
            if (leftPart.includes("_")) {
                if (leftPart.includes("+")) {
                    let numbers = leftPart.split("+");
                    let leftNumber = parseInt(numbers[0].trim());
                    result = rightSide - leftNumber;
                } else if (leftPart.includes("-")) {
                    let numbers = leftPart.split("-");
                    let leftNumber = parseInt(numbers[0].trim());
                    result = leftNumber - rightSide;
                } else if (leftPart.includes("×")) {
                    let numbers = leftPart.split("×");
                    let leftNumber = parseInt(numbers[0].trim());
                    result = rightSide / leftNumber;  // Solve for missing multiplier
                }
            } else {
                // Handle standard equations
                let equationParts = leftPart.split(" ");
                let firstNum = parseInt(equationParts[0].trim());
                let operator = equationParts[1];
                let secondNum = parseInt(equationParts[2].trim());

                switch (operator) {
                    case "+":
                        result = firstNum + secondNum;
                        break;
                    case "-":
                        result = firstNum - secondNum;
                        break;
                    case "×":
                        result = firstNum * secondNum;
                        break;
                    default:
                        console.log("Unsupported operator:", operator);
                        return;
                }
            }

            // Show the result in the input box
            console.log("Calculated result:", result);  // For debugging
            document.getElementsByClassName("questions-input-adjustment questions-input-width-v3")[0].value = result;
        } else {
            console.log("Equation format not recognized.");
        }
    }

    // Event listener for the "A" key to solve the equation
    window.addEventListener("keydown", function(e) {
        if (e.key === "A" || e.key === "a") {
            solveEquation();
        }
    });
})();