Use javascript to add night mode to the page
- 2020-03-30 01:28:41
- OfStack
HTML + CSS:
<div class="cover"></div>
<style>
.cover{
position:fixed;
top: 0px;
left: 0px;
outline:5000px solid rgba(0, 0, 0, 0.3);
z-index: 99999;
}
</style>
Then write a night mode plus in JavaScript:
<script>
var brightness;
//According to mask
function cover(brightness) {
if (typeof(div) == 'undefined') {
div = document.createElement('div');
div.setAttribute('style', 'position:fixed;top:0;left:0;outline:5000px solid;z-index:99999;');
document.body.appendChild(div);
} else {
div.style.display = '';
}
div.style.outlineColor = 'rgba(0,0,0,' + brightness + ')';
}
//Event listeners
window.addEventListener('keydown', function(e) {
if (e.altKey && e.keyCode == 90) { //Alt+Z: turn on night mode
cover(brightness = 0.3);
}
if (e.altKey && e.keyCode == 88) { //Alt + X: closed
cover(brightness = 0);
}
if (e.altKey && e.keyCode == 38) { //Alt+ write : increases brightness
if (brightness - 0.05 > 0.05) cover(brightness -= 0.05);
}
if (e.altKey && e.keyCode == 40) { //Alt+ left : reduce brightness
if (brightness + 0.05 < 0.95) cover(brightness += 0.05);
}
}, false);
</script>
You can also write a GreaseMonkey script that adds night mode to any page as a browser extension