im.literally.omori

make a sparx auto solver

Kodari AI
KodariNov 9 at 9:44 pm 3
SparxSolver
A browser extension that automatically solves Sparx Maths problems. It detects math questions on the page, analyzes the problem structure, and provides step-by-step solutions with the correct answer. Features include automatic answer detection, multiple solution methods, progress tracking, and a user-friendly overlay interface. The extension works across all Sparx Maths exercise types including number problems, algebra, geometry, and word problems, offering instant feedback and learning support for students.

This version detects and solves basic arithmetic expressions locally. It won’t solve every Sparx question or submit answers automatically.

Kodari can make mistakes. Review important code before using.
Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 259
content.js
(() => {
  let panel = null;
  let timer = null;

  function calculate(input) {
    const expression = input.replace(/[×✕]/g, "*").replace(/[÷]/g, "/").replace(/[−–]/g, "-");
    const tokens = expression.match(/\d+(?:\.\d+)?|[()+*/-]/g);
    if (!tokens || tokens.join("") !== expression.replace(/\s/g, "")) return null;
    let index = 0;

    function factor() {
      if (tokens[index] === "+") {
        index++;
        return factor();
      }
      if (tokens[index] === "-") {
        index++;
        return -factor();
      }
      if (tokens[index] === "(") {
        index++;
        const value = sum();
        if (tokens[index++] !== ")") throw new Error("Unclosed parenthesis");
        return value;
      }
      const token = tokens[index++];
      if (!token || !/^\d/.test(token)) throw new Error("Expected a number");
      return Number(token);
    }

    function product() {
      let value = factor();
      while (tokens[index] === "*" || tokens[index] === "/") {
        const operator = tokens[index++];
        const right = factor();
        value = operator === "*" ? value * right : value / right;
      }
      return value;
    }

    function sum() {
      let value = product();
      while (tokens[index] === "+" || tokens[index] === "-") {
        const operator = tokens[index++];
        const right = product();
        value = operator === "+" ? value + right : value - right;
      }
      return value;
    }

    try {
      const value = sum();
      if (index !== tokens.length || !Number.isFinite(value)) return null;
      return Number(value.toPrecision(12)).toString();
    } catch (error) {
      console.error("Could not parse arithmetic expression:", error);
      return null;
    }
  }

  function scan() {
    if (!panel) return;
    const results = [];
    const seen = new Set();
    const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
      acceptNode(node) {
        const parent = node.parentElement;
        if (!parent || panel.contains(parent) || parent.closest("script, style, textarea, input, select, [contenteditable=true]")) {
          return NodeFilter.FILTER_REJECT;
        }
        const style = getComputedStyle(parent);
        return style.display === "none" || style.visibility === "hidden"
          ? NodeFilter.FILTER_REJECT
          : NodeFilter.FILTER_ACCEPT;
      }
    });

    while (walker.nextNode() && results.length < 8) {
      const text = walker.currentNode.nodeValue.trim();
      if (text.length > 100) continue;
      const match = text.match(/^\s*([0-9().+\-−–×✕÷*/\s]+?)(?:\s*=\s*\??)?\s*$/);
      if (!match) continue;
      const expression = match[1].trim();
      if (!/[+\-−–×✕÷*/]/.test(expression) || !/\d/.test(expression) || seen.has(expression)) continue;
      const answer = calculate(expression);
      if (answer === null) continue;
      seen.add(expression);
      results.push(`${expression} = ${answer}`);
    }

    panel.replaceChildren();
    const title = document.createElement("strong");
    title.textContent = "Arithmetic suggestions";
    panel.append(title);
    if (results.length === 0) {
      const empty = document.createElement("p");
      empty.textContent = "No supported text expressions found.";
      panel.append(empty);
    } else {
      for (const result of results) {
        const line = document.createElement("p");
        line.textContent = result;
        panel.append(line);
      }
    }
    const note = document.createElement("p");
    note.className = "sparx-note";
    note.textContent = "Check suggestions before using them.";
    panel.append(note);
  }

  function setEnabled(enabled) {
    if (timer) {
      clearInterval(timer);
      timer = null;
    }
    if (panel) {
      panel.remove();
      panel = null;
    }
    if (!enabled) return;
    panel = document.createElement("aside");
    panel.id = "sparx-arithmetic-helper";
    panel.setAttribute("aria-label", "Arithmetic suggestions");
    document.body.append(panel);
    scan();
    timer = setInterval(scan, 3000);
  }

  chrome.storage.onChanged.addListener((changes, area) => {
    if (area === "local" && changes.enabled) setEnabled(changes.enabled.newValue === true);
  });

  chrome.storage.local.get({ enabled: false }).then((settings) => {
    setEnabled(settings.enabled);
  }).catch((error) => {
    console.error("Failed to load helper settings:", error);
  });
})();