11// ==UserScript==
22// @name 知乎增强
3- // @version 1.2.7
3+ // @version 1.2.8
44// @author X.I.U
5- // @description 移除登录弹窗、一键收起回答、收起当前回答(点击两侧空白处)、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
5+ // @description 移除登录弹窗、一键收起回答、收起当前回答/评论 (点击两侧空白处)、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
66// @match *://www.zhihu.com/*
77// @match *://zhuanlan.zhihu.com/*
88// @icon https://static.zhihu.com/heifetz/favicon.ico
@@ -382,28 +382,30 @@ function collapsedAnswer(){
382382 }
383383}
384384
385- // 收起当前回答(收起回答)
386- function collapsedNowAnswer_2 ( ) {
387- let rightButton = document . querySelector ( '.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom' )
388- if ( rightButton ) {
389- rightButton = rightButton . querySelector ( '.ContentItem-rightButton' )
390- if ( rightButton && rightButton . attributes [ 0 ] . name === "data-zop-retract-question" ) {
391- rightButton . click ( ) ;
392- }
393- }
394- }
395385
396- // 收起当前回答(监听点击事件)
386+ // 收起当前回答(监听点击事件,点击网页两侧空白处 )
397387function collapsedNowAnswer ( selectors ) {
398388 if ( menu_collapsedNowAnswer ) {
399389 document . querySelector ( selectors ) . onclick = function ( event ) {
400390 if ( event . target == this ) {
401- collapsedNowAnswer_2 ( ) ;
391+ let rightButton = document . querySelector ( '.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom' )
392+ if ( rightButton ) {
393+ rightButton = rightButton . querySelector ( '.ContentItem-rightButton' )
394+ if ( rightButton && rightButton . attributes [ 0 ] . name === "data-zop-retract-question" ) {
395+ rightButton . click ( ) ;
396+ }
397+ }
398+
399+ let commentCollapseButton = document . querySelector ( '.CommentCollapseButton' )
400+ if ( commentCollapseButton ) {
401+ commentCollapseButton . click ( ) ;
402+ }
402403 }
403404 }
404405 }
405406}
406407
408+
407409var postNum ;
408410// 区分问题文章
409411function addTypeTips ( ) {
@@ -438,17 +440,40 @@ function addTypeTips() {
438440 }
439441}
440442
441- // 知乎免登录,来自:https://greasyfork.org/zh-CN/scripts/417126
442- function removeLogin ( ) {
443+ // 监听 网页插入元素 事件
444+ function addEventListener_DOMNodeInserted ( ) {
445+ // 知乎免登录,来自:https://greasyfork.org/zh-CN/scripts/417126
443446 let removeLoginModal = e => {
444- if ( e . target . getElementsByClassName ( 'Modal-wrapper' ) . length > 0 ) {
447+ if ( e . target . innerHTML && e . target . getElementsByClassName ( 'Modal-wrapper' ) . length > 0 ) {
445448 if ( e . target . getElementsByClassName ( 'Modal-wrapper' ) [ 0 ] . querySelector ( '.signFlowModal' ) ) {
446449 e . target . getElementsByClassName ( 'Modal-wrapper' ) [ 0 ] . remove ( ) ;
447450 }
448451 setTimeout ( ( ) => { document . documentElement . style . overflowY = 'scroll' ; } , 0 ) ;
449452 }
450453 }
451- document . addEventListener ( 'DOMNodeInserted' , removeLoginModal ) ;
454+
455+ // 收起当前评论(监听点击事件,点击网页两侧空白处)
456+ let collapseNowComment = e => {
457+ if ( e . target . innerHTML && e . target . getElementsByClassName ( 'Modal-wrapper Modal-enter' ) . length > 0 ) {
458+ document . getElementsByClassName ( 'Modal-backdrop' ) [ 0 ] . onclick = function ( event ) {
459+ if ( event . target == this ) {
460+ let closeButton = document . getElementsByClassName ( 'Modal-closeButton' ) [ 0 ]
461+ if ( closeButton ) {
462+ closeButton . click ( ) ;
463+ }
464+ }
465+ }
466+ }
467+ }
468+
469+ if ( document . querySelector ( 'button.AppHeader-login' ) ) { // 未登录时才会监听并移除登录弹窗
470+ document . addEventListener ( 'DOMNodeInserted' , removeLoginModal ) ;
471+ document . querySelector ( 'button.AppHeader-login' ) . onclick = function ( ) { location . href = 'https://www.zhihu.com/signin' ; } // [登录]按钮跳转至登录页面
472+ document . querySelector ( '.AppHeader-profile button.Button--primary' ) . onclick = function ( ) { location . href = 'https://www.zhihu.com/signin' ; } // [加入知乎]按钮跳转至注册页面(实际上是同一个页面)
473+ } else if ( window . location . href . indexOf ( "zhuanlan" ) > - 1 ) {
474+ document . addEventListener ( 'DOMNodeInserted' , removeLoginModal ) ;
475+ }
476+ document . addEventListener ( 'DOMNodeInserted' , collapseNowComment ) ; // 收起当前评论(监听点击事件,点击网页两侧空白处)
452477}
453478
454479
@@ -473,11 +498,7 @@ function EventXMLHttpRequest() {
473498})(XMLHttpRequest.prototype.open);*/
474499
475500( function ( ) {
476- if ( document . querySelector ( 'button.AppHeader-login' ) ) { // 未登录时才会监听并移除登录弹窗
477- removeLogin ( ) ;
478- document . querySelector ( 'button.AppHeader-login' ) . onclick = function ( ) { location . href = 'https://www.zhihu.com/signin' ; } // [登录]按钮跳转至登录页面
479- document . querySelector ( '.AppHeader-profile button.Button--primary' ) . onclick = function ( ) { location . href = 'https://www.zhihu.com/signin' ; } // [加入知乎]按钮跳转至注册页面(实际上是同一个页面)
480- }
501+ addEventListener_DOMNodeInserted ( ) ; // 监听 网页插入元素 事件
481502
482503
483504 // 默认折叠邀请,来自:https://greasyfork.org/scripts/402808
@@ -533,7 +554,6 @@ function EventXMLHttpRequest() {
533554 EventXMLHttpRequest ( ) ; // 区分问题文章
534555 }
535556 } else if ( window . location . href . indexOf ( "zhuanlan" ) > - 1 ) { // 文章 //
536- removeLogin ( ) ; // 移除登录弹窗
537557 setInterval ( topTime_zhuanlan , 300 ) ; // 置顶显示时间
538558 } else if ( window . location . href . indexOf ( "column" ) > - 1 ) { // 专栏 //
539559 collapsedAnswer ( ) ; // 一键收起回答
0 commit comments