-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
90 lines (83 loc) · 2.91 KB
/
background.js
File metadata and controls
90 lines (83 loc) · 2.91 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
// background.js
// Capture snapshot of all tabs (auto-capture)
function captureSnapshot() {
chrome.tabs.query({}, (tabs) => {
if (chrome.runtime.lastError) {
console.error('Error querying tabs for auto-capture:', chrome.runtime.lastError);
return;
}
const now = new Date();
const extensionBaseUrl = chrome.runtime.getURL('');
const filteredTabs = tabs.filter(tab => !tab.url.startsWith(extensionBaseUrl));
const snapshot = {
timestamp: now.getTime(),
// display format is handled in UI
formatted: '',
tabs: filteredTabs.map(tab => ({ title: tab.title || '', url: tab.url || '' }))
};
// Save to storage
chrome.storage.local.get({ snapshots: [] }, result => {
const snaps = result.snapshots;
snaps.push(snapshot);
// optional: sort by timestamp
snaps.sort((a, b) => b.timestamp - a.timestamp);
chrome.storage.local.set({ snapshots: snaps });
});
});
}
// Setup auto-capture alarm based on settings
function setupAlarm() {
chrome.storage.sync.get({ autoCaptureEnabled: false, autoCaptureInterval: 30 }, settings => {
chrome.alarms.clear('autoCapture', () => {
if (settings.autoCaptureEnabled) {
chrome.alarms.create('autoCapture', { periodInMinutes: settings.autoCaptureInterval });
}
});
});
}
// When the extension icon is clicked, open the history page only
chrome.action.onClicked.addListener(() => {
chrome.tabs.create({ url: chrome.runtime.getURL('history.html') });
});
// Listen for restore-tabs, export-tabs and reschedule-alarm messages from the history page
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Export CSV file via downloads API
if (message.action === 'export-tabs' && message.dataUrl && message.filename) {
chrome.downloads.download({
url: message.dataUrl,
filename: message.filename,
saveAs: true,
conflictAction: 'overwrite'
});
sendResponse({ success: true });
return;
}
if (message.action === 'restore-tabs' && Array.isArray(message.urls)) {
message.urls.forEach(url => {
chrome.tabs.create({ url });
});
sendResponse({ success: true });
}
if (message.action === 'reschedule-alarm') {
setupAlarm();
sendResponse({ success: true });
return;
}
});
// Alarm handler for auto-capture
chrome.alarms.onAlarm.addListener(alarm => {
if (alarm.name === 'autoCapture') {
captureSnapshot();
}
});
// React to settings changes to reschedule alarm
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'sync' && (changes.autoCaptureEnabled || changes.autoCaptureInterval)) {
setupAlarm();
}
});
// Initialize alarms on startup
chrome.runtime.onInstalled.addListener(setupAlarm);
chrome.runtime.onStartup.addListener(setupAlarm);
// Also schedule on service worker load
setupAlarm();