Last active
December 30, 2015 06:29
-
-
Save renatosnrg/7789701 to your computer and use it in GitHub Desktop.
Count total rules and selectors for each CSS file in a web page
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
function showCSSCountInfo(options) { | |
var styleSheets = document.styleSheets, | |
totalStyleSheets = styleSheets.length; | |
options = options || {}; | |
alert_exceeded = options.alert_exceeded || true; | |
max = options.max || 4096; | |
for (var j = 0; j < totalStyleSheets; j++) { | |
var styleSheet = styleSheets[j], | |
rules = styleSheet.cssRules, | |
totalSelectorsInStylesheet = 0; | |
var totalRulesInStylesheet = rules ? rules.length : 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); | |
if (alert_exceeded && totalSelectorsInStylesheet > max) { | |
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