Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
258 lines (224 loc) · 9.35 KB
/
Copy pathbackground.js
File metadata and controls
258 lines (224 loc) · 9.35 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Tab Archaeologist — Background Service Worker v2
// Tags, notes, session event log, snooze alarms
const TAB_DATA_KEY = 'tabData';
const GRAVEYARD_KEY = 'graveyard';
const SNOOZED_KEY = 'snoozed';
const SESSION_LOG_KEY = 'sessionLog';
let activeTabId = null;
let activeWindowId = null;
let activeStart = null;
// ─── Storage helpers ──────────────────────────────────────────────────────────
async function getTabData() { const r = await chrome.storage.local.get(TAB_DATA_KEY); return r[TAB_DATA_KEY] || {}; }
async function getGraveyard() { const r = await chrome.storage.local.get(GRAVEYARD_KEY); return r[GRAVEYARD_KEY] || []; }
async function getSnoozed() { const r = await chrome.storage.local.get(SNOOZED_KEY); return r[SNOOZED_KEY] || []; }
async function getSessionLog() { const r = await chrome.storage.local.get(SESSION_LOG_KEY); return r[SESSION_LOG_KEY] || []; }
async function saveTabData(d) { await chrome.storage.local.set({ [TAB_DATA_KEY]: d }); }
async function saveGraveyard(d) { await chrome.storage.local.set({ [GRAVEYARD_KEY]: d }); }
async function saveSnoozed(d) { await chrome.storage.local.set({ [SNOOZED_KEY]: d }); }
async function saveSessionLog(d) { await chrome.storage.local.set({ [SESSION_LOG_KEY]: d }); }
// ─── Session event log ────────────────────────────────────────────────────────
async function logEvent(event) {
const log = await getSessionLog();
log.push(event);
const cutoff = Date.now() - 90 * 86400 * 1000;
let pruned = log.filter(e => e.ts >= cutoff);
if (pruned.length > 8000) pruned.splice(0, pruned.length - 8000);
await saveSessionLog(pruned);
}
// ─── Blank tab entry ──────────────────────────────────────────────────────────
function blankEntry(tab) {
return {
url: tab.url || '',
title: tab.title || 'Loading…',
favicon: tab.favIconUrl || '',
openTime: Date.now(),
lastActiveTime: null,
totalActiveMs: 0,
activeSessions: 0,
tags: [],
note: ''
};
}
// ─── Tab lifecycle ────────────────────────────────────────────────────────────
chrome.tabs.onCreated.addListener(async (tab) => {
const tabData = await getTabData();
if (!tabData[tab.id]) {
tabData[tab.id] = blankEntry(tab);
await saveTabData(tabData);
}
const skipUrl = !tab.url || tab.url.startsWith('chrome://') || tab.url === 'about:blank';
if (!skipUrl) {
await logEvent({ ts: Date.now(), event: 'open', url: tab.url || '', title: tab.title || '' });
}
});
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status !== 'complete') return;
const tabData = await getTabData();
if (tabData[tabId]) {
tabData[tabId].url = tab.url || tabData[tabId].url;
tabData[tabId].title = tab.title || tabData[tabId].title;
tabData[tabId].favicon = tab.favIconUrl || tabData[tabId].favicon;
} else {
tabData[tabId] = blankEntry(tab);
}
await saveTabData(tabData);
});
chrome.tabs.onActivated.addListener(async ({ tabId, windowId }) => {
const now = Date.now();
if (activeTabId !== null && activeStart !== null) {
const elapsed = now - activeStart;
const tabData = await getTabData();
if (tabData[activeTabId]) {
tabData[activeTabId].totalActiveMs = (tabData[activeTabId].totalActiveMs || 0) + elapsed;
tabData[activeTabId].lastActiveTime = now;
await saveTabData(tabData);
}
}
activeTabId = tabId;
activeWindowId = windowId;
activeStart = now;
const tabData = await getTabData();
if (tabData[activeTabId]) {
tabData[activeTabId].activeSessions = (tabData[activeTabId].activeSessions || 0) + 1;
await saveTabData(tabData);
}
});
chrome.tabs.onRemoved.addListener(async (tabId) => {
const now = Date.now();
const tabData = await getTabData();
const tab = tabData[tabId];
if (!tab) return;
if (tabId === activeTabId && activeStart !== null) {
tab.totalActiveMs = (tab.totalActiveMs || 0) + (now - activeStart);
activeTabId = null;
activeStart = null;
}
const skipUrl = !tab.url || tab.url.startsWith('chrome://') || tab.url === 'about:blank';
if (!skipUrl) {
const lifespan = now - (tab.openTime || now);
const wasZombie = lifespan > 3 * 86400 * 1000 && (tab.totalActiveMs || 0) < 30_000;
let dom = '';
try { dom = new URL(tab.url).hostname.replace(/^www\./, ''); } catch {}
const graveyard = await getGraveyard();
graveyard.unshift({
url: tab.url,
title: tab.title || tab.url,
favicon: tab.favicon || '',
openTime: tab.openTime,
closedTime: now,
totalActiveMs: tab.totalActiveMs || 0,
activeSessions: tab.activeSessions || 0,
tags: tab.tags || [],
note: tab.note || '',
wasZombie,
domain: dom
});
if (graveyard.length > 300) graveyard.splice(300);
await saveGraveyard(graveyard);
await logEvent({
ts: now,
event: 'close',
url: tab.url,
title: tab.title || '',
domain: dom,
lifespan,
totalActiveMs: tab.totalActiveMs || 0,
wasZombie
});
}
delete tabData[tabId];
await saveTabData(tabData);
});
// ─── Messages from popup ──────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.type === 'GET_ALL_TABS') {
(async () => {
const [tabData, graveyard, snoozed, sessionLog] = await Promise.all([
getTabData(), getGraveyard(), getSnoozed(), getSessionLog()
]);
sendResponse({ tabData, graveyard, snoozed, sessionLog });
})();
return true;
}
if (msg.type === 'SNOOZE_TAB') {
snoozeTab(msg.tabId, msg.hours).then(() => sendResponse({ ok: true }));
return true;
}
// Save tags + note to a live tab
if (msg.type === 'SAVE_TAB_META') {
(async () => {
const tabData = await getTabData();
if (tabData[msg.tabId]) {
tabData[msg.tabId].tags = msg.tags || [];
tabData[msg.tabId].note = msg.note || '';
await saveTabData(tabData);
sendResponse({ ok: true });
} else {
sendResponse({ ok: false });
}
})();
return true;
}
// Save tags + note to a graveyard entry
if (msg.type === 'SAVE_GRAVE_META') {
(async () => {
const graveyard = await getGraveyard();
if (graveyard[msg.idx] !== undefined) {
graveyard[msg.idx].tags = msg.tags || [];
graveyard[msg.idx].note = msg.note || '';
await saveGraveyard(graveyard);
sendResponse({ ok: true });
} else {
sendResponse({ ok: false });
}
})();
return true;
}
if (msg.type === 'CLEAR_GRAVEYARD') {
saveGraveyard([]).then(() => sendResponse({ ok: true }));
return true;
}
});
// ─── Snooze ───────────────────────────────────────────────────────────────────
async function snoozeTab(tabId, hours) {
const tabData = await getTabData();
const tab = tabData[tabId];
if (!tab) return;
const snoozeUntil = Date.now() + hours * 3600 * 1000;
const alarmName = `snooze_${tabId}_${Date.now()}`;
const snoozed = await getSnoozed();
snoozed.push({
url: tab.url, title: tab.title, favicon: tab.favicon,
openTime: tab.openTime, tags: tab.tags || [], note: tab.note || '',
snoozeUntil, alarmName
});
await saveSnoozed(snoozed);
chrome.alarms.create(alarmName, { when: snoozeUntil });
await chrome.tabs.remove(tabId);
}
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (!alarm.name.startsWith('snooze_')) return;
const snoozed = await getSnoozed();
const idx = snoozed.findIndex(s => s.alarmName === alarm.name);
if (idx === -1) return;
const item = snoozed.splice(idx, 1)[0];
await saveSnoozed(snoozed);
await chrome.tabs.create({ url: item.url, active: false });
chrome.notifications.create({
type: 'basic', iconUrl: 'icons/icon48.png',
title: '⛏ Tab Resurrected',
message: `"${item.title}" is back from its nap.`
});
});
// ─── Startup / install ────────────────────────────────────────────────────────
async function syncExistingTabs() {
const tabs = await chrome.tabs.query({});
const tabData = await getTabData();
let changed = false;
for (const tab of tabs) {
if (!tabData[tab.id]) { tabData[tab.id] = blankEntry(tab); changed = true; }
}
if (changed) await saveTabData(tabData);
}
chrome.runtime.onStartup.addListener(syncExistingTabs);
chrome.runtime.onInstalled.addListener(syncExistingTabs);