Created
December 7, 2020 18:22
-
-
Save oussla/4b40e8e6fdb0aa8936a0118028fe7466 to your computer and use it in GitHub Desktop.
CSS Selectors Count
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
/** | |
CSS Selectors Count | |
Count the total number of CSS selectors and provide grand total. | |
To be run on DevTools console. | |
From https://stackoverflow.com/a/12313690/1310132 | |
*/ | |
var | |
styleSheets = document.styleSheets, | |
totalStyleSheets = styleSheets.length, | |
grandTotalSelectors = 0; | |
for (var j = 0; j < totalStyleSheets; j++) { | |
var styleSheet = styleSheets[j]; | |
try { | |
var 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; | |
} | |
} | |
console.log("Stylesheet: " + styleSheet.href); | |
console.log("Total rules: " + totalRulesInStylesheet); | |
console.log("Total selectors: " + totalSelectorsInStylesheet); | |
grandTotalSelectors += totalSelectorsInStylesheet; | |
} | |
catch (e) { | |
console.log(e); | |
} | |
} | |
console.log("Grand Total selectors: " + grandTotalSelectors); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment