Skip to content

Instantly share code, notes, and snippets.

@tyom
Last active December 17, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyom/5621247 to your computer and use it in GitHub Desktop.
Save tyom/5621247 to your computer and use it in GitHub Desktop.
Count CSS selectors on current page
(function() {
var styleSheets = document.styleSheets;
for (var j = 0; j < styleSheets.length; j++) {
if (styleSheets[j].href) {
var stylesheet = styleSheets[j];
var rules = stylesheet.rules;
var totalSelectors = 0;
for (var i = 0; i < rules.length; i++) {
if (rules[i].selectorText) {
totalSelectors += rules[i].selectorText.split(',').length;
}
}
console.log('---');
console.log('URL: ' + stylesheet.href);
console.log('Rules: ' + stylesheet.rules.length);
console.log('Selectors: ' + totalSelectors);
}
}
return "---";
})()
  1. Add CSSSelectorCounter.js to Chrome Snippets.

  2. If CSS is served from external domains (i.e. CDN) open Chrome with disabled same origin policy: open -a Google\ Chrome --args --disable-web-security

  3. Run CSS Selector Counter snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment