Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
63 lines (52 loc) · 1.9 KB
/
Copy pathpopup.js
File metadata and controls
63 lines (52 loc) · 1.9 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
document.addEventListener('DOMContentLoaded', async () => {
try {
// Get current tab count
const tabs = await chrome.tabs.query({});
const currentCount = tabs.length;
// Update count display
document.getElementById('tabCount').textContent = currentCount;
// Get history data
const { history } = await chrome.storage.local.get(['history']);
// Calculate today's change
const today = new Date().toISOString().split('T')[0];
let todayChange = 0;
if (history.daily[today]) {
todayChange = currentCount - history.daily[today].startCount;
}
// Calculate this week's change
const thisWeek = getWeekNumber(new Date());
let weekChange = 0;
if (history.weekly[thisWeek]) {
weekChange = currentCount - history.weekly[thisWeek].startCount;
}
// Update change displays
updateChangeDisplay('todayChange', todayChange);
updateChangeDisplay('weekChange', weekChange);
// Set up settings link
document.getElementById('settingsLink').addEventListener('click', () => {
chrome.runtime.openOptionsPage();
});
} catch (error) {
console.error('Error loading popup:', error);
}
});
// Helper function to update change display
function updateChangeDisplay(elementId, change) {
const element = document.getElementById(elementId);
if (change > 0) {
element.textContent = `+${change}`;
element.className = 'change positive';
} else if (change < 0) {
element.textContent = change.toString();
element.className = 'change negative';
} else {
element.textContent = '0';
element.className = 'change neutral';
}
}
// Helper function to get week number
function getWeekNumber(date) {
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
const pastDaysOfYear = (date - firstDayOfYear) / 86400000;
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}