Mathspace Solver (Auto-fill Answers)

Solves Mathspace problems and auto-fills the answer boxes

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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
    });

})();