-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
369 lines (325 loc) · 10.4 KB
/
storage.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
// Storage helper functions
async function saveProfile(profileData, connectionType) {
try {
// Validate connection type
const validTypes = ['fellow-students', 'alumni', 'industry', 'recruiters', 'other'];
if (!validTypes.includes(connectionType)) {
console.error('Invalid connection type');
return false;
}
// Get the profile URL consistently
const profileUrl = profileData.profileUrl || profileData.linkedinUrl || profileData.profileURL || profileData.url || window.location.href;
// Check if profile already exists
const existingProfiles = await getProfiles();
const isDuplicate = existingProfiles.some(profile =>
profile.profileURL === profileUrl
);
if (isDuplicate) {
console.log('Profile already exists');
return { success: false, reason: 'duplicate' };
}
// Save the new profile
await chrome.storage.local.set({
[`profile_${Date.now()}`]: {
...profileData,
savedAt: new Date().toISOString(),
profileURL: profileUrl,
connectionType: connectionType // Add connection type to stored data
}
});
return { success: true };
} catch (error) {
console.error('Error saving profile:', error);
return { success: false, reason: 'error' };
}
}
async function getSpecificProfile(savedAt) {
try {
const key = `profile_${new Date(savedAt).getTime()}`;
const result = await chrome.storage.local.get(key);
return result[key] || null;
} catch (error) {
console.error('Error getting specific profile:', error);
return null;
}
}
async function getProfiles() {
try {
const result = await chrome.storage.local.get(null);
return Object.entries(result)
.filter(([key]) => key.startsWith('profile_'))
.map(([_, value]) => value)
.sort((a, b) => new Date(b.savedAt) - new Date(a.savedAt));
} catch (error) {
console.error('Error getting profiles:', error);
return [];
}
}
async function updateProfile(savedAt, updatedData) {
try {
const key = `profile_${new Date(savedAt).getTime()}`;
const result = await chrome.storage.local.get(key);
const existingProfile = result[key];
if (!existingProfile) {
console.error('Profile not found');
return false;
}
// Merge existing profile with updated data, maintaining the original savedAt and profileURL
const updatedProfile = {
...existingProfile,
...updatedData,
savedAt: existingProfile.savedAt, // Keep original savedAt
profileURL: existingProfile.profileURL // Keep original profileURL
};
await chrome.storage.local.set({ [key]: updatedProfile });
return true;
} catch (error) {
console.error('Error updating profile:', error);
return false;
}
}
async function isProfileSaved(profileUrl) {
const profiles = await getProfiles();
const savedProfile = profiles.find(profile => profile.profileURL === profileUrl);
return savedProfile || null;
}
async function saveClientProfile(profileData) {
try {
// Ensure we have a consistent URL property
const profileUrl = profileData.profileUrl || profileData.linkedinUrl || profileData.profileURL || profileData.url || window.location.href;
await chrome.storage.local.set({
clientProfile: {
...profileData,
savedAt: new Date().toISOString(),
profileURL: profileUrl // Store URL consistently as profileURL
}
});
return true;
} catch (error) {
console.error('Error saving client profile:', error);
return false;
}
}
async function getClientProfile() {
try {
const result = await chrome.storage.local.get('clientProfile');
return result.clientProfile || null;
} catch (error) {
console.error('Error getting client profile:', error);
return null;
}
}
async function deleteProfile(savedAt) {
if (savedAt === await getClientProfile().savedAt) {
console.error("Cannot delete client profile");
return false;
}
try {
const key = `profile_${new Date(savedAt).getTime()}`;
await chrome.storage.local.remove(key);
return true;
} catch (error) {
console.error('Error deleting profile:', error);
return false;
}
}
async function printProfile(savedAt) {
try {
const key = `profile_${new Date(savedAt).getTime()}`;
const result = await chrome.storage.local.get(key);
const profile = result[key];
console.log(profile);
return true;
} catch (error) {
console.error('Error printing profile:', error);
return false;
}
}
async function saveGeneratedMessage(message, RecipientData, messageType) {
try {
// Get existing messages first
const result = await chrome.storage.local.get('generatedMessages');
const existingMessages = result.generatedMessages || [];
// Create new message object with timestamp
const newMessage = {
message: message,
RecipientName: RecipientData.name,
RecipientUrl: RecipientData.profileURL,
messageType: messageType,
timestamp: new Date().toISOString()
};
// Add new message to the beginning of the array
existingMessages.unshift(newMessage);
// Store the updated messages array
await chrome.storage.local.set({ generatedMessages: existingMessages });
return true;
} catch (error) {
console.error('Error saving generated message:', error);
return false;
}
}
async function getGeneratedMessage() {
try {
const result = await chrome.storage.local.get('generatedMessages');
return result.generatedMessages || [];
} catch (error) {
console.error('Error getting generated messages:', error);
return [];
}
}
async function deleteGeneratedMessage(RecipientName) {
try {
const existingMessages = await getGeneratedMessage();
const updatedMessages = existingMessages.filter(message => message.RecipientName !== RecipientName);
await chrome.storage.local.set({ generatedMessages: updatedMessages });
return true;
} catch (error) {
console.error('Error deleting generated message:', error);
return false;
}
}
async function encryptData(data) {
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(data);
// Generate a random key
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
// Generate a random IV
const iv = crypto.getRandomValues(new Uint8Array(12));
// Encrypt the data
const encryptedData = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: iv },
key,
dataBuffer
);
// Export the key
const exportedKey = await crypto.subtle.exportKey('raw', key);
return {
encrypted: Array.from(new Uint8Array(encryptedData)),
iv: Array.from(iv),
key: Array.from(new Uint8Array(exportedKey))
};
}
async function decryptData(encrypted, key, iv) {
const importedKey = await crypto.subtle.importKey(
'raw',
new Uint8Array(key),
'AES-GCM',
true,
['decrypt']
);
const decryptedData = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: new Uint8Array(iv) },
importedKey,
new Uint8Array(encrypted)
);
return new TextDecoder().decode(decryptedData);
}
async function saveAIApiKey(AItype, apiKey) {
const aiList = ["gemini", "chatgpt", "claude"];
if (!aiList.includes(AItype)) {
console.error("Invalid AI type");
return false;
}
try {
const encryptedData = await encryptData(apiKey);
await chrome.storage.local.set({
[AItype]: encryptedData
});
return true;
} catch (error) {
console.error('Error saving API key:', error);
return false;
}
}
async function getAIApiKey(AItype) {
const aiList = ["gemini", "chatgpt", "claude"];
if (!aiList.includes(AItype)) {
console.error("Invalid AI type");
return null;
}
try {
const result = await chrome.storage.local.get(AItype);
if (!result[AItype]) return null;
const decrypted = await decryptData(
result[AItype].encrypted,
result[AItype].key,
result[AItype].iv
);
return decrypted;
} catch (error) {
console.error('Error getting API key:', error);
return null;
}
}
// Template management functions
async function saveTemplate(template) {
try {
// Get existing templates
const result = await chrome.storage.local.get('messageTemplates');
const existingTemplates = result.messageTemplates || [];
// Create new template object with ID and timestamp
const newTemplate = {
...template,
id: Date.now().toString(),
createdAt: new Date().toISOString()
};
// Add new template to the array
existingTemplates.push(newTemplate);
// Store the updated templates array
await chrome.storage.local.set({ messageTemplates: existingTemplates });
return true;
} catch (error) {
console.error('Error saving template:', error);
return false;
}
}
async function getTemplates() {
try {
const result = await chrome.storage.local.get('messageTemplates');
return result.messageTemplates || [];
} catch (error) {
console.error('Error getting templates:', error);
return [];
}
}
async function deleteTemplate(templateId) {
try {
const result = await chrome.storage.local.get('messageTemplates');
const templates = result.messageTemplates || [];
const updatedTemplates = templates.filter(template => template.id !== templateId);
await chrome.storage.local.set({ messageTemplates: updatedTemplates });
return true;
} catch (error) {
console.error('Error deleting template:', error);
return false;
}
}
async function updateTemplate(templateId, updatedData) {
try {
const result = await chrome.storage.local.get('messageTemplates');
const templates = result.messageTemplates || [];
const updatedTemplates = templates.map(template =>
template.id === templateId ? { ...template, ...updatedData } : template
);
await chrome.storage.local.set({ messageTemplates: updatedTemplates });
return true;
} catch (error) {
console.error('Error updating template:', error);
return false;
}
}
// Add this function to get a specific template
async function getSpecificTemplate(templateId) {
try {
const result = await chrome.storage.local.get('messageTemplates');
const templates = result.messageTemplates || [];
return templates.find(template => template.id === templateId) || null;
} catch (error) {
console.error('Error getting specific template:', error);
return null;
}
}