From a72f8eb4732b3e8ce3d470be9242845d532ed775 Mon Sep 17 00:00:00 2001 From: cyf Date: Wed, 22 Jul 2026 14:30:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E7=9F=A5=E4=B9=8E?= =?UTF-8?q?=E6=9A=97=E9=BB=91=E6=A8=A1=E5=BC=8F=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zhihu-Beautification.user.js | 79 ++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/Zhihu-Beautification.user.js b/Zhihu-Beautification.user.js index 584671e36..da91b184d 100644 --- a/Zhihu-Beautification.user.js +++ b/Zhihu-Beautification.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name 知乎美化 -// @version 1.5.20 +// @version 1.5.21 // @author X.I.U // @description 宽屏显示、暗黑模式(4种)、暗黑模式跟随浏览器、屏蔽首页活动广告、隐藏文章开头大图、调整图片最大高度、向下翻时自动隐藏顶栏 // @match *://www.zhihu.com/* @@ -23,6 +23,9 @@ (function() { 'use strict'; + const THEME_COOKIE_NAME = 'themeApp', + THEME_MODE_COOKIE_NAME = 'themeModeApp', + THEME_COOKIE_EXPIRES_DAYS = 400; var menu_ALL = [ ['menu_widescreenDisplay', '宽屏显示', '勾选 = 该页面开启宽屏显示(刷新后查看效果)', ''], ['menu_widescreenDisplayIndex', '首页', '宽屏显示', true], @@ -81,9 +84,9 @@ } GM_setValue(`${Name}`, menu_status); if (menu_status === 1) { // 设置 Cookie - if (getTheme() === 'light') setTheme('dark'); + if (!isManualTheme('dark')) setTheme('dark'); } else { - if (getTheme() === 'dark') { + if (!isManualTheme('light')) { setTheme('light'); } else { if (menu_value('menu_darkMode')) {location.reload();} else {registerMenuCommand();} @@ -102,7 +105,7 @@ GM_setValue(`${Name}`, false); if (Name === 'menu_darkMode') { // 暗黑模式 - if (getTheme() === 'dark') {setTheme('light');} else {location.reload();} + if (!isManualTheme('light')) {setTheme('light');} else {location.reload();} } else { GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}}); } @@ -111,9 +114,9 @@ if (Name === 'menu_darkMode') { if (menu_value('menu_darkModeType') === 1) { - if (getTheme() === 'light') setTheme('dark'); + if (!isManualTheme('dark')) setTheme('dark'); } else { - if (getTheme() === 'dark') {setTheme('light');} else {location.reload();} + if (!isManualTheme('light')) {setTheme('light');} else {location.reload();} } } else { GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}}); @@ -427,17 +430,17 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();} style_darkMode_4 = style_darkMode_4_firefox } - // 如果开启了 [暗黑模式跟随浏览器] 且 当前浏览器是暗黑模式 + // 如果开启了 [暗黑模式跟随浏览器] 且当前浏览器是亮色模式 if (menu_value('menu_darkModeAuto') && !window.matchMedia('(prefers-color-scheme: dark)').matches) { - // 如果是暗黑模式,则需要改为白天模式 - if (getTheme() === 'dark') { + // 确保知乎使用白天模式 + if (!isManualTheme('light')) { setTheme('light'); } } else { // 如果暗黑模式为 1 if (menu_value('menu_darkModeType') === 1) { - // 如果当前知乎主题为白天模式,那就是改为暗黑模式 - if (getTheme() === 'light') { + // 确保知乎使用暗黑模式 + if (!isManualTheme('dark')) { setTheme('dark'); } // 如果是问题日志页,则改为暗黑模式 @@ -446,7 +449,7 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();} style_darkMode_1 += style_darkMode_1_x; } } else { // 如果是其他暗黑模式,则需要确保为白天模式 - if (getTheme() === 'dark') { + if (!isManualTheme('light')) { setTheme('light'); } } @@ -466,7 +469,7 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();} } } } else { - if (getTheme() === 'dark'){ + if (!isManualTheme('light')){ setTheme('light'); } } @@ -487,15 +490,15 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();} // 隐藏文章开头大图 if (menu_value('menu_postimg')) style += style_2; - if (document.lastChild) { - document.lastChild.appendChild(style_Add).textContent = style; + if (document.documentElement) { + document.documentElement.appendChild(style_Add).textContent = style; } else { // 避免网站加载速度太慢的备用措施 let timer1 = setInterval(function(){ // 每 10 毫秒检查一下 html 是否已存在 - if (document.lastChild) { + if (document.documentElement) { clearInterval(timer1); // 取消定时器 - document.lastChild.appendChild(style_Add).textContent = style; + document.documentElement.appendChild(style_Add).textContent = style; } - }); + }, 10); } } @@ -521,30 +524,46 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();} }, false); } + // 获取 Cookie + function getCookie(name) { + const prefix = `${name}=`, + cookies = document.cookie.split(';'); + for (let i=0; i