Skip to content

Commit d649e17

Browse files
committed
优化 代码
1 parent 37ab90e commit d649e17

3 files changed

Lines changed: 36 additions & 96 deletions

File tree

DarkMode.user.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,9 @@
9595
}
9696

9797

98+
// 菜单数字图标
9899
function menu_num(num) {
99-
switch(num) {
100-
case 1:
101-
return '1️⃣'
102-
break;
103-
case 2:
104-
return '2️⃣'
105-
break;
106-
case 3:
107-
return '3️⃣'
108-
break;
109-
}
100+
return ['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟'][num]
110101
}
111102

112103

GithubEnhanced-High-Speed-Download.user.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,9 @@
109109
registerMenuCommand(); // 重新注册脚本菜单
110110
};
111111

112+
// 菜单数字图标
112113
function menu_num(num) {
113-
switch(num) {
114-
case 0:
115-
return '0️⃣'
116-
break;
117-
case 1:
118-
return '1️⃣'
119-
break;
120-
case 2:
121-
return '2️⃣'
122-
break;
123-
case 3:
124-
return '3️⃣'
125-
break;
126-
case 4:
127-
return '4️⃣'
128-
break;
129-
case 5:
130-
return '5️⃣'
131-
break;
132-
case 6:
133-
return '6️⃣'
134-
break;
135-
}
114+
return ['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟'][num]
136115
}
137116

138117
addRelease(); // Release 加速

Zhihu-Beautification.user.js

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@
6767
}
6868
GM_setValue(`${Name}`, menu_status);
6969
if (menu_status === 1) { // 设置 Cookie
70-
if (getTheme() === 'light') document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
70+
if (getTheme() === 'light') document.cookie='theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/';
7171
} else {
72-
if (getTheme() === 'dark') document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
72+
if (getTheme() === 'dark') document.cookie='theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
7373
}
7474
if (menu_value('menu_darkMode')) {
7575
location.reload(); // 刷新网页
@@ -79,21 +79,9 @@
7979
}
8080
};
8181

82+
// 菜单数字图标
8283
function menu_num(num) {
83-
switch(num) {
84-
case 1:
85-
return '1️⃣'
86-
break;
87-
case 2:
88-
return '2️⃣'
89-
break;
90-
case 3:
91-
return '3️⃣'
92-
break;
93-
case 4:
94-
return '4️⃣'
95-
break;
96-
}
84+
return ['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟'][num]
9785
}
9886

9987
// 菜单开关
@@ -102,7 +90,7 @@
10290
GM_setValue(`${Name}`, false);
10391

10492
if (Name === 'menu_darkMode') { // 暗黑模式
105-
if (getTheme() === 'dark') document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
93+
if (getTheme() === 'dark') document.cookie='theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
10694
location.reload(); // 刷新网页
10795
} else {
10896
GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
@@ -112,9 +100,9 @@
112100

113101
if (Name === 'menu_darkMode') {
114102
if (menu_value('menu_darkModeType') === 1) {
115-
if (getTheme() === 'light') document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
103+
if (getTheme() === 'light') document.cookie='theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/';
116104
} else {
117-
if (getTheme() === 'dark') document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
105+
if (getTheme() === 'dark') document.cookie='theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
118106
}
119107
location.reload(); // 刷新网页
120108
} else {
@@ -254,6 +242,10 @@ html[data-theme=dark] body::-webkit-scrollbar-thumb, html[data-theme="dark"] .Me
254242
html[data-theme=dark] body::-webkit-scrollbar-track {background: #22272e !important;}
255243
html[data-theme=dark] .MessagesBox::-webkit-scrollbar-track, html[data-theme="dark"] .Messages-list::-webkit-scrollbar-track, html[data-theme=dark] .PushNotifications-list::-webkit-scrollbar-track, html[data-theme=dark] .CommentListV2::-webkit-scrollbar-track, .ChatListGroup-SectionContent::-webkit-scrollbar-track, html[data-theme=dark] .ChatSideBar-Search-ResultListWrap::-webkit-scrollbar-track, html[data-theme=dark] .ChatBox textarea.Input::-webkit-scrollbar-track {background: #2d333b !important;}
256244
245+
/* 滚动条 - 回答目录 */
246+
html[data-theme=dark] .AnswerItem .RichContent-hasCatalog .RichContent-inner .Catalog.isCatalogV2::-webkit-scrollbar {width: 0 !important;}
247+
html[data-theme=dark] .AnswerItem .RichContent-hasCatalog .RichContent-inner .Catalog.isCatalogV2 > :first-child {background: #2D333B !important;}
248+
257249
html {scrollbar-width: thin; scrollbar-color: #3f4752 #22272e;}
258250
.MessagesBox, .Messages-list, .PushNotifications-list, .CommentListV2, .ChatListGroup-SectionContent, .ChatSideBar-Search-ResultListWrap {scrollbar-width: thin; scrollbar-color: #3f4752 #2D333B;}
259251
@@ -308,21 +300,24 @@ html {filter: brightness(75%) sepia(30%) !important; background-image: url();}
308300
`
309301
let style_Add = document.createElement('style');
310302

311-
// 暗黑模式
303+
// 如果开启了暗黑模式
312304
if (menu_value('menu_darkMode')) {
305+
// 如果暗黑模式为 1
313306
if (menu_value('menu_darkModeType') === 1) {
307+
// 如果当前知乎主题为白天模式,那就是改为暗黑模式
314308
if (getTheme() === 'light') {
315-
document.cookie="theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/";
309+
document.cookie='theme=dark; expires=Thu, 18 Dec 2031 12:00:00 GMT; path=/';
316310
document.lastChild.setAttribute('data-theme', 'dark');
317311
location.reload(); // 刷新网页
318312
}
313+
// 如果是问题日志页,则改为暗黑模式
319314
if (location.pathname.indexOf('/log') > -1) {
320315
document.documentElement.setAttribute('data-theme', 'dark');
321316
style_darkMode_1 += style_darkMode_1_x;
322317
}
323-
} else {
318+
} else { // 如果是其他暗黑模式,则需要确保为白天模式
324319
if (getTheme() === 'dark') {
325-
document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
320+
document.cookie='theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
326321
document.lastChild.setAttribute('data-theme', 'light');
327322
location.reload(); // 刷新网页
328323
}
@@ -348,67 +343,42 @@ html {filter: brightness(75%) sepia(30%) !important; background-image: url();}
348343
}
349344
} else {
350345
if (getTheme() === 'dark'){
351-
document.cookie="theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
346+
document.cookie='theme=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
352347
document.lastChild.setAttribute('data-theme', 'light');
353348
location.reload(); // 刷新网页
354349
}
355350
}
356351

357352
// 宽屏显示
358-
if (menu_value('menu_widescreenDisplay')) {
359-
style += style_1;
360-
}
361-
353+
if (menu_value('menu_widescreenDisplay')) style += style_1;
362354
// 调整图片最大高度
363-
if (menu_value('menu_picHeight')) {
364-
style += style_5;
365-
}
366-
355+
if (menu_value('menu_picHeight')) style += style_5;
367356
// 隐藏文章开头大图
368-
if (menu_value('menu_postimg')) {
369-
style += style_2;
370-
}
371-
357+
if (menu_value('menu_postimg')) style += style_2;
372358
// 向下翻时自动隐藏顶栏
373-
if (menu_value('menu_hideTitle')) {
374-
style += style_4;
375-
}
359+
if (menu_value('menu_hideTitle')) style += style_4;
376360

377361
// 文章编辑页面与实际文章宽度一致
378-
if(window.location.href.indexOf("zhuanlan") > -1){
379-
if(window.location.href.indexOf("/edit") > -1){
380-
style += style_3;
381-
}
382-
}
383-
384-
/*style_Add.innerHTML = style;
385-
if (document.head) {
386-
document.head.appendChild(style_Add);
387-
} else {
388-
let timer = setInterval(function(){
389-
if (document.head) {
390-
document.head.appendChild(style_Add);
391-
clearInterval(timer);
392-
}
393-
}, 1);
394-
}*/
362+
if (window.location.href.indexOf('zhuanlan') > -1 && window.location.href.indexOf('/edit') > -1) style += style_3;
395363

396364
if (document.lastChild) {
397365
document.lastChild.appendChild(style_Add).textContent = style;
398366
} else { // 避免网站加载速度太慢的备用措施
399-
let timer1 = setInterval(function(){ // 每 5 毫秒检查一下 html 是否已存在
367+
let timer1 = setInterval(function(){ // 每 10 毫秒检查一下 html 是否已存在
400368
if (document.lastChild) {
401369
clearInterval(timer1); // 取消定时器
402370
document.lastChild.appendChild(style_Add).textContent = style;
403371
}
404-
}, 5);
372+
}, 10);
405373
}
406374
}
375+
376+
377+
// 获取知乎 Cookie 中的主题类型
407378
function getTheme() {
408-
let name = "theme=";
409-
let ca = document.cookie.split(';');
410-
for(let i=0; i<ca.length; i++)
411-
{
379+
let name = "theme=",
380+
ca = document.cookie.split(';');
381+
for (let i=0; i<ca.length; i++) {
412382
let c = ca[i].trim();
413383
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
414384
}

0 commit comments

Comments
 (0)