-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
115 lines (99 loc) · 3.44 KB
/
Copy pathbackground.js
File metadata and controls
115 lines (99 loc) · 3.44 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
// ================= ENVIRONMENT SAFETY CHECK =================
if (typeof chrome === 'undefined') {
console.error('This file must run inside a Chrome Extension service worker.');
} else {
// ================= BACKGROUND SERVICE WORKER =================
// Initialize extension installation
chrome.runtime.onInstalled.addListener((details) => {
console.log('Extension installed:', details.reason);
if (details.reason === 'install') {
const defaultSettings = {
autoSave: true,
notifications: false,
analytics: true,
quickNote: '',
pageViews: 0,
timeSaved: 0,
autoStart: false,
showBadge: true,
themeColor: '#667eea',
fontSize: 'medium',
dataCollection: true,
clearOnExit: false,
storageLimit: 100
};
chrome.storage.sync.set(defaultSettings, () => {
console.log('Default settings initialized');
});
}
});
// Listen for messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Background received message:', message);
switch (message.type) {
case 'UPDATE_STATS':
handleStatsUpdate(message.data);
break;
case 'CAPTURE_PAGE':
handlePageCapture(message.data);
break;
case 'GET_DATA':
handleGetData(message.key, sendResponse);
return true;
}
});
// Handle statistics updates
function handleStatsUpdate(data) {
chrome.storage.sync.get(['pageViews', 'timeSaved'], (result) => {
chrome.storage.sync.set({
pageViews: (result.pageViews || 0) + (data.pageViews || 1),
timeSaved: (result.timeSaved || 0) + (data.timeSaved || 0)
});
});
}
// Handle page capture
function handlePageCapture(data) {
chrome.storage.local.get(['capturedPages'], (result) => {
const pages = result.capturedPages || [];
pages.unshift({
...data,
timestamp: new Date().toISOString()
});
if (pages.length > 50) pages.pop();
chrome.storage.local.set({ capturedPages: pages });
});
}
// Handle data retrieval
function handleGetData(key, sendResponse) {
chrome.storage.sync.get(key, (result) => {
sendResponse(result[key]);
});
}
// Track page views
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
chrome.storage.sync.get(['analytics', 'pageViews', 'showBadge'], (result) => {
if (result.analytics) {
const views = (result.pageViews || 0) + 1;
chrome.storage.sync.set({ pageViews: views });
if (result.showBadge) {
chrome.action.setBadgeText({ text: views.toString() });
chrome.action.setBadgeBackgroundColor({ color: '#667eea' });
}
}
});
}
});
// On browser startup
chrome.runtime.onStartup.addListener(() => {
chrome.storage.sync.get(['clearOnExit'], (result) => {
if (result.clearOnExit) {
chrome.storage.sync.clear();
}
});
});
// Suspend handler
chrome.runtime.onSuspend.addListener(() => {
console.log('Service worker suspended');
});
} // ===== END SAFETY CHECK =====