Created
January 10, 2021 13:55
-
-
Save willwillems/95939fd96e97eaea6abff757fb3a0485 to your computer and use it in GitHub Desktop.
Get matching CSS rules for a DOM element using the new CSSOM stylesheet implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getMatchingCSSRulesForEl (el) { | |
return [...document.styleSheets] | |
.filter(ss => { try { return ss.cssRules; } catch (e) { return false } }) | |
.flatMap(e => [...e.cssRules].filter(r => el.matches(r.selectorText))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Due to a weird CORS CSS implementation in Chrome the only reliable way to filter inaccessible styleSheets is to try to access them and fail.