diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 6a24499..2787117 100644 --- a/Zhihu-Enhanced.user.js +++ b/Zhihu-Enhanced.user.js @@ -3,7 +3,7 @@ // @name:zh-CN 知乎增强 // @name:zh-TW 知乎增強 // @name:en Zhihu enhancement -// @version 2.2.7 +// @version 2.2.8 // @author X.I.U // @description 移除登录弹窗、屏蔽首页视频、默认收起回答、快捷收起回答/评论(左键两侧)、快捷回到顶部(右键两侧)、屏蔽用户、屏蔽关键词、移除高亮链接、屏蔽盐选内容/热榜杂项、净化搜索热门、净化标题消息、展开问题描述、显示问题作者、置顶显示时间、完整问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链 // @description:zh-TW 移除登錄彈窗、屏蔽首頁視頻、默認收起回答、快捷收起回答/評論、快捷回到頂部、屏蔽用戶、屏蔽關鍵詞、移除高亮鏈接、屏蔽鹽選內容、淨化搜索熱門、淨化標題消息、置頂顯示時間、完整問題時間、區分問題文章、默認高清原圖、默認站外直鏈... @@ -32,7 +32,7 @@ 'use strict'; var menu_ALL = [ ['menu_defaultCollapsedAnswer', '默认收起回答', '默认收起回答', true], - ['menu_collapsedAnswer', '一键收起回答', '一键收起回答', true], + ['menu_collapsedAnswer', '一键收起回答/评论', '一键收起回答/评论', true], ['menu_collapsedNowAnswer', '快捷收起回答/评论 (点击两侧空白处)', '快捷收起回答/评论', true], ['menu_backToTop', '快捷回到顶部 (右键两侧空白处)', '快捷回到顶部', true], ['menu_blockUsers', '屏蔽指定用户', '屏蔽指定用户', true], @@ -209,39 +209,42 @@ function defaultCollapsedAnswer() { } -// 一键收起回答(全部) +// 一键收起回答+评论(全部) function collapsedAnswer() { if (!menu_value('menu_collapsedAnswer')) return //console.log('1111', document.querySelector('.CornerAnimayedFlex')) if (document.querySelector('.CornerAnimayedFlex') && !document.getElementById('collapsed-button')) { - //console.log('2222') + // 向网页中插入收起全部回答按钮+样式+绑定点击事件 document.head.appendChild(document.createElement('style')).textContent = '.CornerButton{margin-bottom:8px !important;}.CornerButtons{bottom:45px !important;}'; - document.querySelector('.CornerAnimayedFlex').insertAdjacentHTML('afterBegin', ''); + document.querySelector('.CornerAnimayedFlex').insertAdjacentHTML('afterBegin', ''); document.getElementById('collapsed-button').onclick = function () { - if (location.pathname === '/' || location.pathname === '/hot' || location.pathname === '/follow') { - document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) { - if (el.hasAttribute('data-zop-retract-question')) { - el.click() - } - }); + + // 收起所有评论(悬浮的 [收起评论]) + document.querySelectorAll('.Comments-container').forEach(function (el) { + let commentCollapseButton = getXpath('//button[text()="收起评论"]', el) + if (commentCollapseButton) commentCollapseButton.click(); + }); + // 收起所有评论(固定的 [收起评论]) + document.querySelectorAll('.RichContent >.ContentItem-actions>button:first-of-type').forEach(function (el) { + if (el.textContent.indexOf('收起评论') > -1) el.click() + }); + + if (location.pathname === '/' || location.pathname === '/hot' || location.pathname === '/follow') {// 对于首页的关注、推荐、热榜 + document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) {if (el.hasAttribute('data-zop-retract-question')) {el.click();};}); } else { - 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(); - }) - }) + // 被 getCollapsedAnswerObserver 函数收起过的,固定 [收起] 按钮 + 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();});}) + // 被 getCollapsedAnswerObserver 函数收起过的,悬浮 [收起] 按钮(悬浮底部的横栏) document.querySelectorAll('.RichContent:not([script-collapsed]) .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) { let el = button.parentElement; - while (!el.classList.contains('RichContent')) { - el = el.parentElement; - } - if (el) { - el.setAttribute('script-collapsed', ''); - } + while (!el.classList.contains('RichContent')) {el = el.parentElement;} + if (el) el.setAttribute('script-collapsed', ''); button.click(); }) + const observer = getCollapsedAnswerObserver(); observer.start(); + if (!menu_value('menu_defaultCollapsedAnswer') && !observer._disconnectListener) { window.addEventListener('urlchange', function() { observer.end();