Created
September 14, 2021 18:18
-
-
Save easrng/fe442b487cbbed55e6821c0070ae110e to your computer and use it in GitHub Desktop.
Override CSS prefers-color-scheme
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 setTheme(theme) { | |
Array.from(document.styleSheets).map(e => { | |
try { | |
return Array.from(e.cssRules) | |
} catch (e) { | |
return [] | |
} | |
}).flat().map(e => { | |
if (e.constructor != CSSMediaRule) return; | |
if (e.originalConditionText) e.conditionText = e.originalConditionText; | |
else e.originalConditionText = e.conditionText | |
if (theme == "system") return | |
let match = e.conditionText.match(/prefers-color-scheme:\s*(light|dark)/i) | |
if (!match) return; | |
e.conditionText = e.conditionText.replace(match[0], (match[1].toLowerCase() == theme ? 'min' : 'max') + '-width: 0') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment