Mathspace Solver (Auto-fill Answers)

Solves Mathspace problems and auto-fills the answer boxes

目前为 2025-05-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         Mathspace Solver (Auto-fill Answers)
// @namespace    http://yourdomain.com/mathspace-solver/
// @version      2.1
// @description  Solves Mathspace problems and auto-fills the answer boxes
// @author       You
// @match        https://mathspace.co/*  // This matches all Mathspace pages
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Log to check if the script is running
    console.log('Mathspace Solver script loaded');

    // Include the math.js library for solving problems
    const script = document.createElement('script');
    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.0.0/math.js';
    script.onload = () => {
        console.log('math.js library loaded');
    };
    document.body.appendChild(script);

    // Function to solve math problems
    function solveMathProblem(problem) {
        console.log(`Solving problem: ${problem}`);
        try {
            // Remove unwanted characters like "solve" from the start of the string
            let cleanedProblem = problem.replace(/solve|simplify|evaluate/g, '').trim();
            
            // Log the cleaned problem
            console.log(`Cleaned Problem: ${cleanedProblem}`);
            
            if (cleanedProblem.includes("=")) {
                // Solve equations like 2x + 3 = 7
                let [lhs, rhs] = cleanedProblem.split("=");
                console.log(`LHS: ${lhs}, RHS: ${rhs}`);

                let equation = math.parse(lhs + '-' + rhs);  // Create an equation
                let solution = math.solve(equation, 'x');  // Solve for x
                console.log(`Solution: ${solution[0]}`);
                return solution[0]; // Return the first solution
            } else {
                // Handle cases like simplifications or direct expressions
                let simplified = math.simplify(cleanedProblem);
                console.log(`Simplified: ${simplified}`);
                return simplified.toString();
            }
        } catch (e) {
            console.log(`Error solving the problem: ${e}`);
            return `Error: ${e}`;
        }
    }

    // Function to fill the Mathspace answer box
    function fillMathspaceAnswerBox(answer) {
        console.log('Attempting to fill answer box...');
        const answerBox = document.querySelector('input[type="text"], textarea');  // Assuming input boxes are <input> or <textarea>
        
        // Check if the answer box exists and fill it
        if (answerBox) {
            answerBox.value = answer;  // Fill the answer box with the solution
            answerBox.dispatchEvent(new Event('input'));  // Trigger input event to notify the page of the change
            console.log(`Filled the answer box with: ${answer}`);
        } else {
            console.log("Answer box not found.");
        }
    }

    // Function to detect and solve the problem
    function solveCurrentProblem() {
        console.log('Checking for problem text...');
        const problemText = document.querySelector('.problem-text');  // Find the problem text element (adjust class/ID)
        
        if (problemText) {
            const problem = problemText.innerText || problemText.textContent; // Get the problem text
            console.log(`Problem detected: ${problem}`);

            // Solve the problem
            const answer = solveMathProblem(problem);

            // Fill the answer box with the solution
            fillMathspaceAnswerBox(answer);
        } else {
            console.log("Problem text not found.");
        }
    }

    // Check the problem and solve automatically
    solveCurrentProblem();

})();