@@ -17,22 +17,28 @@ import prismStyles from "prismjs/themes/prism-tomorrow.min.css?raw";
1717
1818const extractUserCssVariables = ( ) => {
1919 let css = `` ;
20- const root = document . documentElement ;
21- if ( root . computedStyleMap ) {
22- for ( const [ property , cssValue ] of root . computedStyleMap ( ) ) {
23- if ( property . startsWith ( "--" ) ) {
24- for ( const value of cssValue ) {
25- css += `${ property } : ${ value . toString ( ) } ;\n` ;
26- }
27- }
20+
21+ for ( const sheet of document . styleSheets ) {
22+ let rules ;
23+ try {
24+ rules = sheet . cssRules ; // throws for cross-origin sheets
25+ } catch {
26+ continue ;
2827 }
29- } else {
30- // fallback to fully computed styles (aliases won't be inferred)
31- const computedStyles = getComputedStyle ( root ) ;
32- for ( const property of computedStyles ) {
33- if ( property . startsWith ( "--" ) ) {
34- const value = computedStyles . getPropertyValue ( property ) ;
35- css += `${ property } : ${ value } ;\n` ;
28+
29+ for ( const rule of rules ) {
30+ if ( rule instanceof CSSStyleRule ) {
31+ // find selectors like ":root, html"
32+ const parts = rule . selectorText . split ( "," ) . map ( ( s ) => s . trim ( ) ) ;
33+ if ( ! parts . some ( ( part ) => part === ":root" || part === "html" ) ) {
34+ continue ;
35+ }
36+ for ( const property of rule . style ) {
37+ if ( property . startsWith ( "--" ) ) {
38+ const value = rule . style . getPropertyValue ( property ) ;
39+ css += `${ property } : ${ value } ;\n` ;
40+ }
41+ }
3642 }
3743 }
3844 }
0 commit comments