@@ -339,6 +339,42 @@ async function retestConnectionOnRefresh(tabId) {
339
339
} ) ;
340
340
}
341
341
342
+ chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
343
+ if ( message . type === "DELEGATE_SCREENSHOT" && message . tabId ) {
344
+ // Get the tab we want to capture
345
+ chrome . tabs . get ( message . tabId , ( tab ) => {
346
+ if ( chrome . runtime . lastError ) {
347
+ sendResponse ( { success : false , error : chrome . runtime . lastError . message } ) ;
348
+ return ;
349
+ }
350
+
351
+ // Get the window containing this tab
352
+ chrome . windows . get ( tab . windowId , { populate : true } , ( window ) => {
353
+ // Make sure the tab is active in its window
354
+ chrome . tabs . update ( message . tabId , { active : true } , ( ) => {
355
+ // Focus the window
356
+ chrome . windows . update ( tab . windowId , { focused : true } , ( ) => {
357
+ // Wait a moment for the window to focus
358
+ setTimeout ( ( ) => {
359
+ // Capture the visible tab in that window
360
+ chrome . tabs . captureVisibleTab ( tab . windowId , { format : "png" } , ( dataUrl ) => {
361
+ if ( chrome . runtime . lastError ) {
362
+ sendResponse ( { success : false , error : chrome . runtime . lastError . message } ) ;
363
+ return ;
364
+ }
365
+
366
+ sendResponse ( { success : true , dataUrl : dataUrl } ) ;
367
+ } ) ;
368
+ } , 100 ) ;
369
+ } ) ;
370
+ } ) ;
371
+ } ) ;
372
+ } ) ;
373
+
374
+ return true ; // Keep the message channel open for the async response
375
+ }
376
+ } ) ;
377
+
342
378
// Function to capture and send screenshot
343
379
function captureAndSendScreenshot ( message , settings , sendResponse ) {
344
380
// Get the inspected window's tab
@@ -352,6 +388,16 @@ function captureAndSendScreenshot(message, settings, sendResponse) {
352
388
return ;
353
389
}
354
390
391
+ // Skip DevTools tabs
392
+ if ( tab . url && tab . url . startsWith ( 'devtools://' ) ) {
393
+ console . error ( "Cannot capture DevTools tab:" , tab . url ) ;
394
+ sendResponse ( {
395
+ success : false ,
396
+ error : "Cannot capture DevTools tabs. Please capture the actual application tab." ,
397
+ } ) ;
398
+ return ;
399
+ }
400
+
355
401
// Get all windows to find the one containing our tab
356
402
chrome . windows . getAll ( { populate : true } , ( windows ) => {
357
403
const targetWindow = windows . find ( ( w ) =>
@@ -367,65 +413,71 @@ function captureAndSendScreenshot(message, settings, sendResponse) {
367
413
return ;
368
414
}
369
415
370
- // Capture screenshot of the window containing our tab
371
- chrome . tabs . captureVisibleTab (
372
- targetWindow . id ,
373
- { format : "png" } ,
374
- ( dataUrl ) => {
375
- // Ignore DevTools panel capture error if it occurs
376
- if (
377
- chrome . runtime . lastError &&
378
- ! chrome . runtime . lastError . message . includes ( "devtools://" )
379
- ) {
380
- console . error (
381
- "Error capturing screenshot:" ,
382
- chrome . runtime . lastError
383
- ) ;
384
- sendResponse ( {
385
- success : false ,
386
- error : chrome . runtime . lastError . message ,
387
- } ) ;
388
- return ;
389
- }
390
-
391
- // Send screenshot data to browser connector using configured settings
392
- const serverUrl = `http://${ settings . serverHost } :${ settings . serverPort } /screenshot` ;
393
- console . log ( `Sending screenshot to ${ serverUrl } ` ) ;
394
-
395
- fetch ( serverUrl , {
396
- method : "POST" ,
397
- headers : {
398
- "Content-Type" : "application/json" ,
399
- } ,
400
- body : JSON . stringify ( {
401
- data : dataUrl ,
402
- path : message . screenshotPath ,
403
- } ) ,
404
- } )
405
- . then ( ( response ) => response . json ( ) )
406
- . then ( ( result ) => {
407
- if ( result . error ) {
408
- console . error ( "Error from server:" , result . error ) ;
409
- sendResponse ( { success : false , error : result . error } ) ;
410
- } else {
411
- console . log ( "Screenshot saved successfully:" , result . path ) ;
412
- // Send success response even if DevTools capture failed
413
- sendResponse ( {
414
- success : true ,
415
- path : result . path ,
416
- title : tab . title || "Current Tab" ,
417
- } ) ;
416
+ console . log ( `Capturing screenshot of window ${ targetWindow . id } containing tab ${ message . tabId } ` ) ;
417
+
418
+ // First focus the window and activate the tab
419
+ chrome . windows . update ( targetWindow . id , { focused : true } , ( ) => {
420
+ chrome . tabs . update ( message . tabId , { active : true } , ( ) => {
421
+ // Small delay to ensure the window and tab are focused
422
+ setTimeout ( ( ) => {
423
+ // Capture screenshot of the window containing our tab
424
+ chrome . tabs . captureVisibleTab (
425
+ targetWindow . id ,
426
+ { format : "png" } ,
427
+ ( dataUrl ) => {
428
+ if ( chrome . runtime . lastError ) {
429
+ console . error (
430
+ "Error capturing screenshot:" ,
431
+ chrome . runtime . lastError
432
+ ) ;
433
+ sendResponse ( {
434
+ success : false ,
435
+ error : chrome . runtime . lastError . message ,
436
+ } ) ;
437
+ return ;
438
+ }
439
+
440
+ // Send screenshot data to browser connector using configured settings
441
+ const serverUrl = `http://${ settings . serverHost } :${ settings . serverPort } /screenshot` ;
442
+ console . log ( `Sending screenshot to ${ serverUrl } ` ) ;
443
+
444
+ fetch ( serverUrl , {
445
+ method : "POST" ,
446
+ headers : {
447
+ "Content-Type" : "application/json" ,
448
+ } ,
449
+ body : JSON . stringify ( {
450
+ data : dataUrl ,
451
+ path : message . screenshotPath ,
452
+ } ) ,
453
+ } )
454
+ . then ( ( response ) => response . json ( ) )
455
+ . then ( ( result ) => {
456
+ if ( result . error ) {
457
+ console . error ( "Error from server:" , result . error ) ;
458
+ sendResponse ( { success : false , error : result . error } ) ;
459
+ } else {
460
+ console . log ( "Screenshot saved successfully:" , result . path ) ;
461
+ // Send success response
462
+ sendResponse ( {
463
+ success : true ,
464
+ path : result . path ,
465
+ title : tab . title || "Current Tab" ,
466
+ } ) ;
467
+ }
468
+ } )
469
+ . catch ( ( error ) => {
470
+ console . error ( "Error sending screenshot data:" , error ) ;
471
+ sendResponse ( {
472
+ success : false ,
473
+ error : error . message || "Failed to save screenshot" ,
474
+ } ) ;
475
+ } ) ;
418
476
}
419
- } )
420
- . catch ( ( error ) => {
421
- console . error ( "Error sending screenshot data:" , error ) ;
422
- sendResponse ( {
423
- success : false ,
424
- error : error . message || "Failed to save screenshot" ,
425
- } ) ;
426
- } ) ;
427
- }
428
- ) ;
477
+ ) ;
478
+ } , 100 ) ; // 100ms delay
479
+ } ) ;
480
+ } ) ;
429
481
} ) ;
430
482
} ) ;
431
483
}
0 commit comments