MathsSpace AI Solver

Uses AI to solve math problems on MathsSpace.co and display step-by-step solutions.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         MathsSpace AI Solver
// @namespace    http://mathsspace.co/
// @version      1.0
// @description  Uses AI to solve math problems on MathsSpace.co and display step-by-step solutions.
// @author       CodeCopilot
// @match        *://*.mathsspace.co/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"; // Replace with your API key

    console.log("[MathsSpace AI Solver] Script Loaded ✅");

    function findMathQuestions() {
        let questionNodes = document.querySelectorAll("p, span, div");
        let mathPattern = /(solve|calculate|what is|find the value of)\s*([\d+\-*/().^%]+)/i;

        questionNodes.forEach(node => {
            let text = node.innerText.trim();
            let match = text.match(mathPattern);

            if (match) {
                let expression = match[2];
                solveWithAI(expression, node);
            }
        });
    }

    async function solveWithAI(expression, questionNode) {
        if (questionNode.querySelector(".math-answer")) return; // Prevent duplicate answers

        let answerBox = document.createElement("div");
        answerBox.className = "math-answer";
        answerBox.style.background = "#2196F3";
        answerBox.style.color = "#fff";
        answerBox.style.padding = "8px";
        answerBox.style.marginTop = "5px";
        answerBox.style.borderRadius = "5px";
        answerBox.style.fontWeight = "bold";
        answerBox.innerText = "Solving...";

        questionNode.appendChild(answerBox);

        let response = await fetch("https://api.openai.com/v1/completions", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${OPENAI_API_KEY}`
            },
            body: JSON.stringify({
                model: "gpt-4",
                prompt: `Solve this math problem step-by-step: ${expression}`,
                max_tokens: 100,
                temperature: 0
            })
        });

        let data = await response.json();
        let solution = data.choices[0].text.trim();

        answerBox.innerText = "Solution: " + solution;
    }

    function observeChanges() {
        let observer = new MutationObserver(() => findMathQuestions());
        observer.observe(document.body, { childList: true, subtree: true });
        console.log("[MathsSpace AI Solver] Watching for changes...");
    }

    findMathQuestions();
    observeChanges();
})();