Skip to content

Commit

Permalink
some refs...
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 25, 2024
1 parent a068685 commit e6e5c04
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/browser/src/integrations/browserapierrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,16 @@ function _wrapEventTarget(target: string): void {
}

fill(proto, 'addEventListener', function (original: VoidFunction,): (
eventName: string,
fn: EventListenerObject,
options?: boolean | AddEventListenerOptions,
...args: Parameters<typeof WINDOW.addEventListener>
) => 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
Expand All @@ -202,7 +200,7 @@ function _wrapEventTarget(target: string): void {
},
});
}
} catch (err) {
} catch {
// can sometimes get 'Permission denied to access property "handle Event'
}

Expand All @@ -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<typeof WINDOW.removeEventListener>
) => () => 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:
*
Expand All @@ -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';
}

0 comments on commit e6e5c04

Please sign in to comment.