Skip to content

Instantly share code, notes, and snippets.

@taskinoz
Last active October 16, 2019 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taskinoz/76386e74e9f372902857aa29032342b4 to your computer and use it in GitHub Desktop.
Save taskinoz/76386e74e9f372902857aa29032342b4 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Custom Dark Mode
// @namespace https://taskinoz.com
// @version 0.1
// @description A custom dark mode that activates at the times you want
// @author You
// @match https://*/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
var active;
var timeStart = [16, 46]; // 24 Hour Time
var timeEnd = [16, 47]; // 24 Hour Time
var now = new Date();
var sameDay;
var checkInterval = 0.01; // Minutes
var x;
// Customm CSS
var CSS = `/* Add CSS Here */
body{
transition: 0.3s;
background: #000;
}
/* Add CSS Here */`;
// Check if it spans over one day to the next
if (timeStart[0] > timeEnd[0]) {
sameDay = false;
console.log(false);
} else {
sameDay = true;
console.log(true);
}
// Convert to date
timeStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), timeStart[0], timeStart[1]);
timeEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), timeEnd[0], timeEnd[1]);
console.log(timeStart);
console.log(timeEnd);
function checkTime() {
if (sameDay) {
if (timeEnd < now && timeStart > now) {
if (active) {
removeCSS();
console.log("Same - Remove");
}
}
else {
if (!active){
addCSS();
console.log("Same - Add");
}
}
}
else {
if (timeStart < now && timeEnd < now) {
if (!active){
addCSS();
console.log("Not - Add");
}
}
else {
if (active) {
removeCSS();
console.log("Not - Remove");
}
}
}
reCheck();
console.log("check");
}
function reCheck() {
setTimeout(function () {
checkTime();
}, checkInterval * 1000 * 60)
now = new Date();
console.log("check:" + now);
}
function addCSS() {
document.getElementsByTagName("body")[0].innerHTML += '<style id="custom-dark-mode">\n/*Dark Mode CSS - Generated by Taskinoz\'s Tampermonkey Extension*/\n' + CSS + '</style>';
active = true;
}
function removeCSS() {
x = document.getElementById("custom-dark-mode");
x.parentNode.removeChild(x);
active = false
}
checkTime();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment