Mathspace Auto Solver

Extracts Mathspace questions and calculates answers

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Mathspace Auto Solver
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Extracts Mathspace questions and calculates answers
// @author       You
// @match        *://*.mathspace.co/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function getQuestionText() {
        // Try to find the question text inside the page
        let questionElement = document.querySelector('.css-1cs14ul'); // Change this if the selector is different
        if (questionElement) {
            return questionElement.innerText.trim();
        }
        return null;
    }

    function solveMathQuestion(question) {
        try {
            // Replace symbols (if necessary) to ensure JavaScript can evaluate it
            let formattedQuestion = question.replace(/×/g, '*').replace(/÷/g, '/');

            // Use JavaScript's eval() function to calculate the answer
            let answer = eval(formattedQuestion);
            return answer;
        } catch (error) {
            console.error("Error calculating the answer:", error);
            return "Error";
        }
    }

    function displayAnswer() {
        let questionText = getQuestionText();

        if (questionText) {
            let answer = solveMathQuestion(questionText);
            alert("Correct Answer: " + answer);
            console.log("Correct Answer:", answer);
        } else {
            console.log("Could not find the question.");
        }
    }

    // Run the script after a delay to ensure content is loaded
    setTimeout(displayAnswer, 3000);
})();