-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
96 lines (84 loc) · 3 KB
/
background.js
File metadata and controls
96 lines (84 loc) · 3 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
let tabTimers = {}; // Stores timer data keyed by tabId: { timeLeft, interval }
// This global interval runs once per second to manage all active timers.
setInterval(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTabId = (tabs.length > 0) ? tabs[0].id : null;
for (const tabIdStr in tabTimers) {
const tabId = parseInt(tabIdStr, 10);
const timer = tabTimers[tabId];
if (timer.timeLeft > 0) {
timer.timeLeft--;
}
if (timer.timeLeft === 0) {
// When time is up, reset the timer and reload the tab.
timer.timeLeft = timer.interval;
chrome.tabs.reload(tabId);
}
// Update the badge text only if the tab is the currently active one.
if (tabId === activeTabId) {
updateBadgeForTab(tabId);
}
}
});
}, 1000);
// Handles messages from the popup script.
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const tabId = message.tabId || (sender.tab && sender.tab.id);
if (!tabId) return;
switch (message.action) {
case "start_refresh":
startTimerForTab(tabId, message.interval);
sendResponse({ status: "success" });
break;
case "stop_refresh":
stopTimerForTab(tabId);
sendResponse({ status: "success" });
break;
case "get_status":
sendResponse(tabTimers[tabId] ?
{ isRunning: true, ...tabTimers[tabId] } :
{ isRunning: false }
);
break;
}
return true; // Needed for asynchronous sendResponse.
});
// Updates the badge when the user switches to a different tab.
chrome.tabs.onActivated.addListener(activeInfo => {
updateBadgeForTab(activeInfo.tabId);
});
// Cleans up the timer when a tab is closed.
chrome.tabs.onRemoved.addListener(tabId => {
stopTimerForTab(tabId);
});
function startTimerForTab(tabId, interval) {
if (tabTimers[tabId]) {
stopTimerForTab(tabId);
}
tabTimers[tabId] = {
timeLeft: interval,
interval: interval
};
// Perform an initial reload.
chrome.tabs.reload(tabId);
updateBadgeForTab(tabId);
}
function stopTimerForTab(tabId) {
if (tabTimers[tabId]) {
delete tabTimers[tabId];
}
// Ensure the badge text is cleared for the specific tab.
chrome.action.setBadgeText({ text: '', tabId: tabId });
}
function updateBadgeForTab(tabId) {
const timer = tabTimers[tabId];
if (timer) {
const minutes = Math.floor(timer.timeLeft / 60);
const seconds = timer.timeLeft % 60;
const text = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
chrome.action.setBadgeText({ text: text, tabId: tabId });
chrome.action.setBadgeBackgroundColor({ color: '#007bff', tabId: tabId });
} else {
chrome.action.setBadgeText({ text: '', tabId: tabId });
}
}