some daily background fixes

This commit is contained in:
Guy Sandler 2026-08-13 16:13:08 -07:00
parent 433ff17f65
commit 2ac702887f
2 changed files with 29 additions and 23 deletions

View File

@ -150,7 +150,7 @@ chrome.runtime.onInstalled.addListener(function () {
}); });
// The NASA APOD API with the demo key is limited to 30 requests/hour and 50/day. // The NASA APOD API with the demo key is limited to 30 requests/hour and 50/day.
// All calls are serialized through this worker and gated against those limits. // Calls are serialized through this worker; the API's own 429 responses handle limiting.
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type === "getNasaBackground") { if (message?.type === "getNasaBackground") {
getNasaBackground().then(sendResponse); getNasaBackground().then(sendResponse);
@ -203,15 +203,6 @@ async function getNasaBackground() {
} }
async function callNasaApi(dateStr) { async function callNasaApi(dateStr) {
const now = Date.now();
const { nasa_api_calls = [] } = await chrome.storage.local.get("nasa_api_calls");
const lastHour = nasa_api_calls.filter(t => now - t < 3600 * 1000);
const lastDay = nasa_api_calls.filter(t => now - t < 24 * 3600 * 1000);
if (lastHour.length >= 30 || lastDay.length >= 50) {
console.warn("[CanvasRefined] NASA API rate limit reached, skipping request");
return "ratelimited";
}
let response; let response;
try { try {
response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&thumbs=true&date=${dateStr}`); response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&thumbs=true&date=${dateStr}`);
@ -220,9 +211,6 @@ async function callNasaApi(dateStr) {
return null; return null;
} }
// The request was made, so it counts against the quota
await chrome.storage.local.set({ nasa_api_calls: [...lastDay, now] });
if (response.status === 429) return "ratelimited"; if (response.status === 429) return "ratelimited";
if (!response.ok) { if (!response.ok) {
const errorData = await response.json().catch(() => ({})); const errorData = await response.json().catch(() => ({}));

View File

@ -258,7 +258,7 @@ function createNasaInfoOverlay() {
const icon = nasaInfoOverlayEl.querySelector("#nasa-info-icon"); const icon = nasaInfoOverlayEl.querySelector("#nasa-info-icon");
const panel = nasaInfoOverlayEl.querySelector("#nasa-info-panel"); const panel = nasaInfoOverlayEl.querySelector("#nasa-info-panel");
const showPanel = async () => { const populatePanel = async () => {
const dateStr = new Date().toISOString().slice(0, 10); const dateStr = new Date().toISOString().slice(0, 10);
const cacheKey = `nasa_apod_${dateStr}`; const cacheKey = `nasa_apod_${dateStr}`;
const cached = await chrome.storage.local.get(cacheKey); const cached = await chrome.storage.local.get(cacheKey);
@ -266,23 +266,41 @@ function createNasaInfoOverlay() {
const metadataKey = `nasa_apod_meta_${metaDate}`; const metadataKey = `nasa_apod_meta_${metaDate}`;
const metadata = await chrome.storage.local.get(metadataKey); const metadata = await chrome.storage.local.get(metadataKey);
const meta = metadata[metadataKey]; const meta = metadata[metadataKey];
if (meta) { if (!meta) return false;
document.getElementById("nasa-info-title").textContent = meta.title || ""; document.getElementById("nasa-info-title").textContent = meta.title || "";
document.getElementById("nasa-info-date").textContent = `Date: ${meta.date}`; document.getElementById("nasa-info-date").textContent = `Date: ${meta.date}`;
document.getElementById("nasa-info-credit").textContent = meta.copyright ? `Credit: ${meta.copyright}` : ""; document.getElementById("nasa-info-credit").textContent = meta.copyright ? `Credit: ${meta.copyright}` : "";
document.getElementById("nasa-info-explanation").textContent = meta.explanation || "No description available."; document.getElementById("nasa-info-explanation").textContent = meta.explanation || "No description available.";
panel.style.display = "block"; return true;
}
}; };
let pinned = false;
const showPanel = async () => {
if (pinned) return;
if (await populatePanel()) panel.style.display = "block";
};
const hidePanel = () => { const hidePanel = () => {
if (pinned) return;
panel.style.display = "none"; panel.style.display = "none";
}; };
const togglePanel = async () => {
if (pinned) {
pinned = false;
panel.style.display = "none";
} else {
pinned = true;
if (await populatePanel()) panel.style.display = "block";
}
};
icon.addEventListener("mouseenter", showPanel); icon.addEventListener("mouseenter", showPanel);
icon.addEventListener("mouseleave", hidePanel); icon.addEventListener("mouseleave", hidePanel);
panel.addEventListener("mouseenter", showPanel); panel.addEventListener("mouseenter", showPanel);
panel.addEventListener("mouseleave", hidePanel); panel.addEventListener("mouseleave", hidePanel);
icon.addEventListener("click", togglePanel);
contentMain.appendChild(nasaInfoOverlayEl); contentMain.appendChild(nasaInfoOverlayEl);
} }