AprilSylph / XKit-Rewritten

🧰 The enhancement suite for Tumblr's new web interface
GNU General Public License v3.0
274 stars 44 forks source link

Inject util: Stop processing disconnected elements #1518

Open marcustyphoon opened 6 days ago

marcustyphoon commented 6 days ago

Description

As noted in https://github.com/AprilSylph/XKit-Rewritten/pull/1466#discussion_r1616413489 and previously addressed by #1501, it is possible for exceptions to be logged to the javascript console when we call certain injected functions on a target element that is no longer in the page by the time the injected function is executed.

As of #1514, it is usually fairly difficult for this to happen.

Nevertheless, it should be possible; the first time an injected function is executed it has to be imported asynchronously, for example.

This therefore uses a similar "just don't trigger the listener and resolve the promise" method to prevent any processing code from being run in this now-extremely-rare case. Fortunately, the new structure makes this a oneliner.

Testing steps

If one wants to observe the behavior this fixes:

marcustyphoon commented 5 days ago

Oh, wait... I guess the target of an event has to be a Node, doesn't it. I forgot it defaulted to document.documentElement.