-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
61 lines (53 loc) · 1.83 KB
/
Copy pathbackground.js
File metadata and controls
61 lines (53 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// background.js
const API = "http://localhost:3001";
// Generate a stable userId from browser fingerprint stored in chrome.storage
async function getUserId() {
const d = await chrome.storage.local.get("userId");
if (d.userId) return d.userId;
const id = "user_" + Math.random().toString(36).slice(2) + Date.now().toString(36);
await chrome.storage.local.set({ userId: id });
return id;
}
chrome.runtime.onInstalled.addListener(() => {
// Check badge every 30 minutes
chrome.alarms.create("badgeRefresh", { periodInMinutes: 30 });
refreshBadge();
});
chrome.runtime.onStartup.addListener(() => {
refreshBadge();
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "badgeRefresh") refreshBadge();
});
async function refreshBadge() {
try {
const userId = await getUserId();
const res = await fetch(`${API}/api/items/${userId}/due-today`);
const data = await res.json();
const items = data.items || [];
// Count undone reviews
const today = new Date().toISOString().split("T")[0];
let pending = 0;
items.forEach((item) => {
item.nextReviewDates.forEach((d, idx) => {
if (d === today && !(item.completedIntervals || []).includes(idx)) pending++;
});
});
await chrome.action.setBadgeText({ text: pending > 0 ? String(pending) : "" });
await chrome.action.setBadgeBackgroundColor({ color: "#ef4444" });
} catch (e) {
// server not running — clear badge
await chrome.action.setBadgeText({ text: "" });
}
}
// Expose userId and API base to popup
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "GET_USER_ID") {
getUserId().then((id) => sendResponse({ userId: id }));
return true;
}
if (msg.type === "REFRESH_BADGE") {
refreshBadge().then(() => sendResponse({ ok: true }));
return true;
}
});