Mathspace Solver (Auto-fill Answers)

Solves Mathspace problems and auto-fills the answer boxes

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

您需要先安装一个扩展,例如 篡改猴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      1.5
// @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';

    // 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) {
        try {
            if (problem.startsWith("solve")) {
                // Solve equations like 2x + 3 = 7
                let expr = problem.replace("solve", "").trim();
                let [lhs, rhs] = expr.split("=");
                let equation = math.parse(lhs + '-' + rhs);  // Create an equation
                let solution = math.solve(equation, 'x');  // Solve for x
                return solution[0]; // Return the first solution (can handle multiple solutions later)
            } else if (problem.startsWith("simplify")) {
                // Simplify expressions
                let expr = problem.replace("simplify", "").trim();
                let simplified = math.simplify(expr);
                return simplified.toString();
            } else {
                return "Unsupported problem type.";
            }
        } catch (e) {
            return `Error: ${e}`;
        }
    }

    // Function to fill the Mathspace answer box
    function fillMathspaceAnswerBox(answer) {
        // Look for the input element (adjust if the structure changes)
        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 (needed for dynamic UI updates)
            console.log(`Filled the answer box with: ${answer}`);
        } else {
            console.log("Answer box not found.");
        }
    }

    // Function to detect and solve the problem
    function solveCurrentProblem() {
        // Look for the problem text (adjust if the structure changes)
        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();

})();