Created
February 24, 2022 13:11
-
-
Save ramakay/625e57ca0977171e107773a8da504b71 to your computer and use it in GitHub Desktop.
Count stylesheet rules per document
This file contains hidden or 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
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