Skip to content

Instantly share code, notes, and snippets.

@mikemunsie
Created January 4, 2018 19:44
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 mikemunsie/0d7e4affb9a232571dbffc8fb900a0a3 to your computer and use it in GitHub Desktop.
Save mikemunsie/0d7e4affb9a232571dbffc8fb900a0a3 to your computer and use it in GitHub Desktop.
Find Stylesheet Classes
// Make one massive array of all the rules from the stylesheets on the page
const styleSheetRules = Object.keys(document.styleSheets).reduce((rules, i) => {
const styleRules = document.styleSheets[i].cssRules || [];
Object.keys(styleRules).forEach((j) => {
rules.push(styleRules[j]);
});
return rules;
}, []);
// Function used to lookup the rules from the stylesheet
const findRules = (search) => {
const rulesFound = styleSheetRules.reduce((items, rule) => {
const selectorText = rule.selectorText;
if (!selectorText) {
return items;
}
if (selectorText.indexOf(search) > -1) {
const classes = selectorText.split(',').forEach((i) => {
const item = i.split(' ')[0].trim().replace('.','');
if (item) {
items.push(item);
}
});
}
return items;
}, []);
return Array.from(new Set(rulesFound)).sort();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment