@@ -8,6 +8,7 @@ let settings = {
8
8
maxLogSize : 20000 ,
9
9
showRequestHeaders : false ,
10
10
showResponseHeaders : false ,
11
+ screenshotPath : "" , // Add new setting for screenshot path
11
12
} ;
12
13
13
14
// Keep track of debugger state
@@ -368,17 +369,144 @@ chrome.devtools.panels.create("Browser Logs", "", "panel.html", (panel) => {
368
369
// Initial attach - we'll keep the debugger attached as long as DevTools is open
369
370
attachDebugger ( ) ;
370
371
371
- // Remove the panel show/hide listeners since we want to stay attached
372
- // panel.onShown.addListener(() => {
373
- // attachDebugger();
374
- // });
375
- //
376
- // panel.onHidden.addListener(() => {
377
- // detachDebugger();
378
- // });
372
+ // Add message passing to panel.js
373
+ panel . onShown . addListener ( ( panelWindow ) => {
374
+ panelWindow . postMessage ( { type : "initializeSelectionButton" } , "*" ) ;
375
+ } ) ;
379
376
} ) ;
380
377
381
378
// Clean up only when the entire DevTools window is closed
382
379
window . addEventListener ( "unload" , ( ) => {
383
380
detachDebugger ( ) ;
384
381
} ) ;
382
+
383
+ // Function to capture and send element data
384
+ function captureAndSendElement ( ) {
385
+ chrome . devtools . inspectedWindow . eval (
386
+ `(function() {
387
+ const el = $0; // $0 is the currently selected element in DevTools
388
+ if (!el) return null;
389
+
390
+ const rect = el.getBoundingClientRect();
391
+
392
+ return {
393
+ tagName: el.tagName,
394
+ id: el.id,
395
+ className: el.className,
396
+ textContent: el.textContent?.substring(0, 100),
397
+ attributes: Array.from(el.attributes).map(attr => ({
398
+ name: attr.name,
399
+ value: attr.value
400
+ })),
401
+ dimensions: {
402
+ width: rect.width,
403
+ height: rect.height,
404
+ top: rect.top,
405
+ left: rect.left
406
+ },
407
+ innerHTML: el.innerHTML.substring(0, 500)
408
+ };
409
+ })()` ,
410
+ ( result , isException ) => {
411
+ if ( isException || ! result ) return ;
412
+
413
+ console . log ( "Element selected:" , result ) ;
414
+
415
+ // Send to browser connector
416
+ sendToBrowserConnector ( {
417
+ type : "selected-element" ,
418
+ timestamp : Date . now ( ) ,
419
+ element : result ,
420
+ } ) ;
421
+ }
422
+ ) ;
423
+ }
424
+
425
+ // Listen for element selection in the Elements panel
426
+ chrome . devtools . panels . elements . onSelectionChanged . addListener ( ( ) => {
427
+ captureAndSendElement ( ) ;
428
+ } ) ;
429
+
430
+ // WebSocket connection management
431
+ let ws = null ;
432
+ let wsReconnectTimeout = null ;
433
+ const WS_RECONNECT_DELAY = 5000 ; // 5 seconds
434
+
435
+ function setupWebSocket ( ) {
436
+ if ( ws ) {
437
+ ws . close ( ) ;
438
+ }
439
+
440
+ ws = new WebSocket ( "ws://localhost:3025/extension-ws" ) ;
441
+
442
+ ws . onopen = ( ) => {
443
+ console . log ( "WebSocket connected" ) ;
444
+ if ( wsReconnectTimeout ) {
445
+ clearTimeout ( wsReconnectTimeout ) ;
446
+ wsReconnectTimeout = null ;
447
+ }
448
+ } ;
449
+
450
+ ws . onmessage = async ( event ) => {
451
+ try {
452
+ const message = JSON . parse ( event . data ) ;
453
+
454
+ if ( message . type === "take-screenshot" ) {
455
+ if ( ! settings . screenshotPath ) {
456
+ ws . send (
457
+ JSON . stringify ( {
458
+ type : "screenshot-error" ,
459
+ error : "Screenshot path not configured" ,
460
+ } )
461
+ ) ;
462
+ return ;
463
+ }
464
+
465
+ // Capture screenshot of the current tab
466
+ chrome . tabs . captureVisibleTab ( null , { format : "png" } , ( dataUrl ) => {
467
+ if ( chrome . runtime . lastError ) {
468
+ ws . send (
469
+ JSON . stringify ( {
470
+ type : "screenshot-error" ,
471
+ error : chrome . runtime . lastError . message ,
472
+ } )
473
+ ) ;
474
+ return ;
475
+ }
476
+
477
+ ws . send (
478
+ JSON . stringify ( {
479
+ type : "screenshot-data" ,
480
+ data : dataUrl ,
481
+ path : settings . screenshotPath ,
482
+ } )
483
+ ) ;
484
+ } ) ;
485
+ }
486
+ } catch ( error ) {
487
+ console . error ( "Error processing WebSocket message:" , error ) ;
488
+ }
489
+ } ;
490
+
491
+ ws . onclose = ( ) => {
492
+ console . log ( "WebSocket disconnected, attempting to reconnect..." ) ;
493
+ wsReconnectTimeout = setTimeout ( setupWebSocket , WS_RECONNECT_DELAY ) ;
494
+ } ;
495
+
496
+ ws . onerror = ( error ) => {
497
+ console . error ( "WebSocket error:" , error ) ;
498
+ } ;
499
+ }
500
+
501
+ // Initialize WebSocket connection when DevTools opens
502
+ setupWebSocket ( ) ;
503
+
504
+ // Clean up WebSocket when DevTools closes
505
+ window . addEventListener ( "unload" , ( ) => {
506
+ if ( ws ) {
507
+ ws . close ( ) ;
508
+ }
509
+ if ( wsReconnectTimeout ) {
510
+ clearTimeout ( wsReconnectTimeout ) ;
511
+ }
512
+ } ) ;
0 commit comments