Skip to content

Instantly share code, notes, and snippets.

@jonesnxt
Last active August 20, 2024 13:06
Show Gist options
  • Save jonesnxt/b8af57e364c328aad29eb512686b2b60 to your computer and use it in GitHub Desktop.
Save jonesnxt/b8af57e364c328aad29eb512686b2b60 to your computer and use it in GitHub Desktop.
Array.from(document.styleSheets).filter((sheet) => { try { sheet.cssRules; return true; } catch (err) { console.log(`Due to CORS issues, this script can't access "${sheet.href}"`); return false; }}).map((sheet) => Array.from(sheet.cssRules).map((rule) => rule.type === rule.FONT_FACE_RULE ? rule.cssText : rule.type === rule.STYLE_RULE ? Array.from(document.querySelectorAll(rule.selectorText)).some((node) => node.getBoundingClientRect().top < window.innerHeight && window.getComputedStyle(node).display !== 'none') ? rule.cssText : null : rule.type === rule.MEDIA_RULE ? `@media ${rule.conditionText} {${Array.from(rule.cssRules).map((mediaRule) => mediaRule.type === mediaRule.FONT_FACE_RULE ? mediaRule.cssText : mediaRule.type === mediaRule.STYLE_RULE ? Array.from(document.querySelectorAll(mediaRule.selectorText)).some((node) => node.getBoundingClientRect().top < window.innerHeight && window.getComputedStyle(node).display !== 'none') ? mediaRule.cssText : null : null).filter((mediaRule) => mediaRule != null).join(' ')}}` : null)).flat().filter((rule) => rule != null && !new RegExp(/^@media.*{}$/).test(rule)).join(' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment