Mathspace Solver (Auto-fill Answers)

Solves Mathspace problems and auto-fills the answer boxes

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

  1. // ==UserScript==
  2. // @name Mathspace Solver (Auto-fill Answers)
  3. // @namespace http://yourdomain.com/mathspace-solver/
  4. // @version 2.1
  5. // @description Solves Mathspace problems and auto-fills the answer boxes
  6. // @author You
  7. // @match https://mathspace.co/* // This matches all Mathspace pages
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Log to check if the script is running
  15. console.log('Mathspace Solver script loaded');
  16.  
  17. // Include the math.js library for solving problems
  18. const script = document.createElement('script');
  19. script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.0.0/math.js';
  20. script.onload = () => {
  21. console.log('math.js library loaded');
  22. };
  23. document.body.appendChild(script);
  24.  
  25. // Function to solve math problems
  26. function solveMathProblem(problem) {
  27. console.log(`Solving problem: ${problem}`);
  28. try {
  29. // Remove unwanted characters like "solve" from the start of the string
  30. let cleanedProblem = problem.replace(/solve|simplify|evaluate/g, '').trim();
  31. // Log the cleaned problem
  32. console.log(`Cleaned Problem: ${cleanedProblem}`);
  33. if (cleanedProblem.includes("=")) {
  34. // Solve equations like 2x + 3 = 7
  35. let [lhs, rhs] = cleanedProblem.split("=");
  36. console.log(`LHS: ${lhs}, RHS: ${rhs}`);
  37.  
  38. let equation = math.parse(lhs + '-' + rhs); // Create an equation
  39. let solution = math.solve(equation, 'x'); // Solve for x
  40. console.log(`Solution: ${solution[0]}`);
  41. return solution[0]; // Return the first solution
  42. } else {
  43. // Handle cases like simplifications or direct expressions
  44. let simplified = math.simplify(cleanedProblem);
  45. console.log(`Simplified: ${simplified}`);
  46. return simplified.toString();
  47. }
  48. } catch (e) {
  49. console.log(`Error solving the problem: ${e}`);
  50. return `Error: ${e}`;
  51. }
  52. }
  53.  
  54. // Function to fill the Mathspace answer box
  55. function fillMathspaceAnswerBox(answer) {
  56. console.log('Attempting to fill answer box...');
  57. const answerBox = document.querySelector('input[type="text"], textarea'); // Assuming input boxes are <input> or <textarea>
  58. // Check if the answer box exists and fill it
  59. if (answerBox) {
  60. answerBox.value = answer; // Fill the answer box with the solution
  61. answerBox.dispatchEvent(new Event('input')); // Trigger input event to notify the page of the change
  62. console.log(`Filled the answer box with: ${answer}`);
  63. } else {
  64. console.log("Answer box not found.");
  65. }
  66. }
  67.  
  68. // Function to detect and solve the problem
  69. function solveCurrentProblem() {
  70. console.log('Checking for problem text...');
  71. const problemText = document.querySelector('.problem-text'); // Find the problem text element (adjust class/ID)
  72. if (problemText) {
  73. const problem = problemText.innerText || problemText.textContent; // Get the problem text
  74. console.log(`Problem detected: ${problem}`);
  75.  
  76. // Solve the problem
  77. const answer = solveMathProblem(problem);
  78.  
  79. // Fill the answer box with the solution
  80. fillMathspaceAnswerBox(answer);
  81. } else {
  82. console.log("Problem text not found.");
  83. }
  84. }
  85.  
  86. // Check the problem and solve automatically
  87. solveCurrentProblem();
  88.  
  89. })();