Created
July 12, 2019 15:35
-
-
Save willstocks/0f187623daca6c29f8f94a431f4f4334 to your computer and use it in GitHub Desktop.
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want/need to put this behind a user checkbox, rather than straight up automated