Mathspace Solver (Auto-fill Answers)

Solves Mathspace problems and auto-fills the answer boxes

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Mathspace Solver (Auto-fill Answers)
// @namespace    http://yourdomain.com/mathspace-solver/
// @version      3.0
// @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 {
            // Clean the problem text by removing non-essential words
            let cleanedProblem = problem.replace(/solve|simplify|evaluate|find/g, '').trim();
            console.log(`Cleaned Problem: ${cleanedProblem}`);

            // Solve equations (like 2x + 3 = 7)
            if (cleanedProblem.includes("=")) {
                let [lhs, rhs] = cleanedProblem.split("=");
                lhs = lhs.trim();
                rhs = rhs.trim();
                console.log(`LHS: ${lhs}, RHS: ${rhs}`);

                // Create an equation and solve it
                let equation = math.parse(lhs + '-' + rhs);  // Ensure it is in a solvable form
                let solution = math.solve(equation, 'x');  // Solve for x
                console.log(`Solution: ${solution[0]}`);
                return solution[0]; // Return the first solution
            } else {
                // Handle other types of problems like simplification
                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.");
        }
    }

    // Wait until Mathspace content is fully loaded before running the script
    window.addEventListener('load', function() {
        console.log('Page fully loaded, running the solver script...');
        setTimeout(solveCurrentProblem, 1000); // Add delay to ensure content is fully rendered
    });

})();