Skip to content

Instantly share code, notes, and snippets.

@westc
Last active June 6, 2024 01:46
Show Gist options
  • Save westc/8544da8618c845f7219b033fc80ee013 to your computer and use it in GitHub Desktop.
Save westc/8544da8618c845f7219b033fc80ee013 to your computer and use it in GitHub Desktop.
Bookmarklet - Dark mode code.
(()=>{
const docElem = document.documentElement;
const FILTERS = [
"",
"invert(1) hue-rotate(180deg) contrast(0.75)",
"invert(1) contrast(0.75)",
"invert(1) hue-rotate(180deg)"
];
const VAR_NAME = "cwestDarkMode";
const DATA_VAR_NAME = `data-${VAR_NAME.replace(/[A-Z]/g, '-$&').toLowerCase()}`;
let filterIndex = docElem.dataset[VAR_NAME];
filterIndex = filterIndex
? (+filterIndex + 1) % FILTERS.length
: +(localStorage.getItem(VAR_NAME) || 1);
docElem.style.filter = FILTERS[filterIndex];
// Store the data in local storage and in the docElem.
docElem.setAttribute(DATA_VAR_NAME, filterIndex);
localStorage.setItem(VAR_NAME, filterIndex);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment