Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 480
popup.js
const foxFacts = [
"Foxes use more than 40 different sounds to communicate with one another.",
"A fox’s whiskers on its legs help it feel its way through tall grass.",
"Foxes can hear small animals moving beneath snow.",
"A group of foxes is sometimes called a skulk or a leash.",
"Foxes use the Earth’s magnetic field to help them judge pouncing distance.",
"Fennec foxes have huge ears that help them lose heat in the desert."
];
const modeButton = document.getElementById("modeButton");
const modeLabel = document.getElementById("modeLabel");
const modeStatus = document.getElementById("modeStatus");
const factText = document.getElementById("factText");
const message = document.getElementById("message");
const themeButton = document.getElementById("themeButton");
let activeTabId = null;
let factIndex = Math.floor(Math.random() * foxFacts.length);
document.getElementById("today").textContent = new Intl.DateTimeFormat(undefined, {
weekday: "long",
month: "long",
day: "numeric"
}).format(new Date());
factText.textContent = foxFacts[factIndex];
function setPageMode(enabled) {
modeButton.setAttribute("aria-pressed", String(enabled));
modeLabel.textContent = enabled ? "Turn off" : "Turn on";
modeStatus.textContent = enabled
? "A warm woodland glow is wrapped around this page."
: "Add a little autumn warmth to this page.";
}
async function readPageMode(tabId) {
const results = await chrome.scripting.executeScript({
target: { tabId },
func: () => document.documentElement.classList.contains("kodari-fox-mode")
});
return Boolean(results[0]?.result);
}
async function loadPopup() {
try {
const { foxTheme } = await chrome.storage.local.get("foxTheme");
document.documentElement.dataset.theme = foxTheme === "moon" ? "moon" : "sun";
themeButton.textContent = foxTheme === "moon" ? "Switch to sunrise" : "Switch to moonlight";
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
activeTabId = tab?.id ?? null;
if (activeTabId === null) {
modeButton.disabled = true;
modeStatus.textContent = "Open a regular webpage to use woodland glow.";
return;
}
setPageMode(await readPageMode(activeTabId));
} catch (error) {
console.error("Could not load Foxglow for this page:", error);
modeButton.disabled = true;
modeStatus.textContent = "This page can’t be styled. Try a regular website.";
}
}
modeButton.addEventListener("click", async () => {
if (activeTabId === null) return;
modeButton.disabled = true;
message.textContent = "";
try {
const enabled = !(await readPageMode(activeTabId));
await chrome.scripting.executeScript({
target: { tabId: activeTabId },
files: ["content.js"]
});
if (enabled) {
await chrome.scripting.insertCSS({
target: { tabId: activeTabId },
files: ["content.css"]
});
} else {
await chrome.scripting.removeCSS({
target: { tabId: activeTabId },
files: ["content.css"]
});
}
await chrome.tabs.sendMessage(activeTabId, {
type: "FOX_MODE",
enabled
});
setPageMode(enabled);
} catch (error) {
console.error("Could not toggle woodland glow:", error);
message.textContent = "This page doesn’t allow the fox glow. Try a regular website.";
} finally {
modeButton.disabled = false;
}
});
document.getElementById("factButton").addEventListener("click", () => {
if (foxFacts.length > 1) {
let nextIndex = factIndex;
while (nextIndex === factIndex) {
nextIndex = Math.floor(Math.random() * foxFacts.length);
}
factIndex = nextIndex;
}
factText.textContent = foxFacts[factIndex];
});
themeButton.addEventListener("click", async () => {
const nextTheme = document.documentElement.dataset.theme === "moon" ? "sun" : "moon";
document.documentElement.dataset.theme = nextTheme;
themeButton.textContent = nextTheme === "moon" ? "Switch to sunrise" : "Switch to moonlight";
try {
await chrome.storage.local.set({ foxTheme: nextTheme });
} catch (error) {
console.error("Could not save Foxglow’s theme:", error);
message.textContent = "Your theme couldn’t be saved.";
}
});
loadPopup();