forked from ossd-s25/websiteTimeTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
49 lines (40 loc) · 1.69 KB
/
popup.js
File metadata and controls
49 lines (40 loc) · 1.69 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
document.addEventListener('DOMContentLoaded', async () => {
const timeList = document.getElementById('timeList');
function formatTime(ms) {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / (1000 * 60)) % 60);
const hours = Math.floor(ms / (1000 * 60 * 60));
return `${hours}h ${minutes}m ${seconds}s`;
}
async function updateTimes() {
try {
const [currentTab] = await browser.tabs.query({ active: true, currentWindow: true });
const currentDomain = new URL(currentTab.url).hostname;
const timeData = await browser.runtime.sendMessage({type: 'getCurrentTimes' });
const sortedSites = Object.entries(timeData).sort(([,a], [,b]) => b - a);
timeList.innerHTML = '';
if (sortedSites.length === 0) {
const div = document.createElement('div');
div.className = 'site-time';
div.textContent = 'No tracking data yet';
timeList.appendChild(div);
return;
}
sortedSites.forEach(([domain, time]) => {
const div = document.createElement('div');
div.className = 'site-time';
div.textContent = `${domain}: ${formatTime(time)}`;
if (domain === currentDomain) {
div.style.fontWeight = 'bold';
div.style.color = '#481f01';
}
timeList.appendChild(div);
});
} catch (error) {
console.error('Error loading time data:', error);
timeList.innerHTML = '<div class="error">Error loading tracking data</div>';
}
}
await updateTimes();
setInterval(updateTimes, 1000);
});