Skip to content

Instantly share code, notes, and snippets.

@alopatindev
Forked from ChandanShakya/Dark.user.js
Last active May 6, 2023 13:18
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 alopatindev/d96d56437be192de74e412c48894e846 to your computer and use it in GitHub Desktop.
Save alopatindev/d96d56437be192de74e412c48894e846 to your computer and use it in GitHub Desktop.
Greasemonkey / Violentmonkey userscript for day, evening and night color schemes
// ==UserScript==
// @name Dark Reader (Unofficial)
// @icon https://darkreader.org/images/darkreader-icon-256x256.png
// @namespace DarkReader
// @description Inverts the brightness of pages to reduce eye strain
// @version 4.7.15
// @author https://github.com/darkreader/darkreader#contributors
// @homepageURL https://darkreader.org/ | https://github.com/darkreader/darkreader
// @run-at document-end
// @grant none
// @include http*
// @require https://cdn.jsdelivr.net/npm/darkreader/darkreader.min.js
// @noframes
// ==/UserScript==
const isFirefox = window.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
function update(state) {
const now = new Date().getHours();
const isMorning = now > 5 && now <= 12;
const isAfternoon = now > 12 && now <= 18;
const isEarlyEvening = now > 18 && now <= 22;
const isLateEvening = now > 22 && now <= 23;
const isNight = now > 23 || now <= 5;
const isDay = isMorning || isAfternoon;
const isDayOrEarlyEvening = isDay || isEarlyEvening;
const newState = [isDayOrEarlyEvening, isLateEvening, isNight];
if (JSON.stringify(state) != JSON.stringify(newState)) {
if (isDayOrEarlyEvening) {
console.log('day or early evening');
if (isFirefox) {
DarkReader.enable({
brightness: 100,
contrast: 100,
sepia: 0
});
} else {
DarkReader.disable();
}
} else if (isLateEvening) {
console.log('late evening');
DarkReader.enable({
brightness: 100,
contrast: 85,
sepia: 30
});
} else if (isNight) {
console.log('night');
DarkReader.enable({
brightness: 100,
contrast: 85,
sepia: 100
});
}
}
return newState;
}
function mainLoop(state, timeout) {
setTimeout(function() {
mainLoop(update(state));
}, timeout);
}
let current_host = new URL(document.URL);
if (current_host.hostname == 'localhost' && current_host.port == 7171) {
console.log('ignore this URL');
} else {
if (isFirefox) {
window.addEventListener('load', function() {
mainLoop([], 100);
}, true);
} else {
mainLoop(update([]), 60 * 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment