Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 359
content.js
(() => {
const duration = 200;
let remaining = duration;
let intervalId = null;
let panel;
let timeDisplay;
let statusDisplay;
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainder = seconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(remainder).padStart(2, "0")}`;
}
function updateDisplay() {
timeDisplay.textContent = formatTime(remaining);
}
function stopTimer() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
}
function startTimer() {
if (intervalId !== null || remaining === 0) {
return;
}
statusDisplay.textContent = "Work through this question.";
intervalId = setInterval(() => {
remaining -= 1;
updateDisplay();
if (remaining <= 0) {
remaining = 0;
updateDisplay();
stopTimer();
statusDisplay.textContent = "200 seconds are up. Check your working.";
}
}, 1000);
}
function pauseTimer() {
stopTimer();
statusDisplay.textContent = "Paused. Resume when you’re ready.";
}
function resetTimer() {
stopTimer();
remaining = duration;
updateDisplay();
statusDisplay.textContent = "Reset for your next question.";
}
function createPanel() {
panel = document.createElement("aside");
panel.id = "sparx-study-timer";
panel.setAttribute("aria-label", "Sparx Maths study timer");
const heading = document.createElement("strong");
heading.textContent = "Study timer";
timeDisplay = document.createElement("div");
timeDisplay.className = "sparx-study-timer__time";
timeDisplay.setAttribute("aria-live", "off");
statusDisplay = document.createElement("div");
statusDisplay.className = "sparx-study-timer__status";
statusDisplay.textContent = "Start when you begin a question.";
const buttons = document.createElement("div");
buttons.className = "sparx-study-timer__buttons";
for (const [label, action] of [
["Start", startTimer],
["Pause", pauseTimer],
["Reset", resetTimer]
]) {
const button = document.createElement("button");
button.type = "button";
button.textContent = label;
button.addEventListener("click", action);
buttons.append(button);
}
panel.append(heading, timeDisplay, buttons, statusDisplay);
document.documentElement.append(panel);
updateDisplay();
}
createPanel();
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type !== "studyTimer") {
return;
}
if (message.action === "start") {
startTimer();
} else if (message.action === "pause") {
pauseTimer();
} else if (message.action === "reset") {
resetTimer();
} else {
sendResponse({ ok: false, error: "Unknown timer command." });
return;
}
sendResponse({
ok: true,
remaining,
status: statusDisplay.textContent
});
});
})();