forked from sushrut111/chrome-tab-reloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
48 lines (41 loc) · 1.6 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
47
48
chrome.browserAction.setBadgeText({ text: "OFF" });
chrome.browserAction.setBadgeBackgroundColor({ color: "#4688F1" });
let reloads_holder = {};
const ON = 'ON';
const OFF = 'OFF';
// Main auto-reload function
chrome.storage.onChanged.addListener((whatChanged, area) => {
if(area === "local") {
const currentKey = Object.keys(whatChanged)[0];
const currentVal = whatChanged[currentKey].newValue;
if (typeof currentVal.active === 'boolean') {
if (currentVal.active) {
// Start reloading current tab
const reloadInterval = currentVal.unit === 'min' ? currentVal.input * 60 * 1000 : currentVal.input * 1000;
reloads_holder[currentKey] = setInterval(() => {
chrome.tabs.reload(parseInt(currentKey));
}, reloadInterval);
} else {
// Stop reloading
clearInterval(reloads_holder[currentKey]);
}
}
}
});
// Update badge text
chrome.tabs.onActivated.addListener(()=>{
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
let tab = tabs[0];
chrome.storage.local.get(tab.id.toString(), (items) => {
if (items[tab.id] && items[tab.id].active) {
chrome.browserAction.setBadgeText({ text: ON });
} else {
chrome.browserAction.setBadgeText({ text: OFF });
}
});
});
});
// Catch tab removed and remove the related reloader.
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
clearInterval(reloads_holder[tabId]);
});