11// ==UserScript==
22// @name 知乎美化
3- // @version 1.5.21
3+ // @version 1.5.22
44// @author X.I.U
55// @description 宽屏显示、暗黑模式(4种)、暗黑模式跟随浏览器、屏蔽首页活动广告、隐藏文章开头大图、调整图片最大高度、向下翻时自动隐藏顶栏
66// @match *://www.zhihu.com/*
1313// @grant GM_getValue
1414// @grant GM_setValue
1515// @grant GM_notification
16+ // @grant window.onurlchange
1617// @sandbox JavaScript
1718// @license GPL-3.0 License
1819// @run -at document-start
4344 if ( GM_getValue ( menu_ALL [ i ] [ 0 ] ) == null ) { GM_setValue ( menu_ALL [ i ] [ 0 ] , menu_ALL [ i ] [ 3 ] ) } ;
4445 }
4546 registerMenuCommand ( ) ;
47+ if ( window . onurlchange === undefined ) { addUrlChangeEvent ( ) ; } // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
4648 addStyle ( ) ;
49+ window . addEventListener ( 'urlchange' , function ( ) { // 网页 URL 动态变化后再次执行(针对在任何页面搜索时)
50+ if ( menu_value ( 'menu_widescreenDisplaySearch' ) && ( location . pathname === '/search' || location . pathname . indexOf ( '/club/' ) > - 1 || location . pathname . indexOf ( '/topic/' ) > - 1 ) ) { addStyle ( ) } ;
51+ } )
4752 // 向下翻时自动隐藏顶栏
4853 if ( menu_value ( 'menu_hideTitle' ) ) setTimeout ( hideTitle , 2000 ) ;
4954
@@ -217,12 +222,12 @@ html[data-theme=light] .AppHeader-notifications:not([aria-label=通知])>div:fir
217222@media only screen and (max-width: ${ Number ( GM_getValue ( 'menu_widescreenDisplayWidth' ) ) + 50 } px) {.Topstory-container {width: 97% !important;}}
218223` ,
219224 style_widescreenDisplayQuestion = `/* 宽屏显示 - 问题页 */
220- .ListShortcut, . QuestionWaiting-mainColumn {width: inherit !important;}
225+ .QuestionWaiting-mainColumn {width: inherit !important;}
221226.Question-mainColumn+div,[data-za-detail-view-path-module="RightSideBar"], .Question-sideColumn, .GlobalSideBar {display: none !important;}
222227.QuestionWaiting-mainColumn {margin-right: 0 !important;}
223- .Question-mainColumn {width: ${ GM_getValue ( 'menu_widescreenDisplayWidth' ) } px; margin: auto!important;}
224- @media only screen and (max-width: ${ Number ( GM_getValue ( 'menu_widescreenDisplayWidth' ) ) + 50 } px) {.Question-mainColumn {width: auto !important;}}
225- @media only screen and (max-width: ${ GM_getValue ( 'menu_widescreenDisplayWidth' ) - 100 } px) {.Question-mainColumn {width: 98.5% !important;}}
228+ .Question-mainColumn, .ListShortcut {width: ${ GM_getValue ( 'menu_widescreenDisplayWidth' ) } px; margin: auto!important;}
229+ @media only screen and (max-width: ${ Number ( GM_getValue ( 'menu_widescreenDisplayWidth' ) ) + 50 } px) {.Question-mainColumn, .ListShortcut {width: auto !important;}}
230+ @media only screen and (max-width: ${ GM_getValue ( 'menu_widescreenDisplayWidth' ) - 100 } px) {.Question-mainColumn, .ListShortcut {width: 98.5% !important;}}
226231.AuthorInfo {max-width: 100% !important;}
227232` ,
228233 style_widescreenDisplaySearch = `/* 宽屏显示 - 搜索页 */
@@ -477,7 +482,7 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();}
477482 // 宽屏显示
478483 if ( menu_value ( 'menu_widescreenDisplayIndex' ) ) style += style_widescreenDisplayIndex ;
479484 if ( menu_value ( 'menu_widescreenDisplayQuestion' ) && location . pathname . indexOf ( '/question/' ) > - 1 ) style += style_widescreenDisplayQuestion ;
480- if ( menu_value ( 'menu_widescreenDisplaySearch' ) ) style += style_widescreenDisplaySearch ;
485+ if ( menu_value ( 'menu_widescreenDisplaySearch' ) && ( location . pathname === '/search' || location . pathname . indexOf ( '/club/' ) > - 1 || location . pathname . indexOf ( '/topic/' ) > - 1 ) ) style += style_widescreenDisplaySearch ;
481486 if ( menu_value ( 'menu_widescreenDisplayCollection' ) && location . pathname . indexOf ( '/collection/' ) > - 1 ) style += style_widescreenDisplayCollection ;
482487 if ( menu_value ( 'menu_widescreenDisplayPost' ) && location . hostname . indexOf ( 'zhuanlan' ) > - 1 && ( location . pathname . indexOf ( '/edit' ) === - 1 || location . pathname . indexOf ( '/write' ) === - 1 ) ) style += style_widescreenDisplayPost ;
483488 if ( menu_value ( 'menu_widescreenDisplayPeople' ) && location . pathname . indexOf ( '/people/' ) > - 1 ) style += style_widescreenDisplayPeople ;
@@ -547,4 +552,26 @@ html {filter: brightness(65%) sepia(30%) !important; background-image: url();}
547552 break ;
548553 }
549554 }
555+
556+
557+ // 自定义 urlchange 事件(用来监听 URL 变化)
558+ function addUrlChangeEvent ( ) {
559+ history . pushState = ( f => function pushState ( ) {
560+ var ret = f . apply ( this , arguments ) ;
561+ window . dispatchEvent ( new Event ( 'pushstate' ) ) ;
562+ window . dispatchEvent ( new Event ( 'urlchange' ) ) ;
563+ return ret ;
564+ } ) ( history . pushState ) ;
565+
566+ history . replaceState = ( f => function replaceState ( ) {
567+ var ret = f . apply ( this , arguments ) ;
568+ window . dispatchEvent ( new Event ( 'replacestate' ) ) ;
569+ window . dispatchEvent ( new Event ( 'urlchange' ) ) ;
570+ return ret ;
571+ } ) ( history . replaceState ) ;
572+
573+ window . addEventListener ( 'popstate' , ( ) => {
574+ window . dispatchEvent ( new Event ( 'urlchange' ) )
575+ } ) ;
576+ }
550577} ) ( ) ;
0 commit comments