Solves Mathspace problems and auto-fills the answer boxes
当前为
// ==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();
})();