-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
46 lines (40 loc) · 1.37 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
let activeTabId = null;
let tabTimes = {};
// Load previously saved tab times from storage on startup
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get("tabTimes", (result) => {
if (result.tabTimes) {
tabTimes = result.tabTimes;
}
});
});
// Handle tab activation
chrome.tabs.onActivated.addListener((activeInfo) => {
const now = Date.now();
if (activeTabId !== null && tabTimes[activeTabId]) {
tabTimes[activeTabId].end = now;
tabTimes[activeTabId].total += tabTimes[activeTabId].end - tabTimes[activeTabId].start;
}
activeTabId = activeInfo.tabId;
if (!tabTimes[activeTabId]) {
tabTimes[activeTabId] = { start: now, end: null, total: 0 };
} else {
tabTimes[activeTabId].start = now;
}
// Save the updated tabTimes to storage
chrome.storage.local.set({ tabTimes });
});
// Handle when a tab is removed (closed)
chrome.tabs.onRemoved.addListener((tabId) => {
delete tabTimes[tabId];
chrome.storage.local.set({ tabTimes });
});
// Handle window focus changes
chrome.windows.onFocusChanged.addListener((windowId) => {
const now = Date.now();
if (windowId === chrome.windows.WINDOW_ID_NONE && activeTabId && tabTimes[activeTabId]) {
tabTimes[activeTabId].end = now;
tabTimes[activeTabId].total += tabTimes[activeTabId].end - tabTimes[activeTabId].start;
chrome.storage.local.set({ tabTimes });
}
});