-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
44 lines (37 loc) · 869 Bytes
/
background.js
File metadata and controls
44 lines (37 loc) · 869 Bytes
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
const CLOSE_DELAY = 15000;
const timers = {};
function isBlank(tab) {
return !tab.url || tab.url === "about:blank";
}
function startTimer(tabId) {
clearTimer(tabId);
timers[tabId] = setTimeout(() => {
chrome.tabs.get(tabId, (tab) => {
if (chrome.runtime.lastError) return;
if (isBlank(tab)) {
chrome.tabs.remove(tabId);
}
});
}, CLOSE_DELAY);
}
function clearTimer(tabId) {
if (timers[tabId]) {
clearTimeout(timers[tabId]);
delete timers[tabId];
}
}
chrome.tabs.onCreated.addListener((tab) => {
if (isBlank(tab)) {
startTimer(tab.id);
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url === "about:blank" || isBlank(tab)) {
startTimer(tabId);
} else {
clearTimer(tabId);
}
});
chrome.tabs.onRemoved.addListener((tabId) => {
clearTimer(tabId);
});