-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
65 lines (52 loc) · 1.38 KB
/
Copy pathbackground.js
File metadata and controls
65 lines (52 loc) · 1.38 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
62
63
64
65
chrome.runtime.onInstalled.addListener(() => {
console.log("UsageLens Installed");
});
function getToday() {
return new Date().toISOString().split("T")[0];
}
function getMonth() {
const d = new Date();
return `${d.getFullYear()}-${d.getMonth() + 1}`;
}
function resetUsageIfNeeded() {
chrome.storage.local.get(["usageData"], (res) => {
const usageData = res.usageData || {};
Object.keys(usageData).forEach((site) => {
const item = usageData[site];
if (item.lastDay !== getToday()) {
item.daily = 0;
item.lastDay = getToday();
}
if (item.lastMonth !== getMonth()) {
item.monthly = 0;
item.lastMonth = getMonth();
}
});
chrome.storage.local.set({ usageData });
});
}
/* every minute check reset */
chrome.alarms.create("usageResetCheck", {
periodInMinutes: 1
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "usageResetCheck") {
resetUsageIfNeeded();
}
});
/* optional badge count */
chrome.tabs.onActivated.addListener(() => {
chrome.storage.local.get(["usageData"], (res) => {
const data = res.usageData || {};
let total = 0;
Object.values(data).forEach((item) => {
total += item.daily || 0;
});
chrome.action.setBadgeText({
text: total ? String(total) : ""
});
chrome.action.setBadgeBackgroundColor({
color: "#8b5cf6"
});
});
});