@@ -208,6 +208,8 @@ function startRecorder() {
208
208
document . addEventListener ( 'keydown' , handleKeydown , true ) ;
209
209
document . addEventListener ( 'mouseover' , handleMouseOver , true ) ;
210
210
document . addEventListener ( 'mouseout' , handleMouseOut , true ) ;
211
+ document . addEventListener ( 'focus' , handleFocus , true ) ;
212
+ document . addEventListener ( 'blur' , handleBlur , true ) ;
211
213
console . log ( 'Permanently attached custom event listeners.' ) ;
212
214
}
213
215
@@ -225,6 +227,8 @@ function stopRecorder() {
225
227
document . removeEventListener ( 'keydown' , handleKeydown , true ) ; // Remove keydown listener
226
228
document . removeEventListener ( 'mouseover' , handleMouseOver , true ) ;
227
229
document . removeEventListener ( 'mouseout' , handleMouseOut , true ) ;
230
+ document . removeEventListener ( 'focus' , handleFocus , true ) ;
231
+ document . removeEventListener ( 'blur' , handleBlur , true ) ;
228
232
} else {
229
233
console . log ( 'Recorder not running, cannot stop.' ) ;
230
234
}
@@ -397,6 +401,7 @@ function handleKeydown(event: KeyboardEvent) {
397
401
398
402
// Store the current overlay to manage its lifecycle
399
403
let currentOverlay : HTMLDivElement | null = null ;
404
+ let currentFocusOverlay : HTMLDivElement | null = null ;
400
405
401
406
// Handle mouseover to create overlay
402
407
function handleMouseOver ( event : MouseEvent ) {
@@ -474,6 +479,69 @@ function handleMouseOut(event: MouseEvent) {
474
479
}
475
480
}
476
481
482
+ // Handle focus to create red overlay for input elements
483
+ function handleFocus ( event : FocusEvent ) {
484
+ if ( ! isRecordingActive ) return ;
485
+ const targetElement = event . target as HTMLElement ;
486
+ if (
487
+ ! targetElement ||
488
+ ! [ 'INPUT' , 'TEXTAREA' , 'SELECT' ] . includes ( targetElement . tagName )
489
+ )
490
+ return ;
491
+
492
+ // Remove any existing focus overlay to avoid duplicates
493
+ if ( currentFocusOverlay ) {
494
+ currentFocusOverlay . remove ( ) ;
495
+ currentFocusOverlay = null ;
496
+ }
497
+
498
+ try {
499
+ const xpath = getXPath ( targetElement ) ;
500
+ let elementToHighlight : HTMLElement | null = document . evaluate (
501
+ xpath ,
502
+ document ,
503
+ null ,
504
+ XPathResult . FIRST_ORDERED_NODE_TYPE ,
505
+ null
506
+ ) . singleNodeValue as HTMLElement | null ;
507
+ if ( ! elementToHighlight ) {
508
+ const enhancedSelector = getEnhancedCSSSelector ( targetElement , xpath ) ;
509
+ elementToHighlight = document . querySelector ( enhancedSelector ) ;
510
+ }
511
+ if ( elementToHighlight ) {
512
+ const rect = elementToHighlight . getBoundingClientRect ( ) ;
513
+ const focusOverlay = document . createElement ( 'div' ) ;
514
+ focusOverlay . className = 'focus-overlay' ;
515
+ Object . assign ( focusOverlay . style , {
516
+ position : 'absolute' ,
517
+ top : `${ rect . top + window . scrollY } px` ,
518
+ left : `${ rect . left + window . scrollX } px` ,
519
+ width : `${ rect . width } px` ,
520
+ height : `${ rect . height } px` ,
521
+ border : '2px solid red' ,
522
+ backgroundColor : 'rgba(255, 0, 0, 0.2)' , // Red tint
523
+ pointerEvents : 'none' ,
524
+ zIndex : '2147483100' , // Higher than mouseover overlay (2147483000)
525
+ } ) ;
526
+ document . body . appendChild ( focusOverlay ) ;
527
+ currentFocusOverlay = focusOverlay ;
528
+ } else {
529
+ console . warn ( 'No element found to highlight for focus, xpath:' , xpath ) ;
530
+ }
531
+ } catch ( error ) {
532
+ console . error ( 'Error creating focus overlay:' , error ) ;
533
+ }
534
+ }
535
+
536
+ // Handle blur to remove focus overlay
537
+ function handleBlur ( event : FocusEvent ) {
538
+ if ( ! isRecordingActive ) return ;
539
+ if ( currentFocusOverlay ) {
540
+ currentFocusOverlay . remove ( ) ;
541
+ currentFocusOverlay = null ;
542
+ }
543
+ }
544
+
477
545
export default defineContentScript ( {
478
546
matches : [ '<all_urls>' ] ,
479
547
main ( ctx ) {
@@ -528,6 +596,8 @@ export default defineContentScript({
528
596
document . removeEventListener ( 'keydown' , handleKeydown , true ) ;
529
597
document . removeEventListener ( 'mouseover' , handleMouseOver , true ) ;
530
598
document . removeEventListener ( 'mouseout' , handleMouseOut , true ) ;
599
+ document . removeEventListener ( 'focus' , handleFocus , true ) ;
600
+ document . removeEventListener ( 'blur' , handleBlur , true ) ;
531
601
stopRecorder ( ) ; // Ensure rrweb is stopped
532
602
} ) ;
533
603
0 commit comments