Created
January 4, 2018 19:44
-
-
Save mikemunsie/0d7e4affb9a232571dbffc8fb900a0a3 to your computer and use it in GitHub Desktop.
Find Stylesheet Classes
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
// 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