Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
114 lines (100 loc) · 3.22 KB
/
Copy pathbackground.js
File metadata and controls
114 lines (100 loc) · 3.22 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
/**
* React Annotator for Claude - Background Script
* Handles storage, messaging, and screenshot capture
*/
const browserAPI = typeof browser !== 'undefined' ? browser : chrome;
// Listen for messages
browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.type) {
case 'GET_ANNOTATIONS':
handleGetAnnotations(message.url).then(sendResponse);
return true;
case 'DELETE_ANNOTATION':
handleDeleteAnnotation(message.url, message.id).then(sendResponse);
return true;
case 'GET_ALL_ANNOTATIONS':
handleGetAllAnnotations().then(sendResponse);
return true;
case 'SAVE_ANNOTATIONS':
handleSaveAnnotations(message.url, message.annotations).then(sendResponse);
return true;
case 'LOAD_ANNOTATIONS':
handleGetAnnotations(message.url).then(sendResponse);
return true;
case 'CAPTURE_SCREENSHOT':
handleCaptureScreenshot(sender.tab.id).then(sendResponse);
return true;
default:
console.warn('[React Annotator] Unknown message:', message.type);
return false;
}
});
/**
* Capture visible tab screenshot
*/
async function handleCaptureScreenshot(tabId) {
try {
const dataUrl = await browserAPI.tabs.captureVisibleTab(null, {
format: 'png'
});
return { success: true, screenshot: dataUrl };
} catch (error) {
console.error('[React Annotator] Screenshot error:', error);
return { success: false, error: error.message };
}
}
/**
* Save annotations for a URL
*/
async function handleSaveAnnotations(url, annotations) {
try {
const storage = await browserAPI.storage.local.get('annotations');
const all = storage.annotations || {};
all[url] = annotations;
if (!annotations || annotations.length === 0) delete all[url];
await browserAPI.storage.local.set({ annotations: all });
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
/**
* Get annotations for a URL
*/
async function handleGetAnnotations(url) {
try {
const storage = await browserAPI.storage.local.get('annotations');
const all = storage.annotations || {};
return { success: true, annotations: all[url] || [] };
} catch (error) {
return { success: false, error: error.message, annotations: [] };
}
}
/**
* Get all annotations
*/
async function handleGetAllAnnotations() {
try {
const storage = await browserAPI.storage.local.get('annotations');
return { success: true, annotations: storage.annotations || {} };
} catch (error) {
return { success: false, error: error.message, annotations: {} };
}
}
/**
* Delete an annotation
*/
async function handleDeleteAnnotation(url, annotationId) {
try {
const storage = await browserAPI.storage.local.get('annotations');
const all = storage.annotations || {};
if (!all[url]) return { success: false, error: 'No annotations for URL' };
all[url] = all[url].filter(a => a.id !== annotationId);
if (all[url].length === 0) delete all[url];
await browserAPI.storage.local.set({ annotations: all });
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
console.log('[React Annotator] Background script loaded');