I\'ve set some CSS custom properties in my stylesheet:
:root {
--bc: #fff;
--bc-primary: #eee;
--bc-secondary: #ffffd;
}
I can re
Based on LGSon's answer here is something similar but using map
, filter
, and flat
to make it easier to read line by line.
const variables = [].slice.call(document.styleSheets)
.map(styleSheet => [].slice.call(styleSheet.cssRules))
.flat()
.filter(cssRule => cssRule.selectorText === ':root')
.map(cssRule => cssRule.cssText.split('{')[1].split('}')[0].trim().split(';'))
.flat()
.filter(text => text !== "")
.map(text => text.split(':'))
.map(parts => ({key: parts[0].trim(), value: parts[1].trim() }))
;
console.log(variables);
:root {
--foo: #fff;
--bar: #aaa
}