Yesterday I ran V8 coverage over all 38 pages of notifio.app and found that a third of the JavaScript executes. The stylesheet is a much smaller problem, 8,348 bytes on the wire against 276,713 of JavaScript, so this is not a performance post. It turned into something more useful: a stylesheet built by a scanner is an index of everything the code could render, and diffing that against what the pages do render finds things no grep would.
The method, and the two traps in it
One request serves the whole site's CSS. Walk its rules, and for each selector ask the page how many elements it matches:
for ( const rule of sheet . cssRules ) { document . querySelectorAll ( rule . selectorText ) . length ; // plus a recursive walk into @layer, @media and @supports }
Enter fullscreen mode
Exit fullscreen mode
That naive version is wrong twice, and both traps flatter you.
State pseudo-classes. document.querySelectorAll('a:hover') is valid and returns zero, because nothing is hovered in a browser with no pointer.…