Forked from michaelnordmeyer/find-unused-css.js
Last active
December 21, 2023 20:00
-
-
Save pointofpresence/bc503520c69ec5fa2dc9b76f460fd58f to your computer and use it in GitHub Desktop.
Find unused CSS rules on a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
for (var ssi = 0; ssi < document.styleSheets.length; ssi++) { | |
let rules; | |
try { | |
rules = document.styleSheets[ssi].cssRules || []; | |
} catch (e) { | |
rules = [] | |
} | |
var sheetHref = document.styleSheets[ssi].href || 'inline'; | |
for (var ri = 0; ri < rules.length; ri++) { | |
if (!document.querySelectorAll(rules[ri].selectorText).length) { | |
console.log(`${sheetHref}: "${rules[ri].selectorText}" not found.`); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment