Last active
January 21, 2023 00:55
-
-
Save johnmellor/d86ec8c4c9eca7e8233b8394fb9dc30a to your computer and use it in GitHub Desktop.
Bookmarklet to toggle dark theme on any website
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
javascript: | |
/* Toggle dark theme. Tested in Chrome 108. */ | |
(()=>{ | |
var id = 'toggle-dark-theme-bookmarklet'; | |
var el = document.getElementById(id); | |
if (el) { el.remove(); return; } | |
document.head.insertAdjacentHTML('beforeend', ` | |
<style id="${id}"> | |
html { | |
filter: invert(100%) hue-rotate(180deg); | |
} | |
/* Invert photos and videos back to normal. But not icons/graphics since | |
inverting a black on transparent graphic (but not its background) might | |
make it black on black; the hue-rotate on html preserves icon/graphic | |
hues which is good enough and much safer. */ | |
video, | |
iframe:is([src*="youtube.com"],[src*="vimeo.com"]), | |
img:not([src*=".apng"],[src*=".gif"],[src*=".png"],[src*=".svg"]), | |
[style*="background-image:"]:not(html,body):not([style*="gradient"],[style*=".apng"],[style*=".gif"],[style*=".png"],[style*=".svg"]) { | |
filter: hue-rotate(180deg) invert(100%); | |
} | |
/* If we inverted an element with background-image back to normal, don't | |
let its descendants get re-inverted. */ | |
[style*="background-image:"]:not(html,body):not([style*="gradient"],[style*=".apng"],[style*=".gif"],[style*=".png"],[style*=".svg"]) * { | |
filter: none; | |
} | |
</style> | |
`); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment