-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
163 lines (136 loc) · 4.5 KB
/
Copy pathbackground.js
File metadata and controls
163 lines (136 loc) · 4.5 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
/**
* Session Snap - Background Service Worker
* Handles session saving, restoring, and keyboard shortcuts
*/
// Listen for keyboard shortcuts
chrome.commands.onCommand.addListener(async (command) => {
if (command === 'quick-save') {
await quickSaveSession();
}
});
// Quick save with auto-generated name
async function quickSaveSession() {
const tabs = await chrome.tabs.query({ currentWindow: true });
const session = {
id: Date.now().toString(),
name: `Quick Save - ${new Date().toLocaleString()}`,
createdAt: Date.now(),
tabs: tabs.map(tab => ({
url: tab.url,
title: tab.title,
favIconUrl: tab.favIconUrl,
pinned: tab.pinned
}))
};
const { sessions = [] } = await chrome.storage.local.get('sessions');
sessions.unshift(session);
// Keep only last 50 sessions
if (sessions.length > 50) {
sessions.pop();
}
await chrome.storage.local.set({ sessions });
// Show notification badge
chrome.action.setBadgeText({ text: '✓' });
chrome.action.setBadgeBackgroundColor({ color: '#4ade80' });
setTimeout(() => {
chrome.action.setBadgeText({ text: '' });
}, 2000);
}
// Handle messages from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'getSessions') {
chrome.storage.local.get('sessions').then(({ sessions = [] }) => {
sendResponse(sessions);
});
return true;
}
if (message.action === 'saveSession') {
saveSession(message.name).then(() => sendResponse({ success: true }));
return true;
}
if (message.action === 'restoreSession') {
restoreSession(message.sessionId, message.newWindow).then(() => sendResponse({ success: true }));
return true;
}
if (message.action === 'deleteSession') {
deleteSession(message.sessionId).then(() => sendResponse({ success: true }));
return true;
}
if (message.action === 'renameSession') {
renameSession(message.sessionId, message.name).then(() => sendResponse({ success: true }));
return true;
}
if (message.action === 'exportSessions') {
chrome.storage.local.get('sessions').then(({ sessions = [] }) => {
sendResponse(sessions);
});
return true;
}
if (message.action === 'importSessions') {
importSessions(message.sessions).then(() => sendResponse({ success: true }));
return true;
}
});
async function saveSession(name) {
const tabs = await chrome.tabs.query({ currentWindow: true });
const session = {
id: Date.now().toString(),
name: name || `Session ${new Date().toLocaleDateString()}`,
createdAt: Date.now(),
tabs: tabs.map(tab => ({
url: tab.url,
title: tab.title,
favIconUrl: tab.favIconUrl,
pinned: tab.pinned
}))
};
const { sessions = [] } = await chrome.storage.local.get('sessions');
sessions.unshift(session);
if (sessions.length > 50) {
sessions.pop();
}
await chrome.storage.local.set({ sessions });
}
async function restoreSession(sessionId, newWindow = false) {
const { sessions = [] } = await chrome.storage.local.get('sessions');
const session = sessions.find(s => s.id === sessionId);
if (!session) return;
const urls = session.tabs.map(tab => tab.url).filter(url => url && !url.startsWith('chrome://'));
if (newWindow) {
await chrome.windows.create({ url: urls });
} else {
for (const tab of session.tabs) {
if (tab.url && !tab.url.startsWith('chrome://')) {
await chrome.tabs.create({
url: tab.url,
pinned: tab.pinned,
active: false
});
}
}
}
}
async function deleteSession(sessionId) {
const { sessions = [] } = await chrome.storage.local.get('sessions');
const filtered = sessions.filter(s => s.id !== sessionId);
await chrome.storage.local.set({ sessions: filtered });
}
async function renameSession(sessionId, name) {
const { sessions = [] } = await chrome.storage.local.get('sessions');
const session = sessions.find(s => s.id === sessionId);
if (session) {
session.name = name;
await chrome.storage.local.set({ sessions });
}
}
async function importSessions(importedSessions) {
const { sessions = [] } = await chrome.storage.local.get('sessions');
const merged = [...importedSessions, ...sessions];
// Dedupe by ID
const unique = merged.filter((s, i, arr) => arr.findIndex(x => x.id === s.id) === i);
// Keep only last 100 after import
if (unique.length > 100) {
unique.length = 100;
}
await chrome.storage.local.set({ sessions: unique });
}