Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
96 lines (84 loc) · 2.87 KB
/
Copy pathbackground.js
File metadata and controls
96 lines (84 loc) · 2.87 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
// background.js - Professional window management
let dashboardWindowId = null;
// Listen for extension icon clicks
chrome.action.onClicked.addListener(async (tab) => {
await openDashboard(tab);
});
// Listen for messages from popup and content scripts
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.action === 'openDashboard') {
await openDashboard();
sendResponse({ success: true });
} else if (request.action === 'dataExtracted') {
console.log('Background: Received extracted data', request.data);
// Store the data
await chrome.storage.local.set({
cursorData: request.data,
lastUpdated: new Date().toISOString()
});
// Update dashboard if open
if (dashboardWindowId) {
try {
await chrome.tabs.sendMessage(dashboardWindowId, {
action: 'updateDashboard',
data: request.data
});
} catch (error) {
console.log('Dashboard window not available for update');
}
}
// Show success badge
if (request.data.extracted) {
chrome.action.setBadgeText({
text: '✓',
tabId: sender.tab?.id
});
chrome.action.setBadgeBackgroundColor({
color: '#4ade80'
});
}
}
});
async function openDashboard(currentTab = null) {
try {
// Check if dashboard window is already open
if (dashboardWindowId) {
try {
const window = await chrome.windows.get(dashboardWindowId);
// Focus existing window
await chrome.windows.update(dashboardWindowId, { focused: true });
return;
} catch (error) {
// Window was closed, reset the ID
dashboardWindowId = null;
}
}
// Create new dashboard window
const window = await chrome.windows.create({
url: chrome.runtime.getURL('dashboard.html'),
type: 'popup',
width: 900,
height: 700,
focused: true
});
dashboardWindowId = window.id;
// Listen for window close
chrome.windows.onRemoved.addListener((windowId) => {
if (windowId === dashboardWindowId) {
dashboardWindowId = null;
}
});
} catch (error) {
console.error('Error opening dashboard:', error);
}
}
// Clear badge when tab changes
chrome.tabs.onActivated.addListener(() => {
chrome.action.setBadgeText({ text: '' });
});
// Handle extension installation
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
console.log('Cursor Usage Tracker installed');
}
});