-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
352 lines (293 loc) · 11.5 KB
/
background.js
File metadata and controls
352 lines (293 loc) · 11.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Initialize storage for persistent tab data
async function initializeStorage() {
const stored = await browser.storage.local.get('tabHistory');
if (!stored.tabHistory) {
await browser.storage.local.set({ tabHistory: {} });
}
}
// Enhanced tab tracking with persistent storage
const enhancedTabTracking = {
// Track when tab is first created
async onTabCreated(tab) {
const history = await browser.storage.local.get('tabHistory');
const tabHistory = history.tabHistory || {};
tabHistory[tab.id] = {
firstOpened: new Date().toISOString(),
lastAccessed: new Date().toISOString(),
totalActiveTime: 0,
accessCount: 1,
url: tab.url,
title: tab.title,
previousUrls: [],
openerTabId: tab.openerTabId || null
};
await browser.storage.local.set({ tabHistory });
},
// Track tab activation
async onTabActivated(activeInfo) {
const history = await browser.storage.local.get('tabHistory');
const tabHistory = history.tabHistory || {};
const tabData = tabHistory[activeInfo.tabId] || {};
tabData.lastAccessed = new Date().toISOString();
tabData.accessCount = (tabData.accessCount || 0) + 1;
tabData.lastActivatedTime = Date.now();
tabHistory[activeInfo.tabId] = tabData;
await browser.storage.local.set({ tabHistory });
},
// Track when tab loses focus (to calculate active time)
async onTabDeactivated(tabId) {
const history = await browser.storage.local.get('tabHistory');
const tabHistory = history.tabHistory || {};
const tabData = tabHistory[tabId];
if (tabData && tabData.lastActivatedTime) {
const activeTime = Date.now() - tabData.lastActivatedTime;
tabData.totalActiveTime = (tabData.totalActiveTime || 0) + activeTime;
delete tabData.lastActivatedTime;
tabHistory[tabId] = tabData;
await browser.storage.local.set({ tabHistory });
}
},
// Track URL changes within same tab
async onTabUpdated(tabId, changeInfo, tab) {
if (changeInfo.url && changeInfo.url.trim()) {
const history = await browser.storage.local.get('tabHistory');
const tabHistory = history.tabHistory || {};
const tabData = tabHistory[tabId] || {};
// Only store previous URL if it's valid and different from new URL
if (tabData.url && tabData.url.trim() && tabData.url !== changeInfo.url) {
tabData.previousUrls = tabData.previousUrls || [];
tabData.previousUrls.push({
url: tabData.url,
timestamp: new Date().toISOString()
});
// Keep only last 10 URLs to prevent excessive storage
if (tabData.previousUrls.length > 10) {
tabData.previousUrls = tabData.previousUrls.slice(-10);
}
}
tabData.url = changeInfo.url;
tabData.title = tab.title || tabData.title;
tabHistory[tabId] = tabData;
await browser.storage.local.set({ tabHistory });
}
},
// Clean up when tabs are closed
async onTabRemoved(tabId) {
const history = await browser.storage.local.get('tabHistory');
const tabHistory = history.tabHistory || {};
delete tabHistory[tabId];
await browser.storage.local.set({ tabHistory });
}
};
// Initialize storage on extension startup
initializeStorage();
// Track when tabs are created
browser.tabs.onCreated.addListener(enhancedTabTracking.onTabCreated);
// Clean up when tabs are closed
browser.tabs.onRemoved.addListener(enhancedTabTracking.onTabRemoved);
// Track last access time for tabs
browser.tabs.onActivated.addListener(enhancedTabTracking.onTabActivated);
// Track tab deactivation for active time calculation
browser.tabs.onActivated.addListener(async (activeInfo) => {
// When a tab is activated, deactivate the previous one
const tabs = await browser.tabs.query({ active: false, currentWindow: true });
tabs.forEach(tab => {
if (tab.id !== activeInfo.tabId) {
enhancedTabTracking.onTabDeactivated(tab.id);
}
});
});
// Track URL changes within tabs
browser.tabs.onUpdated.addListener(enhancedTabTracking.onTabUpdated);
// Listen for keyboard shortcut
browser.commands.onCommand.addListener((command) => {
if (command === "save-tabs") {
saveTabs();
}
});
// Listen for messages from popup
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "saveTabs") {
saveTabs();
}
});
async function saveTabs() {
try {
// Get user preference
const prefs = await browser.storage.local.get('saveMethod');
const saveMethod = prefs.saveMethod || 'editor';
// Get all tabs in current window
const tabs = await browser.tabs.query({ currentWindow: true });
// Generate markdown content
const markdown = await generateMarkdown(tabs);
// Create filename with timestamp
const now = new Date();
const filename = `tabs_${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}-${String(now.getMinutes()).padStart(2, '0')}.md`;
switch (saveMethod) {
case 'editor':
// Open editor with the markdown content
const editorUrl = browser.runtime.getURL('editor.html') +
'?content=' + encodeURIComponent(markdown) +
'&filename=' + encodeURIComponent(filename);
browser.tabs.create({
url: editorUrl,
active: true
});
break;
case 'direct':
// Direct download
const blob = new Blob([markdown], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
await browser.downloads.download({
url: url,
filename: filename,
saveAs: false
});
setTimeout(() => URL.revokeObjectURL(url), 1000);
break;
case 'clipboard':
// Copy to clipboard - need to do this in active tab
const code = `
navigator.clipboard.writeText(${JSON.stringify(markdown)}).then(() => {
alert('Tab list copied to clipboard!');
}).catch(() => {
// Fallback: copy to clipboard using older method
const textarea = document.createElement('textarea');
textarea.value = ${JSON.stringify(markdown)};
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
alert('Tab list copied to clipboard!');
});
`;
// Get active tab and inject script
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
browser.tabs.executeScript(activeTab.id, {
code: code
});
break;
}
} catch (error) {
console.error('Error saving tabs:', error);
}
}
async function generateMarkdown(tabs) {
const history = await browser.storage.local.get('tabHistory');
const tabHistory = history.tabHistory || {};
const lines = [];
const processed = new Set();
// Header
lines.push(`# Saved Tabs - ${new Date().toLocaleString()}`);
lines.push('');
lines.push(`**Total tabs:** ${tabs.length}`);
lines.push(`**Window:** Current window`);
lines.push('');
lines.push('---');
lines.push('');
// Build parent-child relationships
const childrenMap = new Map();
tabs.forEach(tab => {
const tabData = tabHistory[tab.id] || {};
const parentId = tabData.openerTabId;
if (parentId && tabs.some(t => t.id === parentId)) {
if (!childrenMap.has(parentId)) {
childrenMap.set(parentId, []);
}
childrenMap.get(parentId).push(tab.id);
}
});
// Process tabs in order
function processTab(tab, level = 0) {
if (processed.has(tab.id)) return;
processed.add(tab.id);
const indent = ' '.repeat(level);
const tabData = tabHistory[tab.id] || {};
// Tab number (position)
const tabNum = tab.index + 1;
// Status indicators
const status = [];
if (tab.active) status.push('ACTIVE');
if (tab.pinned) status.push('PINNED');
if (tab.audible) status.push('🔊');
if (tab.mutedInfo && tab.mutedInfo.muted) status.push('🔇');
// Main tab entry with position number
const statusStr = status.length > 0 ? ` [${status.join(', ')}]` : '';
lines.push(`${indent}${tabNum}. [${tab.title || 'Untitled'}](${tab.url})${statusStr}`);
// Add enhanced metadata
if (tabData.firstOpened) {
const firstOpened = new Date(tabData.firstOpened);
lines.push(`${indent} - First opened: ${firstOpened.toLocaleDateString()} ${firstOpened.toLocaleTimeString()}`);
}
if (tabData.accessCount) {
lines.push(`${indent} - Times accessed: ${tabData.accessCount}`);
}
if (tabData.totalActiveTime) {
const minutes = Math.floor(tabData.totalActiveTime / 60000);
const seconds = Math.floor((tabData.totalActiveTime % 60000) / 1000);
lines.push(`${indent} - Total active time: ${minutes}m ${seconds}s`);
}
if (tabData.previousUrls && tabData.previousUrls.length > 0) {
const recentUrls = tabData.previousUrls.slice(-3).filter(prev => prev.url && prev.url.trim());
if (recentUrls.length > 0) {
lines.push(`${indent} - Last ${recentUrls.length} previous URLs:`);
recentUrls.forEach(prev => {
const prevTime = new Date(prev.timestamp);
lines.push(`${indent} • ${prev.url} (${prevTime.toLocaleTimeString()})`);
});
}
}
// Add container info if using containers
if (tab.cookieStoreId && tab.cookieStoreId !== 'firefox-default') {
lines.push(`${indent} - Container: ${tab.cookieStoreId}`);
}
// Add automatic Notes section for annotation
lines.push(`${indent} - Notes: `);
// Process children immediately after parent
const children = childrenMap.get(tab.id) || [];
children.forEach(childId => {
const childTab = tabs.find(t => t.id === childId);
if (childTab) {
lines.push(`${indent} ↳ Child tab:`);
processTab(childTab, level + 1);
}
});
}
// Process all tabs in their original order
tabs.forEach(tab => {
processTab(tab);
if (!childrenMap.has(tab.id) || childrenMap.get(tab.id).length === 0) {
lines.push(''); // Add spacing between top-level tabs
}
});
// Add enhanced statistics at the end
lines.push('---');
lines.push('');
lines.push('## Statistics');
lines.push(`- Total tabs: ${tabs.length}`);
lines.push(`- Pinned tabs: ${tabs.filter(t => t.pinned).length}`);
lines.push(`- Tabs with audio: ${tabs.filter(t => t.audible).length}`);
lines.push(`- Active tab: Tab ${tabs.findIndex(t => t.active) + 1}`);
// Add time-based statistics
let totalActiveTime = 0;
let totalAccessCount = 0;
let tabsWithHistory = 0;
tabs.forEach(tab => {
const tabData = tabHistory[tab.id] || {};
if (tabData.totalActiveTime) totalActiveTime += tabData.totalActiveTime;
if (tabData.accessCount) totalAccessCount += tabData.accessCount;
if (tabData.previousUrls && tabData.previousUrls.length > 0) tabsWithHistory++;
});
if (totalActiveTime > 0) {
const minutes = Math.floor(totalActiveTime / 60000);
const seconds = Math.floor((totalActiveTime % 60000) / 1000);
lines.push(`- Total active time: ${minutes}m ${seconds}s`);
}
if (totalAccessCount > 0) {
lines.push(`- Total access count: ${totalAccessCount}`);
}
if (tabsWithHistory > 0) {
lines.push(`- Tabs with navigation history: ${tabsWithHistory}`);
}
return lines.join('\n');
}