From cd903e4d554d8d267a423b1f4d1495466c0fe4a3 Mon Sep 17 00:00:00 2001 From: xiu2 <54703944+XIU2@users.noreply.github.com> Date: Thu, 13 May 2021 08:59:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20[=E6=9A=97=E9=BB=91?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F]=20=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DarkMode.user.js | 171 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 172 insertions(+) create mode 100644 DarkMode.user.js diff --git a/DarkMode.user.js b/DarkMode.user.js new file mode 100644 index 000000000..a024f84b0 --- /dev/null +++ b/DarkMode.user.js @@ -0,0 +1,171 @@ +// ==UserScript== +// @name 暗黑模式 +// @version 1.0.0 +// @author X.I.U +// @description 最简单的全网通用暗黑模式 +// @match *://*/* +// @icon https://i.loli.net/2021/03/07/rdijeYm83pznxWq.png +// @grant GM_registerMenuCommand +// @grant GM_unregisterMenuCommand +// @grant GM_getValue +// @grant GM_setValue +// @noframes +// @license GPL-3.0 License +// @run-at document-start +// @namespace https://github.com/XIU2/UserScript +// ==/UserScript== + +(function() { + var menu_ALL = [ + ['menu_runDuringTheDay', '白天保持开启 (比晚上更亮一点)', '白天保持开启', true], + ['menu_darkModeType', '点击切换模式', '点击切换模式', 1] + ], menu_ID = []; + for (let i=0;i menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单 + for (let i=0;i 3){ // 避免在减少 raw 数组后,用户储存的数据大于数组而报错 + menu_ALL[i][3] = 1; + GM_setValue('menu_darkModeType', menu_ALL[i][3]); + } + menu_ID[i] = GM_registerMenuCommand(`🔄 [ ${menu_ALL[i][3]} ] ${menu_ALL[i][1]}`, function(){menu_toggle(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`)}); + } else { + menu_ID[i] = GM_registerMenuCommand(`🌝 [ ${menu_ALL[i][3]?'√':'×'} ] ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)}); + } + } + menu_ID[menu_ID.length] = GM_registerMenuCommand('💬 反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/412212/feedback', {active: true,insert: true,setParent: true});}); + } + + + // 切换暗黑模式 + function menu_toggle(menu_status, Name) { + menu_status = parseInt(menu_status) + if (menu_status >= 3){ + menu_status = 1; + } else { + menu_status += 1; + } + GM_setValue(`${Name}`, menu_status); + location.reload(); // 刷新网页 + }; + + + // 菜单开关 + function menu_switch(menu_status, Name, Tips) { + if (menu_status == 'true'){ + GM_setValue(`${Name}`, false); + GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500}); + }else{ + GM_setValue(`${Name}`, true); + GM_notification({text: `已开启 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500}); + } + registerMenuCommand(); // 重新注册脚本菜单 + }; + + + // 返回菜单值 + function menu_value(menuName) { + for (let menu of menu_ALL) { + if (menu[0] == menuName) { + return menu[3] + } + } + } + + + // 添加样式 + function addStyle() { + let grayLevel,rgbValueArry, + style_Add = document.createElement('style'), + hours = new Date().getHours(), + style = ``, + style_00 = `body {background-color: #ffffff !important;}`, + style_11 = `html {filter: brightness(80%) !important;}`, + style_11_firefox = `html {filter: brightness(80%) !important; background-image: url();}`, + style_12 = `html {filter: brightness(70%) !important;}`, + style_12_firefox = `html {filter: brightness(70%) !important; background-image: url();}`, + style_21 = `html {filter: brightness(85%) sepia(20%) !important;}`, + style_21_firefox = `html {filter: brightness(85%) sepia(20%) !important; background-image: url();}`, + style_22 = `html {filter: brightness(70%) sepia(30%) !important;}`, + style_22_firefox = `html {filter: brightness(70%) sepia(30%) !important; background-image: url();}`, + style_31 = `html {filter: invert(80%) !important;} img, video {filter: invert(1) !important;}`, + style_31_firefox = `html {filter: invert(80%) !important;} img, video {filter: invert(1) !important; background-image: url();}`; + + // 判断网页是否没有设置背景颜色(没有背景颜色会导致滤镜对背景颜色无效) + if (document.body) { + rgbValueArry = window.getComputedStyle(document.body).backgroundColor.replace ('rgb(', '').replace ('rgba(', '').replace (')', '').split (', '); + grayLevel = rgbValueArry [0] + rgbValueArry [1] + rgbValueArry [2]; + if (grayLevel === "000") style += style_00 + } + + // Firefox 浏览器需要特殊对待 + if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { + style_11 = style_11_firefox + style_12 = style_12_firefox + style_21 = style_21_firefox + style_22 = style_22_firefox + style_31 = style_31_firefox + } + + // 白天(7点到19点) + if (hours > 6 || hours < 19) { + if (menu_value('menu_runDuringTheDay')) { + style_12 = style_11 + style_22 = style_21 + } else { + style_12 = style_22 = '' + } + } + + switch(menu_value('menu_darkModeType')) { + case 1: + style += style_12; + break; + case 2: + style += style_22; + break; + case 3: + style += style_31; + break; + } + style_Add.innerHTML = style; + if (document.head) { + document.head.appendChild(style_Add); + } else { // 为了避免脚本运行的时候 head 还没加载导致报错 + let timer = setInterval(function(){ + if (document.head) { + document.head.appendChild(style_Add); + clearInterval(timer); + } + }, 1); + } + + // 为了避免 body 还没加载导致无法检查是否设置背景颜色的备用措施 + if (!grayLevel) { + let timer2 = setInterval(function(){ + if (document.body) { + let rgbValueArry = window.getComputedStyle(document.body).backgroundColor.replace ('rgb(', '').replace ('rgba(', '').replace (')', '').split (', '), + style_Add1 = document.createElement('style'); + if (rgbValueArry [0] + rgbValueArry [1] + rgbValueArry [2] === "000") { + style_Add1.innerHTML = 'body {background-color: #ffffff !important;}'; + document.head.appendChild(style_Add1); + } + clearInterval(timer2); + } + }, 1); + } + } +})(); \ No newline at end of file diff --git a/README.md b/README.md index d8f052edb..84e93921b 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ | | 脚本名称 | 脚本功能 | 安装 \| 备用 | | :----: | :---- | :---- | :----: | +| [](https://github.com/XIU2) | **暗黑模式** | 最简单的全网通用暗黑模式 | **[安装](https://greasyfork.org/zh-CN/scripts/426377)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/DarkMode.user.js)** | | [](https://www.zhihu.com/people/xiu2) | **知乎 美化** | 宽屏显示、**暗黑模式**、隐藏文章开头大图、调整图片最大高度... | **[安装](https://greasyfork.org/zh-CN/scripts/412212)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Zhihu-Beautification.user.js)** | | [](https://www.zhihu.com/people/xiu2) | **知乎 增强** | **移除登录弹窗**、一键收起回答、屏蔽指定用户、屏蔽盐选内容... | **[安装](https://greasyfork.org/zh-CN/scripts/419081)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Zhihu-Enhanced.user.js)** | | [](https://www.v2ex.com/) | **V2ex 增强** | **自动签到**、链接转图片、自动无缝翻页、新标签页打开链接... | **[安装](https://greasyfork.org/zh-CN/scripts/424246)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/V2ex-Enhanced.user.js)** |