@@ -172,18 +172,16 @@ function _wrapEventTarget(target: string): void {
172172 }
173173
174174 fill ( proto , 'addEventListener' , function ( original : VoidFunction , ) : (
175- eventName : string ,
176- fn : EventListenerObject ,
177- options ?: boolean | AddEventListenerOptions ,
175+ ...args : Parameters < typeof WINDOW . addEventListener >
178176 ) => void {
179177 return function (
180178 this : unknown ,
181- eventName : string ,
182- fn : EventListenerObject ,
183- options ?: boolean | AddEventListenerOptions ,
179+ eventName ,
180+ fn ,
181+ options ,
184182 ) : ( eventName : string , fn : EventListenerObject , capture ?: boolean , secure ?: boolean ) => void {
185183 try {
186- if ( typeof fn . handleEvent === 'function' ) {
184+ if ( isEventListenerObject ( fn ) ) {
187185 // ESlint disable explanation:
188186 // First, it is generally safe to call `wrap` with an unbound function. Furthermore, using `.bind()` would
189187 // introduce a bug here, because bind returns a new function that doesn't have our
@@ -202,7 +200,7 @@ function _wrapEventTarget(target: string): void {
202200 } ,
203201 } ) ;
204202 }
205- } catch ( err ) {
203+ } catch {
206204 // can sometimes get 'Permission denied to access property "handle Event'
207205 }
208206
@@ -226,16 +224,9 @@ function _wrapEventTarget(target: string): void {
226224
227225 fill ( proto , 'removeEventListener' , function ( originalRemoveEventListener : ( ) => void , ) : (
228226 this : unknown ,
229- eventName : string ,
230- fn : EventListenerObject ,
231- options ?: boolean | EventListenerOptions ,
227+ ...args : Parameters < typeof WINDOW . removeEventListener >
232228 ) => ( ) => void {
233- return function (
234- this : unknown ,
235- eventName : string ,
236- fn : EventListenerObject ,
237- options ?: boolean | EventListenerOptions ,
238- ) : ( ) => void {
229+ return function ( this : unknown , eventName , fn , options ) : ( ) => void {
239230 /**
240231 * There are 2 possible scenarios here:
241232 *
@@ -253,16 +244,19 @@ function _wrapEventTarget(target: string): void {
253244 * then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
254245 * to get rid of the initial handler and it'd stick there forever.
255246 */
256- const wrappedEventHandler = fn as unknown as WrappedFunction ;
257247 try {
258- const originalEventHandler = wrappedEventHandler && wrappedEventHandler . __sentry_wrapped__ ;
248+ const originalEventHandler = ( fn as WrappedFunction ) . __sentry_wrapped__ ;
259249 if ( originalEventHandler ) {
260250 originalRemoveEventListener . call ( this , eventName , originalEventHandler , options ) ;
261251 }
262252 } catch ( e ) {
263253 // ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
264254 }
265- return originalRemoveEventListener . call ( this , eventName , wrappedEventHandler , options ) ;
255+ return originalRemoveEventListener . call ( this , eventName , fn , options ) ;
266256 } ;
267257 } ) ;
268258}
259+
260+ function isEventListenerObject ( obj : unknown ) : obj is EventListenerObject {
261+ return typeof ( obj as EventListenerObject ) . handleEvent === 'function' ;
262+ }
0 commit comments