Skip to content

Instantly share code, notes, and snippets.

@ramakay
Created February 24, 2022 13:11
Show Gist options
  • Save ramakay/625e57ca0977171e107773a8da504b71 to your computer and use it in GitHub Desktop.
Save ramakay/625e57ca0977171e107773a8da504b71 to your computer and use it in GitHub Desktop.
Count stylesheet rules per document
var
styleSheets = document.styleSheets,
totalStyleSheets = styleSheets.length,
overAllDocumentRules =0;
try {
for (var j = 0; j < totalStyleSheets; j++){
try{
var
styleSheet = styleSheets[j],
rules = styleSheet.cssRules,
totalRulesInStylesheet = rules.length,
totalSelectorsInStylesheet = 0;
for (var i = 0; i < totalRulesInStylesheet; i++) {
if (rules[i].selectorText){
totalSelectorsInStylesheet += rules[i].selectorText.split(',').length;
}
}
}
catch(err){
console.warn("skipping>", styleSheet.href, err);
console.warn(styleSheet)
}
if(totalRulesInStylesheet>1000) console.error("This stylesheet has over 1000 rules")
console.log("Stylesheet: "+styleSheet.href);
console.log(styleSheet)
console.info("Total rules: "+totalRulesInStylesheet);
overAllDocumentRules += totalRulesInStylesheet;
console.warn("Total Document Rules > "+ overAllDocumentRules)
}
}
catch(err){
console.warn("Error",err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment