Night Mode

Adds a transparent dark layer depending on the time of the day to any website to make it less bright.

安装此脚本
作者推荐脚本

您可能也喜欢Redirect to Google Translator

安装此脚本
  1. // ==UserScript==
  2. // @name Night Mode
  3. // @namespace Nightmode
  4. // @version 8
  5. // @description Adds a transparent dark layer depending on the time of the day to any website to make it less bright.
  6. // @author hacker09
  7. // @include *
  8. // @require https://gf.qytechs.cn/scripts/403996-exev/code/ExEv.js?version=808391
  9. // @icon https://i.imgur.com/XxHMRlM.png
  10. // @run-at document-start
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @noframes
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. if (GM_getValue("DefaultOpacity") === undefined) { //If the variable doesn't exist yet define the variables
  19. GM_setValue('DefaultOpacity', 30); //Set the default opacity % number
  20. GM_setValue('FromHour', 21); ///From this hour on enable the dark layer
  21. GM_setValue('ToHour', 8); //Up to this hour keep the dark layer enabled
  22. } //Finishes the if condition
  23.  
  24. var timeinhours = new Date().getHours(); //Get the actual time
  25.  
  26. if (GM_getValue('FromHour') < GM_getValue('ToHour')) //If the FromHour time is smaller than the ToHour time (If from hour is morning,afternoon or evening hour)
  27. { //Starts the if condition
  28. var TimeCondition = timeinhours >= GM_getValue('FromHour') && timeinhours <= GM_getValue('ToHour'); //Creates a new variable to check and hold the time condition
  29. } //Finishes the if condition
  30. if (GM_getValue('FromHour') > GM_getValue('ToHour')) //If the FromHour time is greater than the ToHour time (If from hour is night hour)
  31. { //Starts the if condition
  32. TimeCondition = timeinhours >= GM_getValue('FromHour') || timeinhours <= GM_getValue('ToHour'); //Creates a new variable to check and hold the time condition
  33. } //Finishes the if condition
  34. if (GM_getValue('FromHour') === GM_getValue('ToHour')) //If the FromHour time is equal the ToHour time
  35. { //Starts the if condition
  36. TimeCondition = timeinhours === GM_getValue('FromHour'); //Creates a new variable to check and hold the time condition
  37. } //Finishes the if condition
  38.  
  39. if (TimeCondition === true || timeinhours === 0) //If the actual time is within the set times
  40. { //Starts the if condition
  41. document.events.on('bodyloaded', async () => { //When the body is loaded
  42. var opacity = (GM_getValue('nightSettings:' + document.location.host) || {}).opacity; //Get the website custom opacity %
  43. var DefaultOpacity = document.createElement('center'); //Creates a new element
  44. var SetCustomLayer = document.createElement('input'); //Creates a new element
  45. var MenuBox = document.createElement('container'); //Creates a new element
  46. var percentage = document.createElement('center'); //Creates a new element
  47. var closeButton = document.createElement('span'); //Creates a new element
  48. var Disable = document.createElement('center'); //Creates a new element
  49. var ShowMenu = document.createElement('div'); //Creates a new element
  50. var From = document.createElement('center'); //Creates a new element
  51. var text = document.createElement('center'); //Creates a new element
  52. var layer = document.createElement('div'); //Creates a new element
  53.  
  54. ShowMenu.setAttribute("id", "nightmenu"); //Sets an id to the ShowMenu button
  55. layer.setAttribute("id", "nightThemeLayout"); //Sets an id to the layer element
  56. text.setAttribute("style", "color: white; margin-right: 10px;"); //The CSS for the text button
  57. SetCustomLayer.setAttribute("style", "cursor: pointer;"); //The CSS for the SetCustomLayer button
  58. [From, percentage, DefaultOpacity, Disable].map(element => element.setAttribute("style", "color: white;")); //The CSS for these 3 elements
  59. closeButton.setAttribute("style", "font: -webkit-control; color: red; position: absolute; left: calc(100% - (18px + 1.75px) * 0.9); top: -2px; cursor: pointer;"); //The CSS for the closeButton button
  60. ShowMenu.setAttribute("style", "cursor: pointer; width: 2rem; height: 2rem; position: fixed; top: " + `calc(${window.innerHeight}px - 2rem)` + "; left: " + `calc(${window.innerWidth}px - 3rem)` + "; text-align: center; background: rgb(0, 0, 0); color: white; padding: 0.5rem 0px; border: 1px solid white; z-index: 2147483647;"); //The CSS for the ShowMenu button
  61. MenuBox.setAttribute("style", "position: fixed; background: rgba(0, 0, 0, 0.8); display: none; transform: translateX(calc(50vw - 50%)); left: 0px; top: 40vh; border: 1px solid white; z-index: 2147483647;"); //The CSS for the MenuBox
  62. layer.setAttribute("style", "width: 100vw; height: 100vh; z-index: 2147483647; background: rgb(0, 0, 0); opacity: " + (opacity || GM_getValue("DefaultOpacity")) + "%" + "; position: fixed; top: 0px; left: 0px; pointer-events: none;"); //The CSS for the layer button
  63.  
  64. DefaultOpacity.innerHTML = 'Default Opacity <input id="DefaultOpacity" style="width: 17px;" value="' + GM_getValue('DefaultOpacity') + '"></input>%'; //Set the default opacity % number to be displayed
  65. From.innerHTML = 'From <input id="FromHour" style="width: 17px;" value="' + GM_getValue('FromHour') + '"></input> to <input id="ToHour" style="width: 17px;" value="' + GM_getValue('ToHour') + '"></input>'; //Set the default from and to times
  66. Disable.innerHTML = 'Disable/Enable <input id="nightDisable" style="width: 17px;" type="checkbox"></input>'; //Add the disable checkbox
  67.  
  68. ShowMenu.innerText = "⚙️"; //Sets the ShowMenu symbol
  69. closeButton.innerText = "❌"; //Sets the closeButton symbol
  70. text.innerText = "Layer's opacity"; //Sets the text element text
  71. percentage.innerText = (opacity || GM_getValue('DefaultOpacity')) + "%"; //Sets the % number text to be shown
  72.  
  73. SetCustomLayer.min = "0"; //Define the minimum range number to 0
  74. SetCustomLayer.max = "95"; //Define the maximum range number to 95
  75. SetCustomLayer.type = "range"; //Let the user define the input from a range
  76. SetCustomLayer.value = (opacity || GM_getValue('DefaultOpacity')); //Set the default opacity % number to be displayed
  77.  
  78. MenuBox.append(text, SetCustomLayer, percentage, From, DefaultOpacity, closeButton, Disable); //Add the element to the page
  79. document.body.append(layer, ShowMenu, MenuBox); //Add the element to the page
  80.  
  81. [document.querySelector("#DefaultOpacity"), document.querySelector("#ToHour"), document.querySelector("#FromHour")].map(element => element.oninput = function() {
  82. GM_setValue('DefaultOpacity', parseInt(document.querySelector("#DefaultOpacity").value)); //Store the new DefaultOpacity variable %
  83. GM_setValue('FromHour', parseInt(document.querySelector("#FromHour").value)); //Store the new FromHour time variable
  84. GM_setValue('ToHour', parseInt(document.querySelector("#ToHour").value)); //Store the new ToHour time variable
  85. });
  86.  
  87. document.querySelector("#nightDisable").addEventListener('change', function() { //If the disable/enable checkbox is checked/unchecked
  88. if (this.checked) { //If the checkbox is checked
  89. document.querySelector("#nightThemeLayout").style.display = 'none'; //Hide the dark layer
  90. document.querySelector("#nightmenu").style.display = 'none'; //Hide the menu
  91. } else { //If the checkbox is unchecked
  92. document.querySelector("#nightThemeLayout").style.display = ''; //Show the dark layer
  93. document.querySelector("#nightmenu").style.display = ''; //Show the menu
  94. } //Finishes the else condition
  95. }); //FInishes the onchange event listener
  96.  
  97. document.addEventListener("fullscreenchange", function() { //When a video is on full screen
  98. if (document.fullscreen) { //If the video entered the full screen mode
  99. ShowMenu.style.display = "none"; //Hide the menu
  100. } else { //If the video lefted the full screen mode
  101. ShowMenu.style.display = "block"; //Show the menu button again
  102. } //Finishes the if condition
  103. }); //Finishes the fullscreenchange event listener
  104.  
  105. ShowMenu.onclick = function() { //When the menu button is clicked
  106. this.style.display = "none"; //Hide the menu
  107. MenuBox.style.display = ''; //Show the options box
  108. }; //FInishes the click event listener
  109.  
  110. closeButton.onclick = function() { //When the close button is clicked
  111. this.parentNode.style.display = "none"; //Hide the options box
  112. ShowMenu.style.display = "block"; //Show the menu button again
  113. }; //FInishes the click event listener
  114.  
  115. SetCustomLayer.oninput = function() { //When the user changed the opacity %
  116. GM_setValue('nightSettings:' + document.location.host, {
  117. "opacity": this.value
  118. }); //Store the new opacity % to be used on this site
  119. percentage.innerText = `${this.value}%`; //Change the displayed opacity %
  120. layer.style.opacity = this.value + "%"; //Change the website opacity
  121. }; //Finishes the oninput event listener
  122. }); //Finishes the bodyloaded event listener
  123. } //Finishes the if condition
  124. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址