Last active
September 25, 2023 11:36
-
-
Save jgphilpott/efdec642fc8bbaaae8115bd4fc49bd05 to your computer and use it in GitHub Desktop.
Code to change favicons depending on a users scheme preference.
This file contains hidden or 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
# Credit: https://stackoverflow.com/a/55252008/1544937 | |
lightSchemeIcon = document.querySelector "link#light-scheme-icon" | |
darkSchemeIcon = document.querySelector "link#dark-scheme-icon" | |
schemeMatcher = window.matchMedia "(prefers-color-scheme: light)" | |
schemeUpdate = -> | |
if schemeMatcher.matches | |
document.head.append lightSchemeIcon | |
darkSchemeIcon.remove() | |
window.scheme = "light" | |
else | |
document.head.append darkSchemeIcon | |
lightSchemeIcon.remove() | |
window.scheme = "dark" | |
schemeMatcher.addListener schemeUpdate | |
schemeUpdate() |
This file contains hidden or 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
// Credit: https://stackoverflow.com/a/55252008/1544937 | |
var darkSchemeIcon, lightSchemeIcon, schemeMatcher, schemeUpdate; | |
lightSchemeIcon = document.querySelector("link#light-scheme-icon"); | |
darkSchemeIcon = document.querySelector("link#dark-scheme-icon"); | |
schemeMatcher = window.matchMedia("(prefers-color-scheme: light)"); | |
schemeUpdate = function() { | |
if (schemeMatcher.matches) { | |
document.head.append(lightSchemeIcon); | |
darkSchemeIcon.remove(); | |
return window.scheme = "light"; | |
} else { | |
document.head.append(darkSchemeIcon); | |
lightSchemeIcon.remove(); | |
return window.scheme = "dark"; | |
} | |
}; | |
schemeMatcher.addListener(schemeUpdate); | |
schemeUpdate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment