You can iterate over document.styleSheets to get each style sheet, then iterate over the rules to find everything that matches a class or ID selector. The following is a simplistic approach that provides a general approach, the regular expressions to get the classes and IDs from the rules needs more work.
function getClassesAndIds() {
var sheet, sheets = document.styleSheets;
var rule, rules;
var classes = [];
var ids = [];
var temp;
for (var i=0, iLen=sheets.length; i<iLen; i++) {
sheet = sheets[i];
rules = sheet.rules;
for (var j=0, jLen=rules.length; j<jLen; j++) {
rule = rules[j];
// Get the classes
temp = rule.cssText.match(/\.\w+/g);
if (temp) {
classes.push.apply(classes, temp);
}
// Get the IDs
temp = rule.cssText.match(/\#\w+/g);
if (temp) {
ids.push.apply(ids, temp);
}
}
}
// Return an array of the class and ID arrays
return [classes,ids];
// or as an object
// return {classes:classes, ids:ids};
}
window.onload = function() {
console.log(getClassesAndIds());
};
From memory there were some quirks in older IE around sheets and rules but right now it evades me…