fix(plugin-inline-script-content): Ensure event handlers added before Bugsnag can be removed #582
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: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. SinceremoveEventListener()
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 originaladdEventListener()
is defined. The check is updated to only wrap it if the object being inspected defines its ownaddEventListener
function, rather than it being in the prototype chain.