Skip to content

Instantly share code, notes, and snippets.

@willstocks
Created July 12, 2019 15:35
Show Gist options
  • Save willstocks/0f187623daca6c29f8f94a431f4f4334 to your computer and use it in GitHub Desktop.
Save willstocks/0f187623daca6c29f8f94a431f4f4334 to your computer and use it in GitHub Desktop.
function checkTime() {
var timeNow = new Date().getHours();
const night = timeNow >= 16 || timeNow <= 7;
return [timeNow, night];
}
function autoToggleNightMode() {
const bodyClass = document.body.classList;
var myinput = document.getElementsByClassName("switch")[0].getElementsByTagName("input")[0];
var currentTime = checkTime()[0];
var nightHours = checkTime()[1];
if (document.cookie.indexOf("linx_dark") !== -1) {
clearInterval(checkNightMode);
return false;
}
if (nightHours === true) {
myinput.setAttribute("checked", "checked");
bodyClass.contains("dark-mode") === false ? bodyClass.add("dark-mode") : null;
console.log("I've defined hour " + currentTime + " of the day as part of the 'night', so I'm automatically toggling night mode on for you!");
} else {
myinput.hasAttribute("checked") ? myinput.removeAttribute("checked") : null;
bodyClass.contains("dark-mode") === true ? bodyClass.remove("dark-mode") : null;
console.log("It's not night time anymore!!");
}
}
autoToggleNightMode();
var checkNightMode = setInterval(autoToggleNightMode, 30000);
@willstocks
Copy link
Author

I want/need to put this behind a user checkbox, rather than straight up automated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment