Skip to content

Instantly share code, notes, and snippets.

@hogashi
Last active July 1, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hogashi/3e433566dd038beb0b94bb76cd62973b to your computer and use it in GitHub Desktop.
Save hogashi/3e433566dd038beb0b94bb76cd62973b to your computer and use it in GitHub Desktop.
copy css rules from a document to another one
// at src document
const rules = Array.from(document.styleSheets)
.reduce((sum, sheet) => {
// errors in CORS at some sheets (e.g. qiita)
// like: "Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules"
try {
return [...sum, ...Array.from(sheet.cssRules).map(rule => rule.cssText)];
} catch(e) {
// console.log('errored', e);
return sum;
}
}, []);
// at dst document
const newSheet = document.querySelector('head').appendChild(document.createElement('style')).sheet;
rules.forEach(rule => newSheet.insertRule(rule, newSheet.length));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment