mirror of
https://github.com/GuySandler/CanvasRefined.git
synced 2026-09-21 04:24:53 +02:00
some daily background fixes
This commit is contained in:
parent
433ff17f65
commit
2ac702887f
@ -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.
|
||||
// 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) => {
|
||||
if (message?.type === "getNasaBackground") {
|
||||
getNasaBackground().then(sendResponse);
|
||||
@ -203,15 +203,6 @@ async function getNasaBackground() {
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
// 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.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
|
||||
@ -258,7 +258,7 @@ function createNasaInfoOverlay() {
|
||||
const icon = nasaInfoOverlayEl.querySelector("#nasa-info-icon");
|
||||
const panel = nasaInfoOverlayEl.querySelector("#nasa-info-panel");
|
||||
|
||||
const showPanel = async () => {
|
||||
const populatePanel = async () => {
|
||||
const dateStr = new Date().toISOString().slice(0, 10);
|
||||
const cacheKey = `nasa_apod_${dateStr}`;
|
||||
const cached = await chrome.storage.local.get(cacheKey);
|
||||
@ -266,23 +266,41 @@ function createNasaInfoOverlay() {
|
||||
const metadataKey = `nasa_apod_meta_${metaDate}`;
|
||||
const metadata = await chrome.storage.local.get(metadataKey);
|
||||
const meta = metadata[metadataKey];
|
||||
if (meta) {
|
||||
document.getElementById("nasa-info-title").textContent = meta.title || "";
|
||||
document.getElementById("nasa-info-date").textContent = `Date: ${meta.date}`;
|
||||
document.getElementById("nasa-info-credit").textContent = meta.copyright ? `Credit: ${meta.copyright}` : "";
|
||||
document.getElementById("nasa-info-explanation").textContent = meta.explanation || "No description available.";
|
||||
panel.style.display = "block";
|
||||
}
|
||||
if (!meta) return false;
|
||||
document.getElementById("nasa-info-title").textContent = meta.title || "";
|
||||
document.getElementById("nasa-info-date").textContent = `Date: ${meta.date}`;
|
||||
document.getElementById("nasa-info-credit").textContent = meta.copyright ? `Credit: ${meta.copyright}` : "";
|
||||
document.getElementById("nasa-info-explanation").textContent = meta.explanation || "No description available.";
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
let pinned = false;
|
||||
|
||||
const showPanel = async () => {
|
||||
if (pinned) return;
|
||||
if (await populatePanel()) panel.style.display = "block";
|
||||
};
|
||||
|
||||
const hidePanel = () => {
|
||||
if (pinned) return;
|
||||
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("mouseleave", hidePanel);
|
||||
panel.addEventListener("mouseenter", showPanel);
|
||||
panel.addEventListener("mouseleave", hidePanel);
|
||||
icon.addEventListener("click", togglePanel);
|
||||
|
||||
contentMain.appendChild(nasaInfoOverlayEl);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user