Mathspace Ultimate Solver (FINAL FIX)

Extracts, correctly solves, and inputs the right answer in Mathspace

目前為 2025-02-13 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Mathspace Ultimate Solver (FINAL FIX)
// @namespace    http://tampermonkey.net/
// @version      7.0
// @description  Extracts, correctly solves, and inputs the right answer in Mathspace
// @author       You
// @match        *://*.mathspace.co/*
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.9.0/math.min.js
// ==/UserScript==

(function() {
    'use strict';

    function waitForElement(selector, callback) {
        const interval = setInterval(() => {
            let element = document.querySelector(selector);
            if (element) {
                clearInterval(interval);
                callback(element);
            }
        }, 500);
    }

    function getQuestionText() {
        let questionElement = document.querySelector('.math-question');
        if (questionElement) {
            let rawQuestion = questionElement.innerText.trim();
            console.log("Extracted Question:", rawQuestion);
            return rawQuestion;
        }
        console.log("Question not found.");
        return null;
    }

    function fixMathExpression(expression) {
        return expression
            .replace(/×/g, '*')          // Convert multiplication
            .replace(/÷/g, '/')          // Convert division
            .replace(/\^/g, '**')        // Convert exponents
            .replace(/([a-zA-Z])(\d+)/g, '$1^$2')  // Convert "n4" to "n^4"
            .replace(/(\d+)\s*\/\s*(\d+)/g, '($1/$2)') // Fix fraction formatting
            .replace(/(\d)([a-zA-Z])/g, '$1*$2'); // Convert implicit multiplication (4n → 4*n)
    }

    function solveMathQuestion(expression) {
        try {
            let formattedExpression = fixMathExpression(expression);
            console.log("Formatted Equation:", formattedExpression);
            let answer = math.simplify(formattedExpression).toString();
            console.log("Solved Answer:", answer);
            return answer;
        } catch (error) {
            console.error("Error solving the equation:", error);
            return "Error";
        }
    }

    function insertAnswer(answer) {
        waitForElement('input', (inputField) => {
            inputField.value = answer;
            inputField.dispatchEvent(new Event('input', { bubbles: true }));
            console.log("Answer inserted:", answer);
            alert("Correct Answer: " + answer);
        });
    }

    function processMathspaceQuestion() {
        let questionText = getQuestionText();
        if (questionText) {
            let answer = solveMathQuestion(questionText);
            insertAnswer(answer);
        }
    }

    setTimeout(processMathspaceQuestion, 3000);
})();