-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
154 lines (142 loc) · 5.27 KB
/
stats.js
File metadata and controls
154 lines (142 loc) · 5.27 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
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
// Progress message templates with dynamic calculations
const progressMessages = [
{
template: "You've already spent {timeLostToday} minutes on YouTube today. Do you really want to continue?",
getValue: stats => stats.timeStats?.timeLostToday || 0,
condition: stats => (stats.timeStats?.timeLostToday || 0) > 0
},
{
template: "Last session, you spent an average of {averageTime} minutes. Can you reduce it today?",
getValue: stats => {
const sessions = stats.timeStats?.sessionTimes || [];
if (sessions.length === 0) return 0;
return Math.round(sessions.reduce((a, b) => a + b, 0) / sessions.length);
},
condition: stats => (stats.timeStats?.sessionTimes?.length || 0) > 0
},
{
template: "This week, you've saved {minutes} minutes. What did you do with them?",
getValue: stats => stats.weeklyMinutes || 0,
condition: stats => stats.weeklyMinutes > 30
},
{
template: "If you replace 30 minutes of YouTube daily, that's {projectedHours} hours a year. What will you build?",
getValue: stats => Math.round((30 * 365) / 60),
condition: stats => stats.streakDays > 3
},
{
template: "You've saved {minutes} minutes today! Imagine how much that adds up over time.",
getValue: stats => stats.savedMinutes || 0,
condition: stats => stats.savedMinutes > 0
},
{
template: "Last week, you saved {weeklyMinutes} minutes. Can you beat that this week?",
getValue: stats => stats.lastWeekMinutes || 0,
condition: stats => stats.lastWeekMinutes > 0
},
{
template: "🌱 Just one small step today. What will you choose?",
condition: () => true
}
];
// Get random message from templates that matches conditions
function getRandomMessage(stats) {
const eligibleMessages = progressMessages.filter(msg => msg.condition(stats));
const randomTemplate = eligibleMessages[Math.floor(Math.random() * eligibleMessages.length)];
if (!randomTemplate.getValue) {
return randomTemplate.template;
}
const value = randomTemplate.getValue(stats);
return randomTemplate.template.replace(/\{(\w+)\}/g, (_, key) => {
if (key === 'projectedHours') return Math.round((30 * 365) / 60);
if (key === 'averageTime') return Math.round(value);
if (key === 'timeLostToday') return Math.round(value);
return value;
});
}
// Initialize progress banner
async function initializeProgressBanner() {
const progressBanner = document.getElementById('progressBanner');
if (!progressBanner) {
console.error('Progress banner element (#progressBanner) not found in the document');
return;
}
try {
// Get all stats
const result = await chrome.storage.local.get(['stats', 'timeStats']);
const stats = {
...result.stats || {
streakDays: 1,
savedMinutes: 15,
lastActive: new Date().toDateString()
},
timeStats: result.timeStats || {
timeLostToday: 0,
sessionTimes: [],
lastReset: new Date().toDateString()
}
};
// Update stats if it's a new day
const today = new Date().toDateString();
if (stats.lastActive !== today) {
stats.savedMinutes = 0;
stats.lastActive = today;
await chrome.storage.local.set({ stats });
}
// Display random message
const message = getRandomMessage(stats);
progressBanner.textContent = message;
progressBanner.classList.add('visible');
} catch (error) {
console.error('Error initializing progress:', error);
progressBanner.textContent = "🌱 Every moment is a chance to grow. What will you choose?";
progressBanner.classList.add('visible');
}
}
// Show progress message with animation
function showProgressMessage(message) {
const progressBanner = document.getElementById('progressBanner');
if (!progressBanner) return;
progressBanner.classList.remove('visible');
setTimeout(() => {
progressBanner.textContent = message;
progressBanner.classList.add('visible');
}, 300);
}
// Initialize stats
async function initializeStats() {
try {
const result = await chrome.storage.local.get(['stats', 'timeStats']);
return {
...result.stats || {
streakDays: 1,
savedMinutes: 15,
lastActive: new Date().toDateString()
},
timeStats: result.timeStats || {
timeLostToday: 0,
sessionTimes: [],
lastReset: new Date().toDateString()
}
};
} catch (error) {
console.error('Error initializing stats:', error);
return {
streakDays: 1,
savedMinutes: 15,
lastActive: new Date().toDateString(),
timeStats: {
timeLostToday: 0,
sessionTimes: [],
lastReset: new Date().toDateString()
}
};
}
}
// Export functions
window.Stats = {
initializeProgressBanner,
showProgressMessage,
getRandomMessage,
initializeStats
};