Skip to content

Instantly share code, notes, and snippets.

@Robdel12
Last active November 13, 2020 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robdel12/5cd25c39ccf58b192402c2c984146d81 to your computer and use it in GitHub Desktop.
Save Robdel12/5cd25c39ccf58b192402c2c984146d81 to your computer and use it in GitHub Desktop.
// snagged from MDN
// The goal is to take all the CSS created in the CSS Object Model (CSSOM)
// and inject it into the DOM so Percy can render it safely in our browsers
let cssOmStyles = [].slice.call(document.styleSheets).reduce((prev: String, styleSheet: CSSStyleSheet) => {
// Make sure it has a rulesheet, does NOT have a href (no external stylesheets), and isn't already in the DOM.
let hasHref = styleSheet.href;
let hasStyleInDom = styleSheet.ownerNode.innerText.length > 0;
if (styleSheet.cssRules && !hasHref && !hasStyleInDom) {
return (
prev +
[].slice.call(styleSheet.cssRules).reduce((prev: String, cssRule: CSSRule) => {
return prev + cssRule.cssText;
}, "")
);
} else {
return prev;
}
}, "");
let $stylesheet = document.createElement('style');
$stylesheet.type = 'text/css';
let styles = document.createTextNode(cssOmStyles);
$stylesheet.appendChild(styles);
document.head.appendChild($stylesheet);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment