Skip to content

Commit a4a6e9b

Browse files
committed
优化 [暗黑模式] 开启关闭逻辑
1 parent 0ebb1d8 commit a4a6e9b

1 file changed

Lines changed: 63 additions & 37 deletions

File tree

Zhihu-Beautification.user.js

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎美化
3-
// @version 1.1.17
3+
// @version 1.1.18
44
// @author X.I.U
55
// @description 宽屏显示、暗黑模式(4种)、隐藏文章开头大图、调整图片最大高度、向下翻时自动隐藏顶栏、文章编辑页面与实际文章宽度一致、屏蔽登录提示
66
// @match *://www.zhihu.com/*
@@ -66,7 +66,7 @@
6666
if (menu_status === 1) { // 设置 Cookie
6767
if (getTheme() === 'light') document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
6868
} else {
69-
if (getTheme() === 'dark') document.cookie="theme=light; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
69+
if (getTheme() === 'dark') document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
7070
}
7171
if (menu_value('menu_darkMode')) {
7272
location.reload(); // 刷新网页
@@ -82,7 +82,7 @@
8282
GM_setValue(`${Name}`, false);
8383

8484
if (Name === 'menu_darkMode') { // 暗黑模式
85-
if (getTheme() === 'dark') document.cookie="theme=light; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
85+
if (getTheme() === 'dark') document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
8686
location.reload(); // 刷新网页
8787
} else {
8888
GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
@@ -94,7 +94,7 @@
9494
if (menu_value('menu_darkModeType') === 1) {
9595
if (getTheme() === 'light') document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
9696
} else {
97-
if (getTheme() === 'dark') document.cookie="theme=light; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
97+
if (getTheme() === 'dark') document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
9898
}
9999
location.reload(); // 刷新网页
100100
} else {
@@ -152,7 +152,7 @@ header.is-hidden {display: none;}
152152
style_5 = `/* 调整图片最大高度 */
153153
.ztext .content_image, .ztext .origin_image, .GifPlayer img {max-height: 500px;width: auto;}
154154
`,
155-
style_6 = `/* 暗黑模式(方案 1) */
155+
style_darkMode_1 = `/* 暗黑模式(方案 1) */
156156
/* 文字颜色 */
157157
html[data-theme=dark] body, html[data-theme=dark] .ContentItem-title, html[data-theme=dark] .QuestionHeader-title, html[data-theme=dark] .Tabs-link, html[data-theme=dark] .CreatorEntrance-title, html[data-theme=dark] .Search-container, html[data-theme=dark] .HotItem-excerpt, html[data-theme=dark] .PushNotifications-item, html[data-theme=dark] .Notifications-Main>header h1, html[data-theme=dark] .Notifications-Section-header h2, html[data-theme=dark] .NotificationList-Item-content, html[data-theme=dark] .Reward {color: #adbac7 !important;}
158158
/* 热榜标题 */
@@ -164,7 +164,7 @@ html[data-theme=dark] .ContentItem-title a:hover, html[data-theme=dark] .RichCon
164164
/* 背景颜色 - 网页 */
165165
html[data-theme=dark] body {background: #22272E !important;}
166166
/* 背景颜色 - 问题 */
167-
html[data-theme=dark] .AppHeader, html[data-theme=dark] .QuestionHeader, html[data-theme=dark] .QuestionHeader-footer, html[data-theme=dark] .Input-wrapper.Input-wrapper--grey, html[data-theme=dark] .EmoticonsFooter-item--selected, html[data-theme=dark] .Card, html[data-theme=dark] .ContentItem-actions, html[data-theme=dark] .MoreAnswers .List-headerText, html[data-theme=dark] .CommentsV2-withPagination, html[data-theme=dark] .Topbar, html[data-theme=dark] .CommentsV2-footer, html[data-theme=dark] .CommentEditorV2-inputWrap--active, html[data-theme=dark] .InputLike, html[data-theme=dark] .Popover-content, html[data-theme=dark] .Notifications-footer, html[data-theme=dark] .Messages-footer, html[data-theme=dark] .Modal-inner, html[data-theme=dark] .Emoticons, html[data-theme=dark] .EmoticonsFooter, html[data-theme=dark] .SearchTabs, html[data-theme=dark] .Popover-arrow:after, html[data-theme=dark] .CommentEditorV2-inputWrap, html[data-theme=dark] .ProfileHeader-wrapper, html[data-theme=dark] .UserCover, html[data-theme=dark] .AnswerForm-footer, html[data-theme=dark] .Editable-toolbar, html[data-theme=dark] .AnswerForm-fullscreenContent .Editable-toolbar, html[data-theme=dark] .KfeCollection-PcCollegeCard-wrapper, html[data-theme=dark] .KfeCollection-PcCollegeCard-root, html[data-theme=dark] .HotItem, html[data-theme=dark] .HotList, html[data-theme=dark] .HotListNavEditPad, html[data-theme=dark] .QuestionWaiting-typesTopper, html[data-theme=dark] .QuestionWaiting-types, html[data-theme=dark] .PostItem, html[data-theme=dark] .ClubSideBar section, html[data-theme=dark] .SearchSubTabs, html[data-theme=dark] .Club-SearchPosts-Content, html[data-theme=dark] .Club-content, html[data-theme=dark] .ClubJoinOrCheckinButton, html[data-theme=dark] .ClubEdit, html[data-theme=dark] .CornerButton, html[data-theme=dark] .Notifications-Section-header, html[data-theme=dark] .NotificationList, .NotificationList-Item.NotificationList-Item:after, .NotificationList-DateSplit.NotificationList-DateSplit:after, html[data-theme=dark] .Chat, .ChatUserListItem:after, .ChatListGroup-SectionTitle--bottomBorder:after, html[data-theme=dark] .ActionMenu, .ChatSideBar-Search--active, html[data-theme=dark] .ChatSideBar-Search-ResultListWrap, html[data-theme=dark] .QuestionMainDivider-inner, html[data-theme=dark] .Topic-bar, html[data-theme=dark] .AnnotationTag {background: #2D333B !important;}
167+
html[data-theme=dark] .AppHeader, html[data-theme=dark] .QuestionHeader, html[data-theme=dark] .QuestionHeader-footer, html[data-theme=dark] .Input-wrapper.Input-wrapper--grey, html[data-theme=dark] .EmoticonsFooter-item--selected, html[data-theme=dark] .Card, html[data-theme=dark] .ContentItem-actions, html[data-theme=dark] .MoreAnswers .List-headerText, html[data-theme=dark] .CommentsV2-withPagination, html[data-theme=dark] .Topbar, html[data-theme=dark] .CommentsV2-footer, html[data-theme=dark] .CommentEditorV2-inputWrap--active, html[data-theme=dark] .InputLike, html[data-theme=dark] .Popover-content, html[data-theme=dark] .Notifications-footer, html[data-theme=dark] .Messages-footer, html[data-theme=dark] .Modal-inner, html[data-theme=dark] .Emoticons, html[data-theme=dark] .EmoticonsFooter, html[data-theme=dark] .SearchTabs, html[data-theme=dark] .Popover-arrow:after, html[data-theme=dark] .CommentEditorV2-inputWrap, html[data-theme=dark] .ProfileHeader-wrapper, html[data-theme=dark] .UserCover, html[data-theme=dark] .AnswerForm-footer, html[data-theme=dark] .Editable-toolbar, html[data-theme=dark] .AnswerForm-fullscreenContent .Editable-toolbar, html[data-theme=dark] .KfeCollection-PcCollegeCard-wrapper, html[data-theme=dark] .KfeCollection-PcCollegeCard-root, html[data-theme=dark] .HotItem, html[data-theme=dark] .HotList, html[data-theme=dark] .HotListNavEditPad, html[data-theme=dark] .QuestionWaiting-typesTopper, html[data-theme=dark] .QuestionWaiting-types, html[data-theme=dark] .PostItem, html[data-theme=dark] .ClubSideBar section, html[data-theme=dark] .SearchSubTabs, html[data-theme=dark] .Club-SearchPosts-Content, html[data-theme=dark] .Club-content, html[data-theme=dark] .ClubJoinOrCheckinButton, html[data-theme=dark] .ClubEdit, html[data-theme=dark] .CornerButton, html[data-theme=dark] .Notifications-Section-header, html[data-theme=dark] .NotificationList, .NotificationList-Item.NotificationList-Item:after, .NotificationList-DateSplit.NotificationList-DateSplit:after, html[data-theme=dark] .Chat, .ChatUserListItem:after, .ChatListGroup-SectionTitle--bottomBorder:after, html[data-theme=dark] .ActionMenu, .ChatSideBar-Search--active, html[data-theme=dark] .ChatSideBar-Search-ResultListWrap, html[data-theme=dark] .QuestionMainDivider-inner, html[data-theme=dark] .Topic-bar, html[data-theme=dark] .AnnotationTag, html[data-theme=dark] .HoverCard, html[data-theme=dark] .HoverCard-loading {background: #2D333B !important;}
168168
html[data-theme=dark] .CommentListV2-header-divider, html[data-theme=dark] .CommentsV2-openComment-divider, html[data-theme=dark] .AnswerForm-fullscreenScroller, html[data-theme=dark] .HotListNav-item {background-color: #222933 !important;}
169169
html[data-theme=dark] .CornerButton:hover {background: #3f4752 !important;} /* 右下角按钮 */
170170
@@ -218,21 +218,61 @@ html[data-theme=dark] .ColumnPageHeader {background: #1c2129 !important;}
218218
.TopstoryTabs-link.is-active, html[data-theme=dark] .TopstoryTabs-link.is-active, html[data-theme=dark] .VoteButton, .Tag, html[data-theme=dark] .Tag, html[data-theme=dark] .HotListNav-item.is-active, html[data-theme=dark] .RichText a.UserLink-link {color: #3faaff !important;}
219219
/*html[data-theme=dark] .Tabs-link.is-active:after {background: #2196F3 !important;}*/
220220
html[data-theme=dark] .Reward-rewardBtn {color: #ffffff !important;}
221+
222+
/* 关闭查看回复时的高闪 */
223+
html[data-theme=dark] .CommentItemV2--highlighted {-webkit-animation: nano !important;animation: nano !important;}
221224
`,
222-
style_7 = `/* 暗黑模式(方案 2) */
225+
style_darkMode_2 = `/* 暗黑模式(方案 2) */
223226
html {filter: invert(80%) !important;}
224227
img, .ZVideoItem-video, .ZVideo-video {filter: invert(1) !important;}
225228
.GifPlayer img, .GifPlayer.isPlaying video {filter: invert(1) !important;}
226229
.GifPlayer.isPlaying img.ztext-gif.GifPlayer-gif2mp4Image {filter: none !important;}
227230
`,
228-
style_8 = `/* 暗黑模式(方案 3) */
231+
style_darkMode_3 = `/* 暗黑模式(方案 3) */
229232
html {filter: brightness(75%) !important;}
230233
`,
231-
style_9 = `/* 暗黑模式(方案 4) */
234+
style_darkMode_4 = `/* 暗黑模式(方案 4) */
232235
html {filter: brightness(75%) sepia(30%) !important;}
233236
`
234237
let style_Add = document.createElement('style');
235238

239+
// 暗黑模式
240+
if (menu_value('menu_darkMode')) {
241+
if (menu_value('menu_darkModeType') === 1) {
242+
if (getTheme() === 'light') {
243+
document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
244+
document.getElementsByTagName('html')[0].setAttribute('data-theme', 'dark');
245+
location.reload(); // 刷新网页
246+
}
247+
} else {
248+
if (getTheme() === 'dark') {
249+
document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
250+
document.getElementsByTagName('html')[0].setAttribute('data-theme', 'light');
251+
location.reload(); // 刷新网页
252+
}
253+
}
254+
switch(menu_value('menu_darkModeType')) {
255+
case 1:
256+
style += style_darkMode_1;
257+
break;
258+
case 2:
259+
style += style_darkMode_2;
260+
break;
261+
case 3:
262+
style += style_darkMode_3;
263+
break;
264+
case 4:
265+
style += style_darkMode_4;
266+
break;
267+
}
268+
} else {
269+
if (getTheme() === 'dark'){
270+
document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
271+
document.getElementsByTagName('html')[0].setAttribute('data-theme', 'light');
272+
location.reload(); // 刷新网页
273+
}
274+
}
275+
236276
// 宽屏显示
237277
if (menu_value('menu_widescreenDisplay')) {
238278
style += style_1;
@@ -253,29 +293,6 @@ html {filter: brightness(75%) sepia(30%) !important;}
253293
style += style_4;
254294
}
255295

256-
// 暗黑模式
257-
if (menu_value('menu_darkMode')) {
258-
switch(menu_value('menu_darkModeType')) {
259-
case 1:
260-
style += style_6;
261-
if (getTheme() === 'light') {
262-
document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
263-
document.getElementsByTagName('html')[0].setAttribute('data-theme', 'dark')
264-
location.reload(); // 刷新网页
265-
}
266-
break;
267-
case 2:
268-
style += style_7;
269-
break;
270-
case 3:
271-
style += style_8;
272-
break;
273-
case 4:
274-
style += style_9;
275-
break;
276-
}
277-
}
278-
279296
// 文章编辑页面与实际文章宽度一致
280297
if(window.location.href.indexOf("zhuanlan") > -1){
281298
if(window.location.href.indexOf("/edit") > -1){
@@ -284,14 +301,23 @@ html {filter: brightness(75%) sepia(30%) !important;}
284301
}
285302

286303
style_Add.innerHTML = style;
287-
document.head.appendChild(style_Add);
304+
if (document.head) {
305+
document.head.appendChild(style_Add);
306+
} else {
307+
let timer = setInterval(function(){
308+
if (document.head) {
309+
document.head.appendChild(style_Add);
310+
clearInterval(timer);
311+
}
312+
}, 1);
313+
}
288314
}
289315
function getTheme() {
290-
var name = "theme=";
291-
var ca = document.cookie.split(';');
292-
for(var i=0; i<ca.length; i++)
316+
let name = "theme=";
317+
let ca = document.cookie.split(';');
318+
for(let i=0; i<ca.length; i++)
293319
{
294-
var c = ca[i].trim();
320+
let c = ca[i].trim();
295321
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
296322
}
297323
return "light";

0 commit comments

Comments
 (0)