Sparx Maths Auto Solver

Solves Sparx Maths homework questions using WolframAlpha

  1. // ==UserScript==
  2. // @name Sparx Maths Auto Solver
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Solves Sparx Maths homework questions using WolframAlpha
  6. // @author Your Name
  7. // @match https://www.sparxmaths.uk/student/homework
  8. // @grant GM_xmlhttpRequest
  9. // @connect api.wolframalpha.com
  10. // @run-at document-end
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. const WOLFRAM_API_KEY = "EP3QKE-3YL22GEG55"; // Your WolframAlpha API key
  15. function getMathSolution(question, callback) {
  16. const query = encodeURIComponent(question);
  17. const apiUrl = "https://api.wolframalpha.com/v2/query?input=" + query +
  18. "&format=plaintext&output=JSON&appid=" + WOLFRAM_API_KEY;
  19. GM_xmlhttpRequest({
  20. method: "GET",
  21. url: apiUrl,
  22. onload: function(response) {
  23. try {
  24. var data = JSON.parse(response.responseText);
  25. if (data.queryresult.success) {
  26. var answer = (data.queryresult.pods && data.queryresult.pods[1] &&
  27. data.queryresult.pods[1].subpods &&
  28. data.queryresult.pods[1].subpods[0] &&
  29. data.queryresult.pods[1].subpods[0].plaintext) || "No answer found";
  30. callback(answer);
  31. } else {
  32. callback("WolframAlpha couldn't solve this.");
  33. }
  34. } catch (error) {
  35. callback("Error fetching the solution.");
  36. }
  37. }
  38. });
  39. }
  40. function findMathQuestions() {
  41. var questions = document.querySelectorAll(".question-content"); // Adjust selector if needed
  42. questions.forEach(function(questionElement) {
  43. var questionText = questionElement.innerText.trim();
  44. getMathSolution(questionText, function(answer) {
  45. var answerDiv = document.createElement("div");
  46. answerDiv.style.color = "green";
  47. answerDiv.style.fontWeight = "bold";
  48. answerDiv.innerText = "Answer: " + answer;
  49. questionElement.appendChild(answerDiv);
  50. });
  51. });
  52. }
  53. setTimeout(findMathQuestions, 3000); // Wait for page to load
  54. })();