Skip to content

Commit 0e8c2be

Browse files
committed
新增 [一键收起评论] 功能(与 [一键收起回答] 合并)
1 parent b379bf5 commit 0e8c2be

1 file changed

Lines changed: 25 additions & 22 deletions

File tree

Zhihu-Enhanced.user.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @name:zh-CN 知乎增强
44
// @name:zh-TW 知乎增強
55
// @name:en Zhihu enhancement
6-
// @version 2.2.7
6+
// @version 2.2.8
77
// @author X.I.U
88
// @description 移除登录弹窗、屏蔽首页视频、默认收起回答、快捷收起回答/评论(左键两侧)、快捷回到顶部(右键两侧)、屏蔽用户、屏蔽关键词、移除高亮链接、屏蔽盐选内容/热榜杂项、净化搜索热门、净化标题消息、展开问题描述、显示问题作者、置顶显示时间、完整问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
99
// @description:zh-TW 移除登錄彈窗、屏蔽首頁視頻、默認收起回答、快捷收起回答/評論、快捷回到頂部、屏蔽用戶、屏蔽關鍵詞、移除高亮鏈接、屏蔽鹽選內容、淨化搜索熱門、淨化標題消息、置頂顯示時間、完整問題時間、區分問題文章、默認高清原圖、默認站外直鏈...
@@ -32,7 +32,7 @@
3232
'use strict';
3333
var menu_ALL = [
3434
['menu_defaultCollapsedAnswer', '默认收起回答', '默认收起回答', true],
35-
['menu_collapsedAnswer', '一键收起回答', '一键收起回答', true],
35+
['menu_collapsedAnswer', '一键收起回答/评论', '一键收起回答/评论', true],
3636
['menu_collapsedNowAnswer', '快捷收起回答/评论 (点击两侧空白处)', '快捷收起回答/评论', true],
3737
['menu_backToTop', '快捷回到顶部 (右键两侧空白处)', '快捷回到顶部', true],
3838
['menu_blockUsers', '屏蔽指定用户', '屏蔽指定用户', true],
@@ -209,39 +209,42 @@ function defaultCollapsedAnswer() {
209209
}
210210

211211

212-
// 一键收起回答(全部)
212+
// 一键收起回答+评论(全部)
213213
function collapsedAnswer() {
214214
if (!menu_value('menu_collapsedAnswer')) return
215215
//console.log('1111', document.querySelector('.CornerAnimayedFlex'))
216216
if (document.querySelector('.CornerAnimayedFlex') && !document.getElementById('collapsed-button')) {
217-
//console.log('2222')
217+
// 向网页中插入收起全部回答按钮+样式+绑定点击事件
218218
document.head.appendChild(document.createElement('style')).textContent = '.CornerButton{margin-bottom:8px !important;}.CornerButtons{bottom:45px !important;}';
219-
document.querySelector('.CornerAnimayedFlex').insertAdjacentHTML('afterBegin', '<button id="collapsed-button" data-tooltip="收起全部回答" data-tooltip-position="left" data-tooltip-will-hide-on-click="false" aria-label="收起全部回答" type="button" class="' + document.querySelector('.CornerAnimayedFlex>button').className + '"><svg class="ContentItem-arrowIcon is-active" aria-label="收起全部回答" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M16.036 19.59a1 1 0 0 1-.997.995H9.032a.996.996 0 0 1-.997-.996v-7.005H5.03c-1.1 0-1.36-.633-.578-1.416L11.33 4.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.005z"></path></svg></button>');
219+
document.querySelector('.CornerAnimayedFlex').insertAdjacentHTML('afterBegin', '<button id="collapsed-button" data-tooltip="收起全部回答/评论" data-tooltip-position="left" data-tooltip-will-hide-on-click="false" aria-label="收起全部回答/评论" type="button" class="' + document.querySelector('.CornerAnimayedFlex>button').className + '"><svg class="ContentItem-arrowIcon is-active" aria-label="收起全部回答/评论" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M16.036 19.59a1 1 0 0 1-.997.995H9.032a.996.996 0 0 1-.997-.996v-7.005H5.03c-1.1 0-1.36-.633-.578-1.416L11.33 4.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.005z"></path></svg></button>');
220220
document.getElementById('collapsed-button').onclick = function () {
221-
if (location.pathname === '/' || location.pathname === '/hot' || location.pathname === '/follow') {
222-
document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) {
223-
if (el.hasAttribute('data-zop-retract-question')) {
224-
el.click()
225-
}
226-
});
221+
222+
// 收起所有评论(悬浮的 [收起评论])
223+
document.querySelectorAll('.Comments-container').forEach(function (el) {
224+
let commentCollapseButton = getXpath('//button[text()="收起评论"]', el)
225+
if (commentCollapseButton) commentCollapseButton.click();
226+
});
227+
// 收起所有评论(固定的 [收起评论])
228+
document.querySelectorAll('.RichContent >.ContentItem-actions>button:first-of-type').forEach(function (el) {
229+
if (el.textContent.indexOf('收起评论') > -1) el.click()
230+
});
231+
232+
if (location.pathname === '/' || location.pathname === '/hot' || location.pathname === '/follow') {// 对于首页的关注、推荐、热榜
233+
document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) {if (el.hasAttribute('data-zop-retract-question')) {el.click();};});
227234
} else {
228-
document.querySelectorAll('[script-collapsed]').forEach(function(scriptCollapsed) {
229-
scriptCollapsed.querySelectorAll('.ContentItem-actions [data-zop-retract-question], .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) {
230-
button.click();
231-
})
232-
})
235+
// 被 getCollapsedAnswerObserver 函数收起过的,固定 [收起] 按钮
236+
document.querySelectorAll('[script-collapsed]').forEach(function(scriptCollapsed) {scriptCollapsed.querySelectorAll('.ContentItem-actions [data-zop-retract-question], .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) {button.click();});})
237+
// 被 getCollapsedAnswerObserver 函数收起过的,悬浮 [收起] 按钮(悬浮底部的横栏)
233238
document.querySelectorAll('.RichContent:not([script-collapsed]) .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) {
234239
let el = button.parentElement;
235-
while (!el.classList.contains('RichContent')) {
236-
el = el.parentElement;
237-
}
238-
if (el) {
239-
el.setAttribute('script-collapsed', '');
240-
}
240+
while (!el.classList.contains('RichContent')) {el = el.parentElement;}
241+
if (el) el.setAttribute('script-collapsed', '');
241242
button.click();
242243
})
244+
243245
const observer = getCollapsedAnswerObserver();
244246
observer.start();
247+
245248
if (!menu_value('menu_defaultCollapsedAnswer') && !observer._disconnectListener) {
246249
window.addEventListener('urlchange', function() {
247250
observer.end();

0 commit comments

Comments
 (0)