Skip to content

Commit 97c4467

Browse files
committed
修复 [收起当前回答/评论(点击两侧空白处)] 在部分回答下不起作用的问题
1 parent 3dc9461 commit 97c4467

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| [<img src="https://user-images.githubusercontent.com/54703944/118383905-b5d32380-b634-11eb-8914-dcea8e628ed3.png" height="16px" />](https://github.com/XIU2) | **护眼模式** | 简单有效的全网通用护眼模式、夜间模式、暗黑模式~ | **[安装](https://greasyfork.org/zh-CN/scripts/426377)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/DarkMode.user.js)** |
2121
| [<img src="https://static.zhihu.com/heifetz/favicon.ico" height="16px" />](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)** |
2222
| [<img src="https://static.zhihu.com/heifetz/favicon.ico" height="16px" />](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)** |
23-
| [<img src="https://www.v2ex.com/static/favicon.ico" height="16px" />](https://www.v2ex.com/) | **V2ex 增强** | **自动签到**、链接转图片、自动无缝翻页、新标签页打开链接... | **[安装](https://greasyfork.org/zh-CN/scripts/424246)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/V2ex-Enhanced.user.js)** |
23+
| [<img src="https://www.v2ex.com/static/favicon.ico" height="16px" />](https://www.v2ex.com/) | **V2EX 增强** | **自动签到**、链接转图片、自动无缝翻页、新标签页打开链接... | **[安装](https://greasyfork.org/zh-CN/scripts/424246)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/V2ex-Enhanced.user.js)** |
2424
| [<img src="https://i.loli.net/2021/03/30/ULV9XunaHesqGIR.png" height="16px" />](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)** |
2525
| [<img src="https://user-images.githubusercontent.com/54703944/118383905-b5d32380-b634-11eb-8914-dcea8e628ed3.png" height="16px" />](https://github.com/XIU2) | **自动无缝翻页 \*** | **所有 Discuz! / Flarum 论坛**、百度、豆瓣、微博、千图网... | **[安装](https://greasyfork.org/zh-CN/scripts/419215)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/Autopage.user.js)** |
2626
| [<img src="https://www.3dmgame.com/favicon.ico" height="16px" />](https://bbs.3dmgame.com) | **3DM论坛 美化** | 精简多余内容、样式优化 | **[安装](https://greasyfork.org/zh-CN/scripts/413593)** \| **[备用](https://cdn.jsdelivr.net/gh/XIU2/UserScript@master/3dm-Beautification.user.js)** |

Zhihu-Enhanced.user.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎增强
3-
// @version 1.5.2
3+
// @version 1.5.3
44
// @author X.I.U
55
// @description 移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
66
// @match *://www.zhihu.com/*
@@ -111,32 +111,46 @@ function collapsedNowAnswer(selectors) {
111111
if (event.target==this) {
112112
// 下面这段主要是 [收起回答],顺便 [收起评论](如果展开了的话)
113113
let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom')
114-
// 悬浮在底部的 [收起回答](此时正在浏览回答内容 [头部区域 + 中间区域])
114+
// 悬浮在底部的 [收起回答](此时正在浏览回答内容 [中间区域])
115115
if (rightButton) {
116116
// 固定的 [收起评论](先看看是否展开评论)
117117
let commentCollapseButton = rightButton.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
118118
if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
119119
// 再去收起回答
120-
rightButton = rightButton.querySelector('.ContentItem-rightButton')
121-
if (rightButton && rightButton.hasAttribute('data-zop-retract-question')) rightButton.click();
122-
// 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域])
120+
rightButton = rightButton.querySelector('.ContentItem-rightButton[data-zop-retract-question]')
121+
if (rightButton) rightButton.click();
122+
// 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域])
123123
} else {
124-
for (let el of document.querySelectorAll('.ContentItem-rightButton')) { // 遍历所有回答底部的 [收起] 按钮
125-
if (el.hasAttribute('data-zop-retract-question')) {
126-
if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内
124+
let answerCollapseButton_ = false;
125+
for (let el of document.querySelectorAll('.ContentItem-rightButton[data-zop-retract-question]')) { // 遍历所有回答底部的 [收起] 按钮
126+
if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内
127+
// 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
128+
let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
129+
// 如果展开了评论,就收起评论
130+
if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
131+
el.click() // 再去收起回答
132+
answerCollapseButton_ = true; // 如果找到并点击收起了,就没必要执行下面的代码了(可视区域中没有 [收起回答] 时)
133+
break
134+
}
135+
}
136+
// 针对完全看不到 [收起回答] 按钮时(如 [头部区域],以及部分明明很长却不显示悬浮横条的回答)
137+
if (!answerCollapseButton_) {
138+
for (let el of document.querySelectorAll('.List-item')) { // 遍历所有回答主体元素
139+
if (isElementInViewport_(el)) { // 判断该回答是否在可视区域内
127140
// 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
128141
let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
129142
// 如果展开了评论,就收起评论
130143
if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
131-
el.click() // 再去收起回答
144+
let answerCollapseButton__ = document.querySelector('.ContentItem-rightButton[data-zop-retract-question]');
145+
if (answerCollapseButton__) answerCollapseButton__.click() // 再去收起回答
132146
break
133147
}
134148
}
135149
}
136150
}
137151

138152
// 下面这段只针对 [收起评论](如果展开了的话)
139-
var commentCollapseButton_ = false, commentCollapseButton__ = false;
153+
let commentCollapseButton_ = false, commentCollapseButton__ = false;
140154
// 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域])
141155
let commentCollapseButton = document.querySelector('.CommentCollapseButton')
142156
if (commentCollapseButton) {
@@ -202,16 +216,22 @@ function backToTop(selectors) {
202216
}
203217

204218

205-
//获取元素是否在可视区域
219+
//获取元素是否在可视区域(完全可见)
206220
function isElementInViewport(el) {
207221
let rect = el.getBoundingClientRect();
208222
return (
209223
rect.top >= 0 &&
210224
rect.left >= 0 &&
211-
rect.bottom <=
212-
(window.innerHeight || document.documentElement.clientHeight) &&
213-
rect.right <=
214-
(window.innerWidth || document.documentElement.clientWidth)
225+
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
226+
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
227+
);
228+
}
229+
//获取元素是否在可视区域(部分可见)
230+
function isElementInViewport_(el) {
231+
let rect = el.getBoundingClientRect();
232+
return (
233+
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
234+
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
215235
);
216236
}
217237

0 commit comments

Comments
 (0)