Skip to content

Instantly share code, notes, and snippets.

@jvperrin
Forked from eltoob/gist:4586719
Last active December 23, 2015 06:48
Show Gist options
  • Save jvperrin/6595957 to your computer and use it in GitHub Desktop.
Save jvperrin/6595957 to your computer and use it in GitHub Desktop.
A script to use the chrome console to run analysis on number of selectors and rules in stylesheets on a page. A stylesheet reported as "null" means that the rules are not from a linked stylesheet but originate from style tags in the document.
var
styleSheets = document.styleSheets,
totalStyleSheets = styleSheets.length;
for (var j = 0; j < totalStyleSheets; j++){
var
styleSheet = styleSheets[j],
rules = styleSheet.cssRules,
totalRulesInStylesheet = rules.length,
totalSelectorsInStylesheet = 0;
for (var i = 0; i < totalRulesInStylesheet; i++) {
if (rules[i].selectorText) {
try {
totalSelectorsInStylesheet += rules[i].selectorText.split(',').length;
} catch(err) {
console.log(err);
}
}
}
console.log("Stylesheet: " + styleSheet.href);
console.log("Total rules: " + totalRulesInStylesheet);
console.log("Total selectors: " + totalSelectorsInStylesheet);
alert("Stylesheet: " + styleSheet.href + "\n" + "Total rules: " + totalRulesInStylesheet + "\n" + "Total selectors: " + totalSelectorsInStylesheet)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment