GoogleChrome / lighthouse

Automated auditing, performance metrics, and best practices for the web.
https://developer.chrome.com/docs/lighthouse/overview/
Apache License 2.0
28.07k stars 9.33k forks source link

Extension unload listeners can fail the bfcache audit but be otherwise undetectable #14980

Closed brendankenny closed 1 year ago

brendankenny commented 1 year ago

From an issue @rviscomi ran into:

An extension can inject an unload listener into the main frame and the bfcache audit will fail on this reason, but the no-unload-listeners audit will pass, and a user looking in the DevTools Elements panel or running getEventListeners(window) in the console will similarly find no 'unload' listeners. This ends up very challenging to debug.

To repro:

Failing bfcache audit in Lighthouse due to 'an unload handler in the main frame' Passing 'no unload event listeners' audit in Lighthouse, with no unload listeners listed

This is due to the 'unload' listener being added inside a content script injected on document_start, which runs in an isolated execution context. Unless no-unload-listeners starts running DOMDebugger.getEventListeners on a window reference from the isolated context, those listeners are invisible to the user (similar to how users need to know to pick the other execution context in the DevTools console dropdown for getEventListeners(window) to show the injected script's unload listeners).

Lighthouse could

connorjclark commented 1 year ago

cycle through execution contexts for the queries for this kind of audit (probably good in general)

Target manager can listen to Runtime.executionContextCreated (and keep association with what session/target it came from), and GlobalListeners gatherer can loop through each when collected the listeners for the main target's window.

Eventually bfcache failures reasons will cite source locations, which would have really helped here. (@brendankenny could link to it)

adamraine commented 1 year ago

cycle through execution contexts for the queries for this kind of audit (probably good in general)

This seems like a good path forward. We will go through just the main frame contexts.