From 1c4740aa5d41578578f20c0bbf4e438e329bbf62 Mon Sep 17 00:00:00 2001 From: xiu2 <54703944+XIU2@users.noreply.github.com> Date: Sat, 27 Mar 2021 13:00:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20[=E6=94=B6=E8=B5=B7?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E8=AF=84=E8=AE=BA]=20=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=88=E7=82=B9=E5=87=BB=E7=BD=91=E9=A1=B5=E4=B8=A4=E4=BE=A7?= =?UTF-8?q?=E7=A9=BA=E7=99=BD=E5=A4=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zhihu-Enhanced.user.js | 68 +++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 93815b9..295d6c4 100644 --- a/Zhihu-Enhanced.user.js +++ b/Zhihu-Enhanced.user.js @@ -1,8 +1,8 @@ // ==UserScript== // @name 知乎增强 -// @version 1.2.7 +// @version 1.2.8 // @author X.I.U -// @description 移除登录弹窗、一键收起回答、收起当前回答(点击两侧空白处)、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链 +// @description 移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链 // @match *://www.zhihu.com/* // @match *://zhuanlan.zhihu.com/* // @icon https://static.zhihu.com/heifetz/favicon.ico @@ -382,28 +382,30 @@ function collapsedAnswer(){ } } -// 收起当前回答(收起回答) -function collapsedNowAnswer_2(){ - let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom') - if(rightButton) { - rightButton = rightButton.querySelector('.ContentItem-rightButton') - if(rightButton && rightButton.attributes[0].name === "data-zop-retract-question") { - rightButton.click(); - } - } -} -// 收起当前回答(监听点击事件) +// 收起当前回答(监听点击事件,点击网页两侧空白处) function collapsedNowAnswer(selectors){ if(menu_collapsedNowAnswer){ document.querySelector(selectors).onclick = function(event){ if (event.target==this) { - collapsedNowAnswer_2(); + let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom') + if(rightButton) { + rightButton = rightButton.querySelector('.ContentItem-rightButton') + if(rightButton && rightButton.attributes[0].name === "data-zop-retract-question") { + rightButton.click(); + } + } + + let commentCollapseButton = document.querySelector('.CommentCollapseButton') + if(commentCollapseButton) { + commentCollapseButton.click(); + } } } } } + var postNum; // 区分问题文章 function addTypeTips() { @@ -438,17 +440,40 @@ function addTypeTips() { } } -// 知乎免登录,来自:https://greasyfork.org/zh-CN/scripts/417126 -function removeLogin() { +// 监听 网页插入元素 事件 +function addEventListener_DOMNodeInserted() { + // 知乎免登录,来自:https://greasyfork.org/zh-CN/scripts/417126 let removeLoginModal = e => { - if (e.target.getElementsByClassName('Modal-wrapper').length > 0) { + if (e.target.innerHTML && e.target.getElementsByClassName('Modal-wrapper').length > 0) { if (e.target.getElementsByClassName('Modal-wrapper')[0].querySelector('.signFlowModal')){ e.target.getElementsByClassName('Modal-wrapper')[0].remove(); } setTimeout(() => {document.documentElement.style.overflowY = 'scroll';}, 0); } } - document.addEventListener('DOMNodeInserted', removeLoginModal); + + // 收起当前评论(监听点击事件,点击网页两侧空白处) + let collapseNowComment = e => { + if (e.target.innerHTML && e.target.getElementsByClassName('Modal-wrapper Modal-enter').length > 0) { + document.getElementsByClassName('Modal-backdrop')[0].onclick = function(event){ + if (event.target==this) { + let closeButton = document.getElementsByClassName('Modal-closeButton')[0] + if(closeButton) { + closeButton.click(); + } + } + } + } + } + + if (document.querySelector('button.AppHeader-login')){ // 未登录时才会监听并移除登录弹窗 + document.addEventListener('DOMNodeInserted', removeLoginModal); + document.querySelector('button.AppHeader-login').onclick=function(){location.href='https://www.zhihu.com/signin';} // [登录]按钮跳转至登录页面 + document.querySelector('.AppHeader-profile button.Button--primary').onclick=function(){location.href='https://www.zhihu.com/signin';} // [加入知乎]按钮跳转至注册页面(实际上是同一个页面) + } else if(window.location.href.indexOf("zhuanlan") > -1){ + document.addEventListener('DOMNodeInserted', removeLoginModal); + } + document.addEventListener('DOMNodeInserted', collapseNowComment); // 收起当前评论(监听点击事件,点击网页两侧空白处) } @@ -473,11 +498,7 @@ function EventXMLHttpRequest() { })(XMLHttpRequest.prototype.open);*/ (function() { - if (document.querySelector('button.AppHeader-login')){ // 未登录时才会监听并移除登录弹窗 - removeLogin(); - document.querySelector('button.AppHeader-login').onclick=function(){location.href='https://www.zhihu.com/signin';} // [登录]按钮跳转至登录页面 - document.querySelector('.AppHeader-profile button.Button--primary').onclick=function(){location.href='https://www.zhihu.com/signin';} // [加入知乎]按钮跳转至注册页面(实际上是同一个页面) - } + addEventListener_DOMNodeInserted(); // 监听 网页插入元素 事件 // 默认折叠邀请,来自:https://greasyfork.org/scripts/402808 @@ -533,7 +554,6 @@ function EventXMLHttpRequest() { EventXMLHttpRequest(); // 区分问题文章 } }else if(window.location.href.indexOf("zhuanlan") > -1){ // 文章 // - removeLogin(); // 移除登录弹窗 setInterval(topTime_zhuanlan, 300); // 置顶显示时间 }else if(window.location.href.indexOf("column") > -1){ // 专栏 // collapsedAnswer(); // 一键收起回答