From 97c4467e0dfc4df34aab9c66ca23ad771b13824b Mon Sep 17 00:00:00 2001 From: xiu2 <54703944+XIU2@users.noreply.github.com> Date: Mon, 9 Aug 2021 10:03:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20[=E6=94=B6=E8=B5=B7?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E5=9B=9E=E7=AD=94/=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=EF=BC=88=E7=82=B9=E5=87=BB=E4=B8=A4=E4=BE=A7=E7=A9=BA=E7=99=BD?= =?UTF-8?q?=E5=A4=84=EF=BC=89]=20=E5=9C=A8=E9=83=A8=E5=88=86=E5=9B=9E?= =?UTF-8?q?=E7=AD=94=E4=B8=8B=E4=B8=8D=E8=B5=B7=E4=BD=9C=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- Zhihu-Enhanced.user.js | 50 +++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a4968be..11dc1b3 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ | [](https://github.com/XIU2) | **护眼模式** | 简单有效的全网通用护眼模式、夜间模式、暗黑模式~ | **[安装](https://greasyfork.org/zh-CN/scripts/426377)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/DarkMode.user.js)** | | [](https://www.zhihu.com/people/xiu2) | **知乎 美化** | 宽屏显示、**暗黑模式**、隐藏文章开头大图、调整图片最大高... | **[安装](https://greasyfork.org/zh-CN/scripts/412212)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Zhihu-Beautification.user.js)** | | [](https://www.zhihu.com/people/xiu2) | **知乎 增强** | **移除登录弹窗**、一键收起回答、屏蔽用户、屏蔽关键词、快... | **[安装](https://greasyfork.org/zh-CN/scripts/419081)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Zhihu-Enhanced.user.js)** | -| [](https://www.v2ex.com/) | **V2ex 增强** | **自动签到**、链接转图片、自动无缝翻页、新标签页打开链接... | **[安装](https://greasyfork.org/zh-CN/scripts/424246)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/V2ex-Enhanced.user.js)** | +| [](https://www.v2ex.com/) | **V2EX 增强** | **自动签到**、链接转图片、自动无缝翻页、新标签页打开链接... | **[安装](https://greasyfork.org/zh-CN/scripts/424246)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/V2ex-Enhanced.user.js)** | | [](https://github.com/XIU2) | **Github 增强** | **高速下载** Git Clone/SSH、Release、Raw、Code(ZIP) 文件... | **[安装](https://greasyfork.org/zh-CN/scripts/412245)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/GithubEnhanced-High-Speed-Download.user.js)** | | [](https://github.com/XIU2) | **自动无缝翻页 \*** | **所有 Discuz! / Flarum 论坛**、百度、豆瓣、微博、千图网... | **[安装](https://greasyfork.org/zh-CN/scripts/419215)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Autopage.user.js)** | | [](https://bbs.3dmgame.com) | **3DM论坛 美化** | 精简多余内容、样式优化 | **[安装](https://greasyfork.org/zh-CN/scripts/413593)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/3dm-Beautification.user.js)** | diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 4cecee9..0f8d511 100644 --- a/Zhihu-Enhanced.user.js +++ b/Zhihu-Enhanced.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name 知乎增强 -// @version 1.5.2 +// @version 1.5.3 // @author X.I.U // @description 移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链 // @match *://www.zhihu.com/* @@ -111,24 +111,38 @@ function collapsedNowAnswer(selectors) { if (event.target==this) { // 下面这段主要是 [收起回答],顺便 [收起评论](如果展开了的话) let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom') - // 悬浮在底部的 [收起回答](此时正在浏览回答内容 [头部区域 + 中间区域]) + // 悬浮在底部的 [收起回答](此时正在浏览回答内容 [中间区域]) if (rightButton) { // 固定的 [收起评论](先看看是否展开评论) let commentCollapseButton = rightButton.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type') if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click(); // 再去收起回答 - rightButton = rightButton.querySelector('.ContentItem-rightButton') - if (rightButton && rightButton.hasAttribute('data-zop-retract-question')) rightButton.click(); - // 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域]) + rightButton = rightButton.querySelector('.ContentItem-rightButton[data-zop-retract-question]') + if (rightButton) rightButton.click(); + // 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域]) } else { - for (let el of document.querySelectorAll('.ContentItem-rightButton')) { // 遍历所有回答底部的 [收起] 按钮 - if (el.hasAttribute('data-zop-retract-question')) { - if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内 + let answerCollapseButton_ = false; + for (let el of document.querySelectorAll('.ContentItem-rightButton[data-zop-retract-question]')) { // 遍历所有回答底部的 [收起] 按钮 + if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内 + // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮) + let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type') + // 如果展开了评论,就收起评论 + if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click(); + el.click() // 再去收起回答 + answerCollapseButton_ = true; // 如果找到并点击收起了,就没必要执行下面的代码了(可视区域中没有 [收起回答] 时) + break + } + } + // 针对完全看不到 [收起回答] 按钮时(如 [头部区域],以及部分明明很长却不显示悬浮横条的回答) + if (!answerCollapseButton_) { + for (let el of document.querySelectorAll('.List-item')) { // 遍历所有回答主体元素 + if (isElementInViewport_(el)) { // 判断该回答是否在可视区域内 // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮) let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type') // 如果展开了评论,就收起评论 if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click(); - el.click() // 再去收起回答 + let answerCollapseButton__ = document.querySelector('.ContentItem-rightButton[data-zop-retract-question]'); + if (answerCollapseButton__) answerCollapseButton__.click() // 再去收起回答 break } } @@ -136,7 +150,7 @@ function collapsedNowAnswer(selectors) { } // 下面这段只针对 [收起评论](如果展开了的话) - var commentCollapseButton_ = false, commentCollapseButton__ = false; + let commentCollapseButton_ = false, commentCollapseButton__ = false; // 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域]) let commentCollapseButton = document.querySelector('.CommentCollapseButton') if (commentCollapseButton) { @@ -202,16 +216,22 @@ function backToTop(selectors) { } -//获取元素是否在可视区域 +//获取元素是否在可视区域(完全可见) function isElementInViewport(el) { let rect = el.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && - rect.bottom <= - (window.innerHeight || document.documentElement.clientHeight) && - rect.right <= - (window.innerWidth || document.documentElement.clientWidth) + rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && + rect.right <= (window.innerWidth || document.documentElement.clientWidth) + ); +} +//获取元素是否在可视区域(部分可见) +function isElementInViewport_(el) { + let rect = el.getBoundingClientRect(); + return ( + rect.top <= (window.innerHeight || document.documentElement.clientHeight) && + rect.left <= (window.innerWidth || document.documentElement.clientWidth) ); }