-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
609 lines (524 loc) · 18.6 KB
/
background.js
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// =============================================================================
// State Management
// =============================================================================
const State = {
tabTree: {},
tabHistory: {},
excludedDomains: [],
userTimeZone: 'UTC',
isTracking: false,
extensionInitialized: false,
trackingCheckInterval: null,
viewerPort: null,
// Update node in both tree and history
updateNode: function(node, updates) {
// Find node in tree
const treeNode = this.findNodeInTree(node.id);
if (treeNode) {
Object.assign(treeNode, updates);
}
// Find and update in history
const historyArr = this.tabHistory[node.tabId];
if (historyArr) {
const historyNode = historyArr.find(n => n.id === node.id);
if (historyNode) {
Object.assign(historyNode, updates);
}
}
// Save changes
this.saveState();
},
// Find node in tree structure
findNodeInTree: function(nodeId) {
const searchNode = (node) => {
if (node.id === nodeId) return node;
if (node.children) {
for (const child of node.children) {
const found = searchNode(child);
if (found) return found;
}
}
return null;
};
for (const rootId in this.tabTree) {
const found = searchNode(this.tabTree[rootId]);
if (found) return found;
}
return null;
},
// Save state to storage
saveState: function() {
chrome.storage.local.set({
tabTree: this.tabTree,
isTracking: this.isTracking
});
},
// Clear all state
clearState: function() {
this.tabTree = {};
this.tabHistory = {};
this.saveState();
},
viewerTabs: new Map(), // Change to Map to store more info
registerViewerTab(tabId, layout = 'vertical') {
this.viewerTabs.set(tabId, { layout });
},
unregisterViewerTab(tabId) {
this.viewerTabs.delete(tabId);
},
isViewerTab(tabId) {
return this.viewerTabs.has(tabId);
},
getViewerLayout(tabId) {
return this.viewerTabs.get(tabId)?.layout || 'vertical';
},
updateViewerLayout(tabId, layout) {
if (this.viewerTabs.has(tabId)) {
this.viewerTabs.set(tabId, { layout });
}
},
// Modify getTabTree to handle viewer connection
getTabTree() {
return this.tabTree || {};
},
// Modify getTabTree response handler
handleGetTabTree() {
// Ensure we always return a valid object even if empty
return {
tabTree: this.tabTree || {}
};
}
};
// Update message handling in background.js
chrome.runtime.onConnect.addListener(port => {
if (port.name === 'viewer') {
console.log('Viewer connected'); // For debugging
// Store the viewer port in State
State.viewerPort = port;
port.onMessage.addListener((msg) => {
if (msg.action === 'layoutChanged') {
State.updateViewerLayout(port.sender.tab.id, msg.layout);
}
});
port.onDisconnect.addListener(() => {
console.log('Viewer disconnected'); // For debugging
State.unregisterViewerTab(port.sender.tab.id);
});
}
});
// =============================================================================
// Tab Management
// =============================================================================
const TabManager = {
async createNode(tab, parentId = null) {
const timestamp = Date.now();
const node = {
id: `${tab.id}-${timestamp}`,
tabId: tab.id,
url: tab.url,
title: tab.title,
createdAt: timestamp,
createdAtHuman: getHumanReadableTime(timestamp),
closedAt: null,
closedAtHuman: null,
children: [],
topWords: null
};
// Analyze content if possible
try {
node.topWords = await analyzePageContent(tab.id);
} catch (error) {
console.error('Content analysis failed:', error);
}
return node;
},
async addTab(tab, parentId = null) {
if (!State.isTracking || isExcluded(tab.url)) return;
const newNode = await this.createNode(tab);
if (parentId) {
// Add as child
const parentNode = State.findNodeInTree(parentId);
if (parentNode) {
if (!parentNode.children) parentNode.children = [];
if (!this.isDuplicateNode(newNode, parentNode.children)) {
parentNode.children.push(newNode);
}
}
} else {
// Add as root
if (!this.isDuplicateNode(newNode, Object.values(State.tabTree))) {
State.tabTree[newNode.id] = newNode;
}
}
// Update history
if (!State.tabHistory[tab.id]) {
State.tabHistory[tab.id] = [];
}
if (!this.isDuplicateNode(newNode, State.tabHistory[tab.id])) {
State.tabHistory[tab.id].push(newNode);
}
State.saveState();
return newNode;
},
isDuplicateNode(newNode, nodes) {
return nodes.some(existing =>
existing.url === newNode.url &&
Math.abs(existing.createdAt - newNode.createdAt) < 1000
);
},
updateTabTitle(tab) {
const history = State.tabHistory[tab.id];
if (history?.length > 0) {
const currentNode = history[history.length - 1];
if (currentNode.title !== tab.title) {
State.updateNode(currentNode, { title: tab.title });
}
}
},
handleTabClose(tabId) {
const timestamp = Date.now();
const closeNode = (node) => {
if (node.tabId === tabId && !node.closedAt) {
State.updateNode(node, {
closedAt: timestamp,
closedAtHuman: getHumanReadableTime(timestamp)
});
return true;
}
if (node.children) {
for (const child of node.children) {
if (closeNode(child)) return true;
}
}
return false;
};
Object.values(State.tabTree).forEach(closeNode);
delete State.tabHistory[tabId];
}
};
// =============================================================================
// Event Handlers
// =============================================================================
const EventHandlers = {
async onTabCreated(tab) {
if (!State.isTracking || State.isViewerTab(tab.id)) return;
const openerKey = `opener_${tab.id}`;
await chrome.storage.local.set({ [openerKey]: tab.openerTabId });
// Add listener for when tab finishes loading
const updateListener = async (tabId, info, updatedTab) => {
if (tabId === tab.id && info.status === 'complete') {
const result = await chrome.storage.local.get(openerKey);
const openerTabId = result[openerKey];
if (openerTabId) {
const parentHistory = State.tabHistory[openerTabId];
if (parentHistory?.length > 0) {
await TabManager.addTab(updatedTab, parentHistory[parentHistory.length - 1].id);
} else {
await TabManager.addTab(updatedTab);
}
chrome.storage.local.remove(openerKey);
} else {
await TabManager.addTab(updatedTab);
}
chrome.tabs.onUpdated.removeListener(updateListener);
}
};
chrome.tabs.onUpdated.addListener(updateListener);
},
async onTabUpdated(tabId, changeInfo, tab) {
if (!State.isTracking || State.isViewerTab(tabId) ||
!changeInfo.status === 'complete' || isExcluded(tab.url)) return;
// Always update title if it has changed
if (changeInfo.title) {
TabManager.updateTabTitle(tab);
}
const history = State.tabHistory[tabId];
if (!history?.length) {
await TabManager.addTab(tab);
return;
}
const currentNode = history[history.length - 1];
if (currentNode.url !== tab.url) {
// Check if we're navigating back
const existingIndex = history.findIndex(node => node.url === tab.url);
if (existingIndex !== -1) {
// Mark nodes after this point as closed
const timestamp = Date.now();
for (let i = existingIndex + 1; i < history.length; i++) {
State.updateNode(history[i], {
closedAt: timestamp,
closedAtHuman: getHumanReadableTime(timestamp)
});
}
State.tabHistory[tabId] = history.slice(0, existingIndex + 1);
TabManager.updateTabTitle(tab);
} else {
// New navigation
await TabManager.addTab(tab, currentNode.id);
}
}
},
onTabRemoved(tabId) {
if (!State.isTracking) return;
if (State.isViewerTab(tabId)) {
State.unregisterViewerTab(tabId);
return;
}
TabManager.handleTabClose(tabId);
}
};
// =============================================================================
// Initialization and Setup
// =============================================================================
async function initializeExtension() {
try {
const result = await chrome.storage.local.get([
'config', 'tabTree', 'userTimeZone', 'isTracking'
]);
// Initialize state
State.excludedDomains = result.config?.excludedDomains || [];
State.tabTree = result.tabTree || {};
State.userTimeZone = result.userTimeZone || 'UTC';
State.isTracking = result.isTracking || false;
// Setup icon and tracking check
updateIcon(State.isTracking);
initTrackingCheck();
State.extensionInitialized = true;
console.log('Extension initialized:', {
isTracking: State.isTracking,
domainsCount: State.excludedDomains.length,
treeSize: Object.keys(State.tabTree).length
});
} catch (error) {
console.error('Initialization error:', error);
}
}
// Initialize event listeners
function initializeEventListeners() {
chrome.tabs.onCreated.addListener(EventHandlers.onTabCreated);
chrome.tabs.onUpdated.addListener(EventHandlers.onTabUpdated);
chrome.tabs.onRemoved.addListener(EventHandlers.onTabRemoved);
// Set up message handling
chrome.runtime.onMessage.addListener(handleMessages);
}
// Start initialization
initializeExtension().then(() => {
initializeEventListeners();
});
// =============================================================================
// Utility Functions
// =============================================================================
function isExcluded(url) {
if (!url) return true;
if (url.startsWith(chrome.runtime.getURL('viewer/'))) return true;
return State.excludedDomains.some(domain => url.includes(domain));
}
function getHumanReadableTime(timestamp) {
return new Date(timestamp).toLocaleString('en-US', {
timeZone: State.userTimeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
}).replace(/[/,:]/g, '-');
}
// =============================================================================
// Icon and UI Management
// =============================================================================
function updateIcon(tracking) {
const iconPath = tracking ? {
16: 'icons/active_16.png',
32: 'icons/active_32.png',
48: 'icons/active_48.png',
128: 'icons/active_128.png'
} : {
16: 'icons/inactive_16.png',
32: 'icons/inactive_32.png',
48: 'icons/inactive_48.png',
128: 'icons/inactive_128.png'
};
chrome.action.setIcon({ path: iconPath });
}
function initTrackingCheck() {
if (State.trackingCheckInterval) {
clearInterval(State.trackingCheckInterval);
}
State.trackingCheckInterval = setInterval(() => {
if (State.isTracking) {
chrome.storage.local.get(['isTracking'], (result) => {
if (result.isTracking !== State.isTracking) {
console.log('Tracking state mismatch detected, reinitializing...');
initializeExtension();
}
});
}
}, 60000); // Check every minute
}
// =============================================================================
// Message Handling
// =============================================================================
function handleMessages(request, sender, sendResponse) {
console.log('Message received:', request); // For debugging
switch (request.action) {
case 'getTabTree':
const response = {
tabTree: State.tabTree || {}
};
console.log('Sending tabTree response:', response); // For debugging
sendResponse(response);
return false; // Changed to false since we're sending synchronously
case 'registerViewer':
State.registerViewerTab(request.tabId);
sendResponse({ success: true });
return false; // Changed to false since we're sending synchronously
case 'unregisterViewer':
State.unregisterViewerTab(request.tabId);
sendResponse({ success: true });
return false; // Changed to false since we're sending synchronously
case 'toggleTracking':
State.isTracking = !State.isTracking;
updateIcon(State.isTracking);
chrome.storage.local.set({ isTracking: State.isTracking });
console.log('Tracking toggled:', State.isTracking); // Add logging
sendResponse({ isTracking: State.isTracking });
return false;
case 'getTrackingStatus':
sendResponse({ isTracking: State.isTracking });
return false; // Changed to false since we're sending synchronously
case 'getTabTree':
sendResponse({ tabTree: State.tabTree });
break;
case 'clearTabTree':
State.clearState();
sendResponse({ success: true });
return false; // Changed to false since we're sending synchronously
case 'updateConfig':
chrome.storage.local.set({ config: request.config })
.then(() => {
State.excludedDomains = request.config.excludedDomains || [];
sendResponse({ success: true });
})
.catch(error => {
console.error('Error updating config:', error);
sendResponse({ error: error.message });
});
return true; // Will respond asynchronously
case 'updateTimeZone':
chrome.storage.local.set({ userTimeZone: request.timeZone })
.then(() => {
State.userTimeZone = request.timeZone;
sendResponse({ success: true });
})
.catch(error => {
console.error('Error updating timezone:', error);
sendResponse({ error: error.message });
});
return true; // Will respond asynchronously
default:
sendResponse({ error: 'Unknown action' });
}
return true; // Will respond asynchronously
}
//
// Add functions for word frequency analysis
async function analyzePageContent(tabId) {
if (!State.isTracking) return null; // Change from isTracking to State.isTracking
// Inject content script to analyze the page
try {
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId: tabId },
func: getWordFrequency,
});
return result;
} catch (error) {
console.error('Error analyzing page content:', error);
return null;
}
}
// Function to be injected into the page
function getWordFrequency() {
// Comprehensive list of English stop words
const stopWords = new Set([
// Articles and basic prepositions
'a', 'an', 'the', 'and', 'or', 'but', 'in', 'on', 'at', 'by', 'for', 'with', 'about',
'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below',
'to', 'from', 'up', 'down', 'of', 'off',
// Pronouns and their variants
'i', 'me', 'my', 'mine', 'myself',
'you', 'your', 'yours', 'yourself', 'yourselves',
'he', 'him', 'his', 'himself',
'she', 'her', 'hers', 'herself',
'it', 'its', 'itself',
'we', 'us', 'our', 'ours', 'ourselves',
'they', 'them', 'their', 'theirs', 'themselves',
'this', 'that', 'these', 'those',
'who', 'whom', 'whose', 'which', 'what',
// Verbs and verb forms
'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing',
'would', 'should', 'could', 'might', 'must', 'can', 'will',
'shall', 'may', 'ought',
// Common contractions
"i'm", "i've", "i'll", "i'd",
"you're", "you've", "you'll", "you'd",
"he's", "he'll", "he'd",
"she's", "she'll", "she'd",
"it's", "it'll", "it'd",
"we're", "we've", "we'll", "we'd",
"they're", "they've", "they'll", "they'd",
"that's", "that'll", "that'd",
"who's", "who'll", "who'd",
"what's", "what're", "what'll", "what'd",
"where's", "where'll", "where'd",
"when's", "when'll", "when'd",
"why's", "why'll", "why'd",
"how's", "how'll", "how'd",
"ain't", "isn't", "aren't", "wasn't", "weren't",
"hasn't", "haven't", "hadn't",
"doesn't", "don't", "didn't",
"won't", "wouldn't", "shan't", "shouldn't",
"can't", "cannot", "couldn't",
"mustn't", "mightn't",
// Common adverbs and adjectives
'just', 'very', 'quite', 'rather', 'somewhat',
'more', 'most', 'much', 'many', 'some', 'few', 'all', 'any', 'enough',
'such', 'same', 'different', 'other', 'another', 'each', 'every', 'either',
'neither', 'several', 'both', 'else',
'here', 'there', 'where', 'when', 'why', 'how',
'again', 'ever', 'never', 'always', 'sometimes', 'often', 'usually',
'already', 'still', 'now', 'then', 'once', 'twice',
'only', 'even', 'also', 'too', 'instead', 'rather',
// Miscellaneous common words
'like', 'well', 'back', 'there', 'still', 'yet', 'else', 'further',
'since', 'while', 'whether', 'though', 'although', 'unless',
'however', 'moreover', 'therefore', 'hence', 'furthermore',
'otherwise', 'nevertheless', 'meanwhile', 'afterward', 'afterwards',
'yes', 'no', 'not', 'nor', 'none', 'nothing', 'nobody',
'anywhere', 'everywhere', 'somewhere', 'nowhere',
'among', 'beside', 'besides', 'beyond', 'within', 'without'
]);
// Get all text content from the page
const text = document.body.innerText;
// Split into words, convert to lowercase, and filter
const words = text.toLowerCase()
.replace(/[^a-z0-9\s]/g, '') // Remove punctuation and special characters
.split(/\s+/) // Split on whitespace
.filter(word =>
word.length > 2 && // Skip very short words
!stopWords.has(word) && // Skip stop words
!/^\d+$/.test(word) // Skip pure numbers
);
// Count word frequencies
const frequencyMap = {};
words.forEach(word => {
frequencyMap[word] = (frequencyMap[word] || 0) + 1;
});
// Get top 5 words by frequency
return Object.entries(frequencyMap)
.sort(([,a], [,b]) => b - a)
.slice(0, 5)
.map(([word, count]) => ({ word, count }));
}