Skip to content

Instantly share code, notes, and snippets.

@harish2704
Created September 1, 2017 11:43
Show Gist options
  • Select an option

  • Save harish2704/c77b4a08676c24654eb908190f020080 to your computer and use it in GitHub Desktop.

Select an option

Save harish2704/c77b4a08676c24654eb908190f020080 to your computer and use it in GitHub Desktop.
Convert / extract all applied styles in DOM element convert it into inline css
function getCss( elem ){
var rules = [].slice.call(window.getMatchedCSSRules( elem ));
return rules.map( rule => rule.style.cssText ).join('');
}
function addInlineCss( elem, isRecurssive ){
elem.setAttribute( 'style', getCss( elem ) );
if( isRecurssive && elem.children.length ){
[].slice.call( elem.children ).forEach( v => addInlineCss(v, true ) )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment