Skip to content

Instantly share code, notes, and snippets.

@jonesnxt
Created July 11, 2022 20:45
Show Gist options
  • Save jonesnxt/68ed797e70423b5853aa05cad0db5a84 to your computer and use it in GitHub Desktop.
Save jonesnxt/68ed797e70423b5853aa05cad0db5a84 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