Created
February 19, 2015 15:23
-
-
Save mmonkey/af10c394909c3ab5ecbf to your computer and use it in GitHub Desktop.
List all class selectors from stylesheets - (Paste in console)
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
var cssRules = []; | |
var selectors = []; | |
var classes = []; | |
var results = []; | |
// Get all selectors from all styleSheets that aren't undefined | |
for(var i = 0; i < document.styleSheets.length; i++) { | |
for(var j = 0; j < document.styleSheets[i].cssRules.length; j++) { | |
if(typeof document.styleSheets[i].cssRules[j].selectorText !== 'undefined') { | |
cssRules.push(document.styleSheets[i].cssRules[j].selectorText); | |
} | |
} | |
} | |
// Get all classes from selectors | |
for(i = 0; i < cssRules.length; i++) { | |
var selectorList = cssRules[i].split(', '); | |
for(var j = 0; j < selectorList.length; j++) { | |
if(selectorList[j].indexOf('.') === 0) { | |
selectors.push(selectorList[j]); | |
} | |
} | |
} | |
// Split out nested selectors, remove selectors that contain ::before and ::after | |
for(i = 0; i < selectors.length; i++) { | |
var selectorList = selectors[i].split(' '); | |
for(var j = 0; j < selectorList.length; j++) { | |
if(selectorList[j].indexOf('.') === 0) { | |
if(selectorList[j].indexOf(':') === -1) { | |
classes.push(selectorList[j]); | |
} | |
} | |
} | |
} | |
classes.sort(); | |
function uniq(a) { | |
var seen = {}; | |
var out = []; | |
var len = a.length; | |
var j = 0; | |
for(var i = 0; i < len; i++) { | |
var item = a[i]; | |
if(seen[item] !== 1) { | |
seen[item] = 1; | |
out[j++] = item; | |
} | |
} | |
return out; | |
} | |
results = uniq(classes); | |
console.log(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment