-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
108 lines (89 loc) · 3.25 KB
/
background.js
File metadata and controls
108 lines (89 loc) · 3.25 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
107
108
// FFSleep Background Script
const api = (typeof browser !== 'undefined') ? browser : chrome;
const DEFAULT_SETTINGS = {
autoSleepEnabled: false,
autoSleepMinutes: 30,
whitelist: [],
totalMemorySavedMB: 0,
tabActivity: {} // { tabId: lastActiveTimeStamp }
};
// Initialize settings
api.runtime.onInstalled.addListener(async () => {
const data = await api.storage.local.get('settings');
if (!data.settings) {
await api.storage.local.set({ settings: DEFAULT_SETTINGS });
}
setupAlarm();
});
// Setup or Clear Alarm based on settings
async function setupAlarm() {
const { settings } = await api.storage.local.get('settings');
await api.alarms.clear('autoSleepCheck');
if (settings.autoSleepEnabled) {
// Check every minute
api.alarms.create('autoSleepCheck', { periodInMinutes: 1 });
}
}
// Listen for storage changes to update alarm
api.storage.onChanged.addListener((changes, area) => {
if (area === 'local' && changes.settings) {
setupAlarm();
}
});
// Track tab activity
api.tabs.onActivated.addListener((activeInfo) => {
updateTabActivity(activeInfo.tabId);
});
async function updateTabActivity(tabId) {
const { settings } = await api.storage.local.get('settings');
settings.tabActivity = settings.tabActivity || {};
settings.tabActivity[tabId] = Date.now();
await api.storage.local.set({ settings });
}
// Alarm Listener: Check for idle tabs
api.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === 'autoSleepCheck') {
await checkAndSleepTabs();
}
});
async function checkAndSleepTabs() {
const { settings } = await api.storage.local.get('settings');
if (!settings.autoSleepEnabled) return;
const tabs = await api.tabs.query({ active: false, discarded: false });
const now = Date.now();
const thresholdMs = settings.autoSleepMinutes * 60 * 1000;
const discardedIds = [];
for (const tab of tabs) {
// Skip hidden/system tabs
if (!tab.url || tab.url.startsWith('about:')) continue;
// Check Whitelist
const isWhitelisted = settings.whitelist.some(domain => tab.url.includes(domain));
if (isWhitelisted) continue;
const lastActive = settings.tabActivity[tab.id] || 0;
// If tab hasn't been active for threshold time
if (now - lastActive > thresholdMs) {
discardedIds.push(tab.id);
}
}
if (discardedIds.length > 0) {
await api.tabs.discard(discardedIds);
// Update stats: Assuming average 75MB per tab
settings.totalMemorySavedMB += (discardedIds.length * 75);
await api.storage.local.set({ settings });
// Optional: Notify user
// api.notifications.create({
// type: 'basic',
// iconUrl: 'icons/icon.svg',
// title: 'FFSleep',
// message: `${discardedIds.length} tabs were put to sleep automatically.`
// });
}
}
// Clean up activity log when tabs are closed
api.tabs.onRemoved.addListener(async (tabId) => {
const { settings } = await api.storage.local.get('settings');
if (settings.tabActivity && settings.tabActivity[tabId]) {
delete settings.tabActivity[tabId];
await api.storage.local.set({ settings });
}
});