-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
216 lines (174 loc) · 6.99 KB
/
Copy pathbackground.js
File metadata and controls
216 lines (174 loc) · 6.99 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
// Applying Assistant - Background Service Worker
// Extension installation and setup
chrome.runtime.onInstalled.addListener(async (details) => {
console.log('Applying Assistant installed');
if (details.reason === 'install') {
// First time installation
await initializeExtension();
// Welcome message (removed tab creation to minimize permissions)
console.log('Welcome! Job Application Assistant is ready to help speed up your job applications.');
} else if (details.reason === 'update') {
// Extension updated
console.log('Job Application Assistant updated to version', chrome.runtime.getManifest().version);
// Migrate settings if needed
await migrateSettings();
}
});
// Initialize extension on first install
async function initializeExtension() {
try {
// Set default settings
const defaultSettings = {
autoDetect: false,
showSuggestions: true,
keyboardShortcuts: true,
enabled: true
};
if (chrome.storage && chrome.storage.local) {
await chrome.storage.local.set({
settings: defaultSettings,
installDate: new Date().toISOString(),
version: chrome.runtime.getManifest().version
});
}
console.log('Extension initialized with default settings');
} catch (error) {
console.error('Error initializing extension:', error);
}
}
// Migrate settings between versions
async function migrateSettings() {
try {
if (!chrome.storage || !chrome.storage.local) return;
const result = await chrome.storage.local.get(['settings', 'version']);
const currentVersion = chrome.runtime.getManifest().version;
const savedVersion = result.version;
if (!savedVersion || savedVersion !== currentVersion) {
// Update version
await chrome.storage.local.set({ version: currentVersion });
// Merge any new default settings
const defaultSettings = {
autoDetect: false,
showSuggestions: true,
keyboardShortcuts: true,
enabled: true
};
const currentSettings = result.settings || {};
const updatedSettings = { ...defaultSettings, ...currentSettings };
await chrome.storage.local.set({ settings: updatedSettings });
console.log('Settings migrated to version', currentVersion);
}
} catch (error) {
console.error('Error migrating settings:', error);
}
}
// Handle messages from content scripts and popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.action) {
case 'getExtensionInfo':
sendResponse({
version: chrome.runtime.getManifest().version,
name: chrome.runtime.getManifest().name
});
break;
case 'openOptionsPage':
if (chrome.runtime && chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
}
sendResponse({ success: true });
break;
case 'reportError':
console.error('Error reported from', sender.tab?.url || 'popup:', request.error);
// Could send to analytics or error reporting service
sendResponse({ success: true });
break;
case 'trackUsage':
// Track template usage for analytics (privacy-friendly)
trackTemplateUsage(request.templateKey);
sendResponse({ success: true });
break;
default:
sendResponse({ error: 'Unknown action' });
}
});
// Track template usage (privacy-friendly analytics)
async function trackTemplateUsage(templateKey) {
try {
if (!chrome.storage || !chrome.storage.local) return;
const result = await chrome.storage.local.get(['usage']);
const usage = result.usage || {};
// Track usage by day (not specific template for privacy)
const today = new Date().toISOString().split('T')[0];
if (!usage[today]) {
usage[today] = 0;
}
usage[today]++;
// Keep only last 30 days of data
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const cutoffDate = thirtyDaysAgo.toISOString().split('T')[0];
Object.keys(usage).forEach(date => {
if (date < cutoffDate) {
delete usage[date];
}
});
await chrome.storage.local.set({ usage });
} catch (error) {
console.error('Error tracking usage:', error);
}
}
// Note: Manual injection removed - extension uses popup interface
// Content scripts auto-inject via manifest declaration
// Note: Auto-injection removed to minimize permissions
// Content scripts are declared in manifest and will auto-inject
// Note: Periodic cleanup removed to minimize permissions
// Data will be cleaned up manually during template usage
// Clean up old data
async function cleanupOldData() {
try {
if (!chrome.storage || !chrome.storage.local) return;
const result = await chrome.storage.local.get(['usage']);
const usage = result.usage || {};
// Remove usage data older than 30 days
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const cutoffDate = thirtyDaysAgo.toISOString().split('T')[0];
let cleaned = false;
Object.keys(usage).forEach(date => {
if (date < cutoffDate) {
delete usage[date];
cleaned = true;
}
});
if (cleaned) {
await chrome.storage.local.set({ usage });
console.log('Cleaned up old usage data');
}
} catch (error) {
console.error('Error during cleanup:', error);
}
}
// Handle extension updates
chrome.runtime.onUpdateAvailable.addListener((details) => {
console.log('Extension update available:', details.version);
// Could show notification to user about update
});
// Handle extension suspend/resume
chrome.runtime.onSuspend.addListener(() => {
console.log('Extension suspending...');
// Clean up any resources if needed
});
chrome.runtime.onSuspendCanceled.addListener(() => {
console.log('Extension suspend canceled');
});
// Note: Context menu functionality removed to minimize permissions
// Users can access templates through the extension popup
// Export functions for testing (if needed)
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
initializeExtension,
migrateSettings,
trackTemplateUsage,
cleanupOldData
};
}