1414// @grant GM_setValue
1515// @grant GM_notification
1616// @grant GM_info
17+ // @grant window.onurlchange
1718// @license GPL-3.0 License
1819// @run -at document-end
1920// @incompatible safari
@@ -169,7 +170,7 @@ function getCollapsedAnswerObserver() {
169170 }
170171 }
171172
172- window . addEventListener ( 'locationchange ' , function ( ) {
173+ window . addEventListener ( 'urlchange ' , function ( ) {
173174 observer [ location . href . indexOf ( '/answer/' ) === - 1 ? 'start' : 'end' ] ( ) ;
174175 } )
175176 window . _collapsedAnswerObserver = observer ;
@@ -222,7 +223,7 @@ function collapsedAnswer() {
222223 const observer = getCollapsedAnswerObserver ( ) ;
223224 observer . start ( ) ;
224225 if ( ! menu_value ( 'menu_defaultCollapsedAnswer' ) && ! observer . _disconnectListener ) {
225- window . addEventListener ( 'locationchange ' , function ( ) {
226+ window . addEventListener ( 'urlchange ' , function ( ) {
226227 observer . end ( ) ;
227228 window . _collapsedAnswerObserver = null ;
228229 } )
@@ -435,7 +436,7 @@ function blockUsers(type) {
435436 }
436437
437438 blockKeywords_now ( ) ;
438- window . addEventListener ( 'locationchange ' , function ( ) {
439+ window . addEventListener ( 'urlchange ' , function ( ) {
439440 setTimeout ( blockKeywords_now , 500 ) ; // 网页 URL 变化后再次执行
440441 } )
441442
@@ -543,7 +544,7 @@ function blockUsers(type) {
543544 }
544545
545546 setTimeout ( blockUsers_now , 500 ) ;
546- window . addEventListener ( 'locationchange ' , function ( ) {
547+ window . addEventListener ( 'urlchange ' , function ( ) {
547548 setTimeout ( blockUsers_now , 500 ) ; // 网页 URL 变化后再次执行
548549 } )
549550
@@ -753,7 +754,7 @@ function blockKeywords(type) {
753754 }
754755
755756 blockKeywords_now ( ) ;
756- window . addEventListener ( 'locationchange ' , function ( ) {
757+ window . addEventListener ( 'urlchange ' , function ( ) {
757758 setTimeout ( blockKeywords_now , 500 ) ; // 网页 URL 变化后再次执行
758759 } )
759760
@@ -778,7 +779,7 @@ function blockKeywords(type) {
778779 }
779780
780781 setTimeout ( blockKeywords_now , 500 ) ;
781- window . addEventListener ( 'locationchange ' , function ( ) {
782+ window . addEventListener ( 'urlchange ' , function ( ) {
782783 setTimeout ( blockKeywords_now , 500 ) ; // 网页 URL 变化后再次执行
783784 } )
784785
@@ -882,7 +883,7 @@ function blockType(type) {
882883 } ) ;
883884 observer . observe ( document , { childList : true , subtree : true } ) ;
884885
885- window . addEventListener ( 'locationchange ' , function ( ) {
886+ window . addEventListener ( 'urlchange ' , function ( ) {
886887 addSetInterval_ ( name ) ;
887888 // 移除相关搜索
888889 if ( menu_value ( 'menu_blockTypeSearch' ) && location . pathname === '/search' && location . search . indexOf ( 'type=content' ) > - 1 ) setTimeout ( function ( ) { document . querySelector ( '.RelevantQuery' ) . parentElement . parentElement . hidden = true ; } , 1500 )
@@ -1056,7 +1057,7 @@ function addToQuestion() {
10561057 } ) ;
10571058 observer . observe ( document , { childList : true , subtree : true } ) ;
10581059
1059- window . addEventListener ( 'locationchange ' , function ( ) {
1060+ window . addEventListener ( 'urlchange ' , function ( ) {
10601061 addSetInterval_ ( 'h2.ContentItem-title a:not(.zhihu_e_tips)' ) ;
10611062 } )
10621063
@@ -1193,24 +1194,24 @@ function closeFloatingComments() {
11931194}*/
11941195
11951196
1196- // 自定义 locationchange 事件(用来监听 URL 变化)
1197- function addLocationchange ( ) {
1197+ // 自定义 urlchange 事件(用来监听 URL 变化)
1198+ function addUrlChangeEvent ( ) {
11981199 history . pushState = ( f => function pushState ( ) {
11991200 var ret = f . apply ( this , arguments ) ;
12001201 window . dispatchEvent ( new Event ( 'pushstate' ) ) ;
1201- window . dispatchEvent ( new Event ( 'locationchange ' ) ) ;
1202+ window . dispatchEvent ( new Event ( 'urlchange ' ) ) ;
12021203 return ret ;
12031204 } ) ( history . pushState ) ;
12041205
12051206 history . replaceState = ( f => function replaceState ( ) {
12061207 var ret = f . apply ( this , arguments ) ;
12071208 window . dispatchEvent ( new Event ( 'replacestate' ) ) ;
1208- window . dispatchEvent ( new Event ( 'locationchange ' ) ) ;
1209+ window . dispatchEvent ( new Event ( 'urlchange ' ) ) ;
12091210 return ret ;
12101211 } ) ( history . replaceState ) ;
12111212
12121213 window . addEventListener ( 'popstate' , ( ) => {
1213- window . dispatchEvent ( new Event ( 'locationchange ' ) )
1214+ window . dispatchEvent ( new Event ( 'urlchange ' ) )
12141215 } ) ;
12151216}
12161217
@@ -1331,11 +1332,11 @@ function questionInvitation(){
13311332
13321333
13331334( function ( ) {
1334- addLocationchange ( ) ;
1335+ if ( window . onurlchange === undefined ) { addUrlChangeEvent ( ) ; } // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
13351336 removeLogin ( ) ; // 移除登录弹窗
13361337 setInterval ( originalPic , 100 ) ; // 默认高清原图
13371338 setInterval ( directLink , 100 ) ; // 默认站外直链
1338- window . addEventListener ( 'locationchange ' , function ( ) { // 针对的是从单个回答页跳转到完整回答页时
1339+ window . addEventListener ( 'urlchange ' , function ( ) { // 针对的是从单个回答页跳转到完整回答页时
13391340 if ( location . pathname . indexOf ( 'question' ) > - 1 && location . pathname . indexOf ( 'waiting' ) === - 1 && location . pathname . indexOf ( '/answer/' ) === - 1 ) { // 回答页 //
13401341 setTimeout ( function ( ) {
13411342 collapsedNowAnswer ( '.QuestionPage' ) ; // 收起当前回答 + 快捷返回顶部
0 commit comments