-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
106 lines (98 loc) · 2.84 KB
/
Copy pathbackground.js
File metadata and controls
106 lines (98 loc) · 2.84 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
97
98
99
100
101
102
103
104
105
106
// Function to group similar tabs
function groupSimilarTabs() {
chrome.tabs.query({}, (tabs) => {
const groups = {};
tabs.forEach((tab) => {
const domain = new URL(tab.url).hostname;
if (!groups[domain]) {
groups[domain] = [];
}
groups[domain].push(tab.id);
});
Object.values(groups).forEach((group) => {
if (group.length > 1) {
chrome.tabs.group({ tabIds: group });
}
});
});
}
// Function to sleep inactive tabs
function sleepInactiveTabs() {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (!tab.active) {
chrome.tabs.discard(tab.id);
}
});
});
}
// Function to close unused tabs
function closeUnusedTabs() {
const timeThreshold = 30 * 60 * 1000; // 30 minutes
chrome.tabs.query({}, (tabs) => {
const currentTime = Date.now();
tabs.forEach((tab) => {
if (currentTime - tab.lastAccessed > timeThreshold) {
chrome.tabs.remove(tab.id);
}
});
});
}
// Function to close duplicate tabs
function closeDuplicateTabs() {
chrome.tabs.query({}, (tabs) => {
const uniqueUrls = new Set();
tabs.forEach((tab) => {
if (uniqueUrls.has(tab.url)) {
chrome.tabs.remove(tab.id);
} else {
uniqueUrls.add(tab.url);
}
});
});
}
// Notify the user when tab is about to close
function showTabCloseNotification(tab) {
chrome.storage.sync.get(['tabSavvySettings'], function(result) {
const settings = result.tabSavvySettings || {};
if (settings.notifyTabClose && tab) {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icon.png', // Path to your icon
title: 'Tab Closed',
message: 'Go to Settings to disable this notification'
});
}
});
}
// Detect tab close and show notification if enabled
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
chrome.storage.sync.get('tabSavvySettings', function(result) {
const settings = result.tabSavvySettings || defaultSettings;
if (settings.notifyTabClose) {
showTabCloseNotification(tabId);
}
});
});
// Set up alarms for periodic actions
chrome.alarms.create('groupTabs', { periodInMinutes: 5 });
chrome.alarms.create('sleepTabs', { periodInMinutes: 5 });
chrome.alarms.create('closeUnused', { periodInMinutes: 25 });
chrome.alarms.create('closeDuplicates', { periodInMinutes: 25 });
// Listen for alarms
chrome.alarms.onAlarm.addListener((alarm) => {
switch (alarm.name) {
case 'groupTabs':
groupSimilarTabs();
break;
case 'sleepTabs':
sleepInactiveTabs();
break;
case 'closeUnused':
closeUnusedTabs();
break;
case 'closeDuplicates':
closeDuplicateTabs();
break;
}
});