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.5
  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. // Clean the problem text by removing non-essential words
  30. let cleanedProblem = problem.replace(/solve|simplify|evaluate|find/g, '').trim();
  31. console.log(`Cleaned Problem: ${cleanedProblem}`);
  32.  
  33. // Solve equations (like 2x + 3 = 7)
  34. if (cleanedProblem.includes("=")) {
  35. let [lhs, rhs] = cleanedProblem.split("=");
  36.  
  37. // Trim whitespaces and check for valid equation format
  38. lhs = lhs.trim();
  39. rhs = rhs.trim();
  40.  
  41. console.log(`LHS: ${lhs}, RHS: ${rhs}`);
  42.  
  43. // Create an equation and solve it
  44. let equation = math.parse(lhs + '-' + rhs); // Ensure it is in a solvable form
  45. let solution = math.solve(equation, 'x'); // Solve for x
  46. console.log(`Solution: ${solution[0]}`);
  47. return solution[0]; // Return the first solution
  48. } else {
  49. // Handle other types of problems like simplification
  50. let simplified = math.simplify(cleanedProblem);
  51. console.log(`Simplified: ${simplified}`);
  52. return simplified.toString();
  53. }
  54. } catch (e) {
  55. console.log(`Error solving the problem: ${e}`);
  56. return `Error: ${e}`;
  57. }
  58. }
  59.  
  60. // Function to fill the Mathspace answer box
  61. function fillMathspaceAnswerBox(answer) {
  62. console.log('Attempting to fill answer box...');
  63. const answerBox = document.querySelector('input[type="text"], textarea'); // Assuming input boxes are <input> or <textarea>
  64. // Check if the answer box exists and fill it
  65. if (answerBox) {
  66. answerBox.value = answer; // Fill the answer box with the solution
  67. answerBox.dispatchEvent(new Event('input')); // Trigger input event to notify the page of the change
  68. console.log(`Filled the answer box with: ${answer}`);
  69. } else {
  70. console.log("Answer box not found.");
  71. }
  72. }
  73.  
  74. // Function to detect and solve the problem
  75. function solveCurrentProblem() {
  76. console.log('Checking for problem text...');
  77. const problemText = document.querySelector('.problem-text'); // Find the problem text element (adjust class/ID)
  78. if (problemText) {
  79. const problem = problemText.innerText || problemText.textContent; // Get the problem text
  80. console.log(`Problem detected: ${problem}`);
  81.  
  82. // Solve the problem
  83. const answer = solveMathProblem(problem);
  84.  
  85. // Fill the answer box with the solution
  86. fillMathspaceAnswerBox(answer);
  87. } else {
  88. console.log("Problem text not found.");
  89. }
  90. }
  91.  
  92. // Check the problem and solve automatically
  93. solveCurrentProblem();
  94.  
  95. })();