@@ -531,17 +531,22 @@ function instrumentDOM(): void {
531
531
options ?: boolean | AddEventListenerOptions ,
532
532
) : AddEventListener {
533
533
if ( type === 'click' || type == 'keypress' ) {
534
- const el = this as InstrumentedElement ;
535
- const handlers = ( el . __sentry_instrumentation_handlers__ = el . __sentry_instrumentation_handlers__ || { } ) ;
536
- const handlerForType = ( handlers [ type ] = handlers [ type ] || { refCount : 0 } ) ;
537
-
538
- if ( ! handlerForType . handler ) {
539
- const handler = makeDOMEventHandler ( triggerDOMHandler ) ;
540
- handlerForType . handler = handler ;
541
- originalAddEventListener . call ( this , type , handler , options ) ;
542
- }
534
+ try {
535
+ const el = this as InstrumentedElement ;
536
+ const handlers = ( el . __sentry_instrumentation_handlers__ = el . __sentry_instrumentation_handlers__ || { } ) ;
537
+ const handlerForType = ( handlers [ type ] = handlers [ type ] || { refCount : 0 } ) ;
543
538
544
- handlerForType . refCount += 1 ;
539
+ if ( ! handlerForType . handler ) {
540
+ const handler = makeDOMEventHandler ( triggerDOMHandler ) ;
541
+ handlerForType . handler = handler ;
542
+ originalAddEventListener . call ( this , type , handler , options ) ;
543
+ }
544
+
545
+ handlerForType . refCount += 1 ;
546
+ } catch ( e ) {
547
+ // Accessing dom properties is always fragile.
548
+ // Also allows us to skip `addEventListenrs` calls with no proper `this` context.
549
+ }
545
550
}
546
551
547
552
return originalAddEventListener . call ( this , type , listener , options ) ;
@@ -555,6 +560,7 @@ function instrumentDOM(): void {
555
560
listener : EventListenerOrEventListenerObject ,
556
561
options ?: boolean | EventListenerOptions ,
557
562
) : ( ) => void {
563
+ // Skip `addEventListenrs` calls with no proper `this` context.
558
564
if ( type === 'click' || type == 'keypress' ) {
559
565
try {
560
566
const el = this as InstrumentedElement ;
@@ -576,7 +582,8 @@ function instrumentDOM(): void {
576
582
}
577
583
}
578
584
} catch ( e ) {
579
- // accessing dom properties is always fragile
585
+ // Accessing dom properties is always fragile.
586
+ // Also allows us to skip `addEventListenrs` calls with no proper `this` context.
580
587
}
581
588
}
582
589
0 commit comments