1- // background.js - Service Worker for screenshot capture
2- // Chrome extensions can only capture screenshots from the background script
3- // Listen for screenshot requests from content script
4- chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
5- if ( request . action === 'captureScreenshot' ) {
6- handleScreenshotCapture ( sender . tab . id , request . options )
7- . then ( screenshot => {
8- sendResponse ( { success : true , screenshot } ) ;
9- } )
10- . catch ( error => {
11- console . error ( '[Sentience] Screenshot capture failed:' , error ) ;
12- sendResponse ( {
13- success : false ,
14- error : error . message || 'Screenshot capture failed'
15- } ) ;
16- } ) ;
17-
18- // Return true to indicate we'll send response asynchronously
19- return true ;
20- }
21- } ) ;
22-
23- /**
24- * Capture screenshot of the active tab
25- * @param {number } tabId - Tab ID to capture
26- * @param {Object } options - Screenshot options
27- * @returns {Promise<string> } Base64-encoded PNG data URL
28- */
29- async function handleScreenshotCapture ( tabId , options = { } ) {
30- try {
31- const {
32- format = 'png' , // 'png' or 'jpeg'
33- quality = 90 // JPEG quality (0-100), ignored for PNG
34- } = options ;
35-
36- // Capture visible tab as data URL
37- const dataUrl = await chrome . tabs . captureVisibleTab ( null , {
38- format : format ,
39- quality : quality
40- } ) ;
41-
42- console . log ( `[Sentience] Screenshot captured: ${ format } , size: ${ dataUrl . length } bytes` ) ;
43-
44- return dataUrl ;
45- } catch ( error ) {
46- console . error ( '[Sentience] Screenshot error:' , error ) ;
47- throw new Error ( `Failed to capture screenshot: ${ error . message } ` ) ;
48- }
49- }
50-
51- /**
52- * Optional: Add viewport-specific capture (requires additional setup)
53- * This would allow capturing specific regions, not just visible area
54- */
55- async function captureRegion ( tabId , region ) {
56- // For region capture, you'd need to:
57- // 1. Capture full visible tab
58- // 2. Use Canvas API to crop to region
59- // 3. Return cropped image
60-
61- // Not implemented in this basic version
62- throw new Error ( 'Region capture not yet implemented' ) ;
63- }
1+ Not Found
0 commit comments