-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
155 lines (137 loc) · 4.99 KB
/
background.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get(['dailyDistances', 'lastResetDate', 'lifetimeDistance', 'activityTimeline'], (result) => {
if (!result.dailyDistances) {
chrome.storage.local.set({
dailyDistances: {},
lastResetDate: new Date().toDateString(),
lifetimeDistance: 0,
fiveDayAverage: 0,
activityTimeline: {}
});
}
});
});
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'update') {
chrome.storage.local.clear(() => {
console.log('Local storage cleared due to extension update');
// Initialize with default values
chrome.storage.local.set({
dailyDistances: {},
lastResetDate: new Date().toDateString(),
lifetimeDistance: 0,
fiveDayAverage: 0,
activityTimeline: {}
});
});
}
});
function getTodayKey() {
return new Date().toISOString().split('T')[0];
}
function updateDistances(distance, timestamp) {
chrome.storage.local.get(['dailyDistances', 'lastResetDate', 'lifetimeDistance', 'activityTimeline'], (result) => {
const today = getTodayKey();
const dailyDistances = result.dailyDistances || {};
const activityTimeline = result.activityTimeline || {};
let lifetimeDistance = result.lifetimeDistance || 0;
// Reset if it's a new day
if (result.lastResetDate !== new Date().toDateString()) {
chrome.storage.local.set({ lastResetDate: new Date().toDateString() });
}
dailyDistances[today] = (dailyDistances[today] || 0) + distance;
lifetimeDistance += distance;
// Update activity timeline
if (!activityTimeline[today]) {
activityTimeline[today] = [];
}
activityTimeline[today].push({ distance: distance, timestamp: timestamp });
// Keep only the last 5 days
const sortedDates = Object.keys(dailyDistances).sort().slice(-5);
const last5DaysDistances = {};
sortedDates.forEach(date => {
last5DaysDistances[date] = dailyDistances[date];
});
// Calculate 5-day average
const fiveDayAverage = sortedDates.length > 0
? sortedDates.reduce((sum, date) => sum + last5DaysDistances[date], 0) / sortedDates.length
: 0;
chrome.storage.local.set({
dailyDistances: last5DaysDistances,
lifetimeDistance: lifetimeDistance,
fiveDayAverage: fiveDayAverage,
activityTimeline: activityTimeline
});
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'updateDistance') {
updateDistances(request.distance, request.timestamp);
sendResponse({ success: true });
return true;
} else if (request.type === 'getDistanceStats') {
chrome.storage.local.get(['dailyDistances', 'lifetimeDistance', 'fiveDayAverage', 'activityTimeline'], (result) => {
sendResponse({
dailyDistances: result.dailyDistances || {},
lifetimeDistance: result.lifetimeDistance || 0,
fiveDayAverage: result.fiveDayAverage || 0,
activityTimeline: result.activityTimeline || {}
});
});
return true;
} else if (request.type === 'checkDateChange') {
const dateChanged = checkDateChange();
sendResponse({ dateChanged: dateChanged });
return true;
} else if (request.type === 'manualReset') {
const today = new Date().toDateString();
chrome.storage.local.get(['dailyDistances', 'activityTimeline'], (result) => {
const dailyDistances = result.dailyDistances || {};
const activityTimeline = result.activityTimeline || {};
const todayKey = getTodayKey();
dailyDistances[todayKey] = 0;
activityTimeline[todayKey] = [];
chrome.storage.local.set({
lastResetDate: today,
dailyDistances: dailyDistances,
activityTimeline: activityTimeline
}, () => {
sendResponse({ success: true });
chrome.runtime.sendMessage({ type: 'updateStats' });
});
});
return true;
}
});
function checkDateChange() {
const today = new Date().toDateString();
chrome.storage.local.get(['lastResetDate', 'dailyDistances', 'activityTimeline'], (result) => {
if (result.lastResetDate !== today) {
const newDailyDistances = { [getTodayKey()]: 0 };
const newActivityTimeline = { [getTodayKey()]: [] };
chrome.storage.local.set({
lastResetDate: today,
dailyDistances: newDailyDistances,
activityTimeline: newActivityTimeline
}, () => {
chrome.runtime.sendMessage({ type: 'updateStats' });
});
return true; // Date changed
}
return false; // Date didn't change
});
}
function scheduleNextCheck() {
const now = new Date();
const nextCheck = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
const timeUntilNextCheck = nextCheck - now;
setTimeout(() => {
checkDateChange();
scheduleNextCheck();
}, timeUntilNextCheck);
}
// Initial check and scheduling
checkDateChange();
scheduleNextCheck();
// Also check every minute to ensure robustness
setInterval(checkDateChange, 60000);