diff --git a/packages/browser/src/integrations/browserapierrors.ts b/packages/browser/src/integrations/browserapierrors.ts index e9fb6e5d0270..2a66a9ad3e5b 100644 --- a/packages/browser/src/integrations/browserapierrors.ts +++ b/packages/browser/src/integrations/browserapierrors.ts @@ -172,18 +172,16 @@ function _wrapEventTarget(target: string): void { } fill(proto, 'addEventListener', function (original: VoidFunction,): ( - eventName: string, - fn: EventListenerObject, - options?: boolean | AddEventListenerOptions, + ...args: Parameters ) => void { return function ( this: unknown, - eventName: string, - fn: EventListenerObject, - options?: boolean | AddEventListenerOptions, + eventName, + fn, + options, ): (eventName: string, fn: EventListenerObject, capture?: boolean, secure?: boolean) => void { try { - if (typeof fn.handleEvent === 'function') { + if (isEventListenerObject(fn)) { // ESlint disable explanation: // First, it is generally safe to call `wrap` with an unbound function. Furthermore, using `.bind()` would // introduce a bug here, because bind returns a new function that doesn't have our @@ -202,7 +200,7 @@ function _wrapEventTarget(target: string): void { }, }); } - } catch (err) { + } catch { // can sometimes get 'Permission denied to access property "handle Event' } @@ -226,16 +224,9 @@ function _wrapEventTarget(target: string): void { fill(proto, 'removeEventListener', function (originalRemoveEventListener: () => void,): ( this: unknown, - eventName: string, - fn: EventListenerObject, - options?: boolean | EventListenerOptions, + ...args: Parameters ) => () => void { - return function ( - this: unknown, - eventName: string, - fn: EventListenerObject, - options?: boolean | EventListenerOptions, - ): () => void { + return function (this: unknown, eventName, fn, options): () => void { /** * There are 2 possible scenarios here: * @@ -253,16 +244,19 @@ function _wrapEventTarget(target: string): void { * then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible * to get rid of the initial handler and it'd stick there forever. */ - const wrappedEventHandler = fn as unknown as WrappedFunction; try { - const originalEventHandler = wrappedEventHandler && wrappedEventHandler.__sentry_wrapped__; + const originalEventHandler = (fn as WrappedFunction).__sentry_wrapped__; if (originalEventHandler) { originalRemoveEventListener.call(this, eventName, originalEventHandler, options); } } catch (e) { // ignore, accessing __sentry_wrapped__ will throw in some Selenium environments } - return originalRemoveEventListener.call(this, eventName, wrappedEventHandler, options); + return originalRemoveEventListener.call(this, eventName, fn, options); }; }); } + +function isEventListenerObject(obj: unknown): obj is EventListenerObject { + return typeof (obj as EventListenerObject).handleEvent === 'function'; +}