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);
});
})();