@@ -408,7 +408,7 @@ let debounceTimerID: number | undefined;
408
408
let lastCapturedEventType : string | undefined ;
409
409
let lastCapturedEventTargetId : string | undefined ;
410
410
411
- type SentryWrappedTarget = EventTarget & { _sentryId ?: string } ;
411
+ type SentryWrappedTarget = HTMLElement & { _sentryId ?: string } ;
412
412
413
413
/**
414
414
* Check whether the event is similar to the last captured one. For example, two click events on the same button.
@@ -440,30 +440,33 @@ function isSimilarToLastCapturedEvent(event: Event): boolean {
440
440
* Decide whether an event should be captured.
441
441
* @param event event to be captured
442
442
*/
443
- function shouldSkipDOMEvent ( event : Event ) : boolean {
443
+ function shouldSkipDOMEvent ( eventType : string , target : SentryWrappedTarget | null ) : boolean {
444
444
// We are only interested in filtering `keypress` events for now.
445
- if ( event . type !== 'keypress' ) {
445
+ if ( eventType !== 'keypress' ) {
446
446
return false ;
447
447
}
448
448
449
- try {
450
- const target = event . target as HTMLElement ;
449
+ if ( ! target || ! target . tagName ) {
450
+ return true ;
451
+ }
451
452
452
- if ( ! target || ! target . tagName ) {
453
- return true ;
454
- }
453
+ // Only consider keypress events on actual input elements. This will disregard keypresses targeting body
454
+ // e.g.tabbing through elements, hotkeys, etc.
455
+ if ( target . tagName === 'INPUT' || target . tagName === 'TEXTAREA' || target . isContentEditable ) {
456
+ return false ;
457
+ }
455
458
456
- // Only consider keypress events on actual input elements. This will disregard keypresses targeting body
457
- // e.g.tabbing through elements, hotkeys, etc.
458
- if ( target . tagName === 'INPUT' || target . tagName === 'TEXTAREA' || target . isContentEditable ) {
459
- return false ;
460
- }
459
+ return true ;
460
+ }
461
+
462
+ function getEventTarget ( event : Event ) : SentryWrappedTarget | null {
463
+ try {
464
+ return event . target as SentryWrappedTarget | null ;
461
465
} catch ( e ) {
462
466
// just accessing `target` property can throw an exception in some rare circumstances
463
467
// see: https://github.com/getsentry/sentry-javascript/issues/838
468
+ return null ;
464
469
}
465
-
466
- return true ;
467
470
}
468
471
469
472
/**
@@ -482,17 +485,19 @@ function makeDOMEventHandler(handler: Function, globalListener: boolean = false)
482
485
return ;
483
486
}
484
487
488
+ const target = getEventTarget ( event ) ;
489
+
485
490
// We always want to skip _some_ events.
486
- if ( shouldSkipDOMEvent ( event ) ) {
491
+ if ( shouldSkipDOMEvent ( event . type , target ) ) {
487
492
return ;
488
493
}
489
494
490
495
// Mark event as "seen"
491
496
addNonEnumerableProperty ( event , '_sentryCaptured' , true ) ;
492
497
493
- if ( event . target && ! ( event . target as SentryWrappedTarget ) . _sentryId ) {
498
+ if ( target && ! target . _sentryId ) {
494
499
// Add UUID to event target so we can identify if
495
- addNonEnumerableProperty ( event . target , '_sentryId' , uuid4 ( ) ) ;
500
+ addNonEnumerableProperty ( target , '_sentryId' , uuid4 ( ) ) ;
496
501
}
497
502
498
503
const name = event . type === 'keypress' ? 'input' : event . type ;
@@ -507,7 +512,7 @@ function makeDOMEventHandler(handler: Function, globalListener: boolean = false)
507
512
global : globalListener ,
508
513
} ) ;
509
514
lastCapturedEventType = event . type ;
510
- lastCapturedEventTargetId = event . target ? ( event . target as SentryWrappedTarget ) . _sentryId : undefined ;
515
+ lastCapturedEventTargetId = target ? target . _sentryId : undefined ;
511
516
}
512
517
513
518
// Start a new debounce timer that will prevent us from capturing multiple events that should be grouped together.
0 commit comments