Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-inline-script-content): Ensure event handlers added before Bugsnag can be removed #582

Merged
merged 1 commit into from
Jul 11, 2019

Conversation

bengourley
Copy link
Contributor

Fixes a bug where an event handler added before Bugsnag is initialised cannot be removed.

The following snippet will continue to log clicks after the removeEventListener function is called:

function click (e) {
  console.log('click event', e)
}
window.addEventListener('click', logClick)
var bugsnagClient = bugsnag('API_KEY')
window.removeEventListener('click', logClick)

With this fix it is now removed correctly.

Details

The inline script tracking code wraps addEventListener() callbacks in a function that tracks where it came from. Since removeEventListener() takes a reference to the function that should be removed, passing it the wrapped function means it is not the same reference as the original function that was added.

The solution is to do as Bugsnag v3 did and call removeEventListener() with both the wrapped and un-wrapped function.

Aside

In debugging this, I also noticed addEventListener() was being wrapped multiple times. It turned out that this is because all of the entries in the list of event target classes share the same object prototype upon which the original addEventListener() is defined. The check is updated to only wrap it if the object being inspected defines its own addEventListener function, rather than it being in the prototype chain.

Copy link

@sazap10 sazap10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SFTM

@bengourley bengourley merged commit 337e987 into next Jul 11, 2019
@bengourley bengourley deleted the remove-event-listener-bug branch July 11, 2019 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants