-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbackground.js
More file actions
591 lines (529 loc) · 26 KB
/
background.js
File metadata and controls
591 lines (529 loc) · 26 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
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
// Importar las clases necesarias
import { Session } from './src/Session.js';
import { Bug } from './src/Annotation.js';
import { Note } from './src/Annotation.js';
import { Idea } from './src/Annotation.js';
import { Question } from './src/Annotation.js';
import { ExportSessionCSV } from './src/ExportSessionCSV.js';
import { JSonSessionService } from './src/JSonSessionService.js';
import { getSystemInfo } from './src/browserInfo.js';
let session = new Session();
// Función para guardar la sesión en el storage
async function saveSession() {
try {
await chrome.storage.local.set({ 'session': session });
} catch (error) {
if (error.name === 'QUOTA_BYTES' || (chrome.runtime.lastError && chrome.runtime.lastError.message && chrome.runtime.lastError.message.includes('QUOTA_BYTES'))) {
console.error('Error saving session due to quota limit:', error);
// Create a deep copy of the session object
const sessionCopy = JSON.parse(JSON.stringify(session));
// Find the oldest annotation with a non-null imageURL
let oldestAnnotationIndex = -1;
for (let i = 0; i < sessionCopy.annotations.length; i++) {
if (sessionCopy.annotations[i].imageURL) {
oldestAnnotationIndex = i;
break;
}
}
if (oldestAnnotationIndex !== -1) {
// const originalImageURL = sessionCopy.annotations[oldestAnnotationIndex].imageURL; // Optional: for logging
sessionCopy.annotations[oldestAnnotationIndex].imageURL = "IMAGE_REMOVED_DUE_TO_STORAGE_LIMIT";
try {
await chrome.storage.local.set({ 'session': sessionCopy });
// Notify user about successful save after removing screenshot
const notifId = 'sessionSavedAfterQuota-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: 'Session Saved with Adjustment',
message: 'The oldest screenshot was removed to save the session due to storage limits.'
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 7000);
// Update the main session object to reflect the change if the save was successful.
session.annotations[oldestAnnotationIndex].imageURL = "IMAGE_REMOVED_DUE_TO_STORAGE_LIMIT";
} catch (secondError) {
console.error('Error saving session even after removing screenshot:', secondError);
// Notify user about failed save even after removing screenshot
const notifId = 'sessionSaveFailedAfterQuota-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: 'Session Save Failed',
message: 'Failed to save session. Insufficient storage even after removing the oldest screenshot.'
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 7000);
}
} else {
// No annotation with imageURL found
console.error('Failed to save session. No screenshots to remove for quota.');
const notifId = 'sessionSaveFailedNoScreenshot-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: 'Session Save Failed',
message: 'Failed to save session. Insufficient storage and no screenshots to remove.'
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 7000);
}
} else {
// Not a quota error, re-throw or handle as appropriate
console.error('Error saving session:', error);
throw error;
}
}
}
// Función para cargar la sesión desde el storage
async function loadSession() {
const data = await chrome.storage.local.get('session');
if (data.session) {
// Reconstruir el objeto Session con sus métodos
const loadedSession = data.session;
session = new Session(loadedSession.startDateTime, loadedSession.browserInfo);
// Reconstruir las anotaciones
loadedSession.annotations.forEach(annotation => {
let newAnnotation;
switch (annotation.type) {
case "Bug":
newAnnotation = new Bug(annotation.name, annotation.url, annotation.timestamp, annotation.imageURL);
session.addBug(newAnnotation);
break;
case "Note":
newAnnotation = new Note(annotation.name, annotation.url, annotation.timestamp, annotation.imageURL);
session.addNote(newAnnotation);
break;
case "Idea":
newAnnotation = new Idea(annotation.name, annotation.url, annotation.timestamp, annotation.imageURL);
session.addIdea(newAnnotation);
break;
case "Question":
newAnnotation = new Question(annotation.name, annotation.url, annotation.timestamp, annotation.imageURL);
session.addQuestion(newAnnotation);
break;
}
});
}
}
// Cargar la sesión al iniciar
loadSession();
// Helper function for notifications of processing errors (before addAnnotation is called)
function notifyProcessingError(annotationType, descriptionName, errorMessage = "") {
const typeStr = annotationType || "Annotation";
const nameStr = descriptionName || "";
const notifId = 'annotationProcessingError-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: `${typeStr} Processing Failed`,
message: `Could not process ${typeStr.toLowerCase()} "${nameStr}" for screenshot. Error: ${errorMessage}`
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 7000);
}
// Escuchar mensajes del popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// Keep 'return true' if any path in this listener might call sendResponse asynchronously.
// For "csToBgCropData", we are not sending a response back to content script currently.
// For other existing cases, they do use sendResponse.
let isAsync = false;
switch (request.type) {
case "initiateCropSelection":
// Forward the crop selection request to the content script
chrome.tabs.query({ active: true, currentWindow: true }, async function(tabs) {
if (tabs && tabs[0] && tabs[0].id != null) {
const tab = tabs[0];
// Check if the page allows content scripts
if (tab.url.startsWith('chrome://') || tab.url.startsWith('edge://') ||
tab.url.startsWith('chrome-extension://') || tab.url.startsWith('about:')) {
const notifId = 'cropSelectionError-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: 'Selection Not Available',
message: 'Screen selection cannot be used on this type of page. Please try on a regular webpage.'
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 5000);
return;
}
try {
// Try to inject content script if it's not already loaded
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['js/content_script.js']
}).catch(() => {
// Content script might already be loaded, ignore error
console.log("Background: Content script might already be loaded");
});
// Small delay to ensure content script is ready
setTimeout(() => {
chrome.tabs.sendMessage(tab.id, {
type: "startSelection",
annotationType: request.annotationType,
description: request.description
}, function(response) {
if (chrome.runtime.lastError) {
console.error("Background: Error sending startSelection to content script:", chrome.runtime.lastError.message);
const notifId = 'cropSelectionError-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: 'Selection Failed',
message: 'Could not start screen selection. Please try again.'
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 5000);
} else {
console.log("Background: Successfully initiated crop selection on content script");
}
});
}, 100);
} catch (error) {
console.error("Background: Error injecting content script:", error);
const notifId = 'cropSelectionError-' + Date.now();
chrome.notifications.create(notifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: 'Selection Failed',
message: 'Could not start screen selection. Error: ' + error.message
});
setTimeout(() => { chrome.notifications.clear(notifId); }, 5000);
}
} else {
console.error("Background: No active tab found for crop selection");
}
});
break;
case "addBug":
console.log("Background: Received message", request.type, ". Name:", request.name, ". imageURL (first 100 chars):", request.imageURL ? request.imageURL.substring(0, 100) : "null");
addAnnotation("Bug", request.name, request.imageURL)
.then(() => sendResponse({ status: "ok" }))
.catch(error => sendResponse({ status: "error", error: error.message }));
isAsync = true;
break;
case "addIdea":
console.log("Background: Received message", request.type, ". Name:", request.name, ". imageURL (first 100 chars):", request.imageURL ? request.imageURL.substring(0, 100) : "null");
addAnnotation("Idea", request.name, request.imageURL)
.then(() => sendResponse({ status: "ok" }))
.catch(error => sendResponse({ status: "error", error: error.message }));
isAsync = true;
break;
case "addNote":
console.log("Background: Received message", request.type, ". Name:", request.name, ". imageURL (first 100 chars):", request.imageURL ? request.imageURL.substring(0, 100) : "null");
addAnnotation("Note", request.name, request.imageURL)
.then(() => sendResponse({ status: "ok" }))
.catch(error => sendResponse({ status: "error", error: error.message }));
isAsync = true;
break;
case "addQuestion":
console.log("Background: Received message", request.type, ". Name:", request.name, ". imageURL (first 100 chars):", request.imageURL ? request.imageURL.substring(0, 100) : "null");
addAnnotation("Question", request.name, request.imageURL)
.then(() => sendResponse({ status: "ok" }))
.catch(error => sendResponse({ status: "error", error: error.message }));
isAsync = true;
break;
case "requestCropScreenshot":
console.log("Background: Received requestCropScreenshot", request);
handleCropScreenshotRequest(request)
.then((croppedImageData) => {
sendResponse({ croppedImageData: croppedImageData });
})
.catch((error) => {
console.error("Background: Failed to capture crop screenshot:", error);
sendResponse({ croppedImageData: null });
});
isAsync = true;
break;
case "csToBgCropData":
console.log("Background: Received csToBgCropData", request);
handleProcessAnnotatedCrop(request)
.then(() => {
console.log("Background: Annotated crop processed successfully");
})
.catch((error) => {
console.error("Background: Failed to process annotated crop:", error);
});
break;
case "updateAnnotationName":
var AnnotationID = request.annotationID;
var newName = request.newName;
var annotations = session.getAnnotations();
var annotation = annotations[AnnotationID];
annotation.setName(newName);
saveSession().then(() => sendResponse({ status: "ok" }));
break;
case "deleteAnnotation":
session.deleteAnnotation(request.annotationID);
saveSession().then(() => sendResponse({ status: "ok" }));
break;
case "exportSessionCSV":
if (!exportSessionCSV()) {
sendResponse({ status: "nothing to export" });
} else {
sendResponse({ status: "ok" });
}
break;
case "exportSessionJSon":
if (!exportSessionJSon()) {
sendResponse({ status: "nothing to export" });
} else {
sendResponse({ status: "ok" });
}
break;
case "importSessionJSon":
var fileData = request.jSonSession;
if (!importSessionJSon(fileData)) {
sendResponse({ status: "nothing to import" });
} else {
sendResponse({ status: "ok" });
}
break;
case "clearSession":
clearSession().then(() => sendResponse({ status: "ok" }));
break;
case "getSessionData":
sendResponse({
bugs: session.getBugs().length,
notes: session.getNotes().length,
ideas: session.getIdeas().length,
questions: session.getQuestions().length,
annotationsCount: session.getAnnotations().length
});
break;
case "getFullSession":
if (!session) {
sendResponse(null);
return true;
}
sendResponse({
startDateTime: session.StartDateTime,
browserInfo: {
browser: session.BrowserInfo.browser || "Chrome",
browserVersion: session.BrowserInfo.browserVersion || chrome.runtime.getManifest().version,
os: session.BrowserInfo.os || navigator.platform,
osVersion: session.BrowserInfo.osVersion || navigator.userAgent,
cookies: session.BrowserInfo.cookies || navigator.cookieEnabled,
flashVersion: session.BrowserInfo.flashVersion || "N/A"
},
annotations: session.annotations.map(annotation => ({
type: annotation.constructor.name,
name: annotation.name,
url: annotation.url,
timestamp: annotation.timestamp,
imageURL: annotation.imageURL
}))
});
break;
}
return isAsync; // Return true only if sendResponse is used asynchronously in any of the handled cases.
});
// Handle crop screenshot request - capture and crop, then return to content script
async function handleCropScreenshotRequest(request) {
try {
console.log("Background: Processing crop screenshot request", request);
// Capture the visible tab
const dataUrl = await chrome.tabs.captureVisibleTab(null, { format: "png" });
if (!dataUrl) {
throw new Error("Failed to capture screenshot");
}
// Convert dataUrl to Blob
const response = await fetch(dataUrl);
const blob = await response.blob();
// Create bitmap from image
const bitmap = await createImageBitmap(blob);
// Create offscreen canvas with crop dimensions
const canvas = new OffscreenCanvas(
request.coordinates.width,
request.coordinates.height
);
const ctx = canvas.getContext('2d');
// Draw the selected portion
ctx.drawImage(
bitmap,
request.coordinates.x,
request.coordinates.y,
request.coordinates.width,
request.coordinates.height,
0,
0,
request.coordinates.width,
request.coordinates.height
);
// Convert canvas to blob
const croppedBlob = await canvas.convertToBlob({ type: 'image/png' });
// Convert blob to dataUrl
const croppedDataUrl = await new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(croppedBlob);
});
console.log("Background: Successfully created cropped screenshot");
return croppedDataUrl;
} catch (error) {
console.error("Background: Error in handleCropScreenshotRequest:", error);
throw error;
}
}
// Handle annotated crop data - save the final annotated image
async function handleProcessAnnotatedCrop(request) {
try {
console.log("Background: Processing annotated crop", request);
// Create annotation with the annotated image
await addAnnotation(
request.annotationType.charAt(0).toUpperCase() + request.annotationType.slice(1),
request.description,
request.annotatedImageData
);
console.log("Background: Successfully processed annotated crop");
} catch (error) {
console.error("Background: Error in handleProcessAnnotatedCrop:", error);
throw error;
}
}
async function addAnnotation(type, name, imageURL) {
console.log("Background: addAnnotation called. Type:", type, ". Name:", name, ". Image URL (first 100 chars):", imageURL ? imageURL.substring(0, 100) : "No image");
if (session.getAnnotations().length == 0) {
await startSession();
}
return new Promise((resolve, reject) => {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
try {
const currentUrl = tabs[0] ? tabs[0].url : "N/A"; // Handle missing tab
const now = new Date().getTime();
let newAnnotation;
let annotationSimpleType = ""; // For user-friendly notification
switch (type) {
case "Bug":
newAnnotation = new Bug(name, currentUrl, now, imageURL);
session.addBug(newAnnotation);
annotationSimpleType = "Bug";
break;
case "Note":
newAnnotation = new Note(name, currentUrl, now, imageURL);
session.addNote(newAnnotation);
annotationSimpleType = "Note";
break;
case "Idea":
newAnnotation = new Idea(name, currentUrl, now, imageURL);
session.addIdea(newAnnotation);
annotationSimpleType = "Idea";
break;
case "Question":
newAnnotation = new Question(name, currentUrl, now, imageURL);
session.addQuestion(newAnnotation);
annotationSimpleType = "Question";
break;
default: // Should not happen
return reject(new Error("Unknown annotation type"));
}
console.log("Background: Attempting to save session for annotation Type:", type, "Name:", name);
saveSession().then(() => {
// --- Create Notification ---
const notifId = 'annotationSaved-' + Date.now();
const notifOptions = {
type: 'basic',
iconUrl: 'icons/iconbig.png', // Ensure this icon path is correct
title: `${annotationSimpleType} Saved!`,
message: `Your ${annotationSimpleType.toLowerCase()} "${name}" has been successfully saved.`
};
chrome.notifications.create(notifId, notifOptions);
// Optional: Clear notification after a few seconds
setTimeout(() => {
chrome.notifications.clear(notifId);
}, 5000); // Clear after 5 seconds
// --- End Notification ---
resolve(); // Resolve the main promise
}).catch(error => {
console.error("Background: Error during saveSession for", type, name, ":", error);
// Optionally, show an error notification here too
const errorNotifId = 'annotationError-' + Date.now();
chrome.notifications.create(errorNotifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: `${annotationSimpleType || type} Save Failed`,
message: `Could not save ${annotationSimpleType.toLowerCase() || type.toLowerCase()} "${name}". Error: ${error.message}`
});
setTimeout(() => {
chrome.notifications.clear(errorNotifId);
}, 7000); // Keep error notifications slightly longer
reject(error);
});
} catch (error) { // Catch synchronous errors in the promise executor
console.error("Background: Error in addAnnotation sync part:", error);
// Send a notification for this synchronous error as well
const syncErrorNotifId = 'annotationSyncError-' + Date.now();
chrome.notifications.create(syncErrorNotifId, {
type: 'basic',
iconUrl: 'icons/iconbig.png',
title: `${type || 'Annotation'} Setup Failed`,
message: `Failed to initiate saving for "${name}". Error: ${error.message}`
});
setTimeout(() => {
chrome.notifications.clear(syncErrorNotifId);
}, 7000);
reject(error);
}
});
});
}
async function startSession() {
var systemInfo = getSystemInfo();
session = new Session(Date.now(), systemInfo);
await saveSession();
}
async function clearSession() {
session.clearAnnotations();
await saveSession();
}
function exportSessionCSV() {
if (session.getAnnotations().length == 0) return false;
var exportService = new ExportSessionCSV(session);
var csvData = exportService.getCSVData();
var browserInfo = session.getBrowserInfo();
var browserInfoString = browserInfo.browser + "_" + browserInfo.browserVersion;
// Formatear la fecha correctamente
const date = new Date(session.getStartDateTime());
const startDateTime = date.getFullYear() +
('0' + (date.getMonth() + 1)).slice(-2) +
('0' + date.getDate()).slice(-2) + '_' +
('0' + date.getHours()).slice(-2) +
('0' + date.getMinutes()).slice(-2);
var fileName = "ExploratorySession_" + browserInfoString + "_" + startDateTime + ".csv";
// Crear data URL
const dataUrl = 'data:text/csv;charset=utf-8;base64,' + btoa(csvData);
chrome.downloads.download({
url: dataUrl,
filename: fileName,
saveAs: true
});
return true;
}
function exportSessionJSon() {
if (session.getAnnotations().length == 0) return false;
var exportJSonService = new JSonSessionService();
var jsonData = exportJSonService.getJSon(session);
var browserInfo = session.getBrowserInfo();
var browserInfoString = browserInfo.browser + "_" + browserInfo.browserVersion;
// Formatear la fecha correctamente
const date = new Date(session.getStartDateTime());
const startDateTime = date.getFullYear() +
('0' + (date.getMonth() + 1)).slice(-2) +
('0' + date.getDate()).slice(-2) + '_' +
('0' + date.getHours()).slice(-2) +
('0' + date.getMinutes()).slice(-2);
var fileName = "ExploratorySession_" + browserInfoString + "_" + startDateTime + ".json";
// Crear data URL
const dataUrl = 'data:application/json;base64,' + btoa(jsonData);
chrome.downloads.download({
url: dataUrl,
filename: fileName,
saveAs: true
});
return true;
}
function importSessionJSon(JSonSessionData) {
debugger;
var exportJSonService = new JSonSessionService();
var importedSession = exportJSonService.getSession(JSonSessionData);
if (importedSession == null)
return false;
clearSession();
session = importedSession;
return true;
}