-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
65 lines (61 loc) · 2.16 KB
/
background.js
File metadata and controls
65 lines (61 loc) · 2.16 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
let count = 0;
let target = 10000;
// listen for messages from popup.js
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// if the message is a request for the count, send the count back
if (msg.request == "count") {
sendResponse({ count: count });
// if the message is a request to reset the count, reset the count
} else if (msg.reset == "yes") {
count = 0;
target = 10000;
// save the count and target to local storage
chrome.storage.local.set({ count: count, target: target });
// set the badge text to the count
chrome.action.setBadgeText({text: String(count)});
// if the message is a request to increment the count, increment the count
} else if (msg.increment == "yes") {
count++;
// save the count to local storage
chrome.storage.local.set({ count: count });
// set the badge text to the count
chrome.action.setBadgeText({text: String(count)});
// send the count back
sendResponse({ count: count });
}
});
// get the count from local storage
chrome.storage.local.get(['count', 'target'], (data) => {
if (data.count) {
// set the count to the count from local storage
count = data.count;
chrome.action.setBadgeText({text: String(count)});
}
if (data.target) {
// set the target to the target from local storage
target = data.target;
}
});
// reset the count every 24 hours
chrome.alarms.create('resetCount', { periodInMinutes: 24 * 60 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'resetCount') {
const today = getToday();
// save the count to local storage with today's date as the key
chrome.storage.local.set({ [today]: count });
// set the count to 0
count = 0;
target = 10000;
// save the count to local storage
chrome.storage.local.set({ count: count, target: target });
chrome.action.setBadgeText({text: String(count)});
}
});
// get today's date in YYYY-MM-DD format
function getToday() {
const today = new Date();
const dd = String(today.getDate()).padStart(2, '0');
const mm = String(today.getMonth() + 1).padStart(2, '0');
const yyyy = today.getFullYear();
return yyyy + '-' + mm + '-' + dd;
}