知乎增强

屏蔽指定类别(视频、盐选、文章、想法、关注[赞同了XX/关注了XX]等等)、屏蔽用户、屏蔽关键词、默认收起回答、快捷收起回答/评论(左键两侧)、快捷回到顶部(右键两侧)、区分问题文章、移除高亮链接、净化搜索热门、净化标题消息、展开问题描述、显示问题作者、默认高清原图(无水印)、置顶显示时间、完整问题时间、直达问题按钮、默认站外直链...

目前为 2024-09-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 知乎增强
  3. // @name:zh-CN 知乎增强
  4. // @name:zh-TW 知乎增強
  5. // @name:en Zhihu enhancement
  6. // @version 2.3.9
  7. // @author X.I.U
  8. // @description 屏蔽指定类别(视频、盐选、文章、想法、关注[赞同了XX/关注了XX]等等)、屏蔽用户、屏蔽关键词、默认收起回答、快捷收起回答/评论(左键两侧)、快捷回到顶部(右键两侧)、区分问题文章、移除高亮链接、净化搜索热门、净化标题消息、展开问题描述、显示问题作者、默认高清原图(无水印)、置顶显示时间、完整问题时间、直达问题按钮、默认站外直链...
  9. // @description:zh-TW 屏蔽指定類別(視頻、鹽選、文章、想法、關注[贊同了XX/關注了XX]等等)、屏蔽用戶、屏蔽關鍵詞、默認收起回答、快捷收起回答/評論、快捷回到頂部、區分問題文章、移除高亮鏈接、默認高清原圖(無水印)、默認站外直鏈...
  10. // @description:en A more personalized Zhihu experience~
  11. // @match *://www.zhihu.com/*
  12. // @match *://zhuanlan.zhihu.com/*
  13. // @exclude https://www.zhihu.com/signin*
  14. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAFo0lEQVR4nJWXT4hlVxHGf9/tJyYuzJtxIziQN8RBFDEtIWB0MW+Mi4CIk4UuAtqvAxPcqAkJRNxMshDUTc8s3TjdO3c9QbIKod+o4CIuZtRFDEi3EIgEM/02jpPIPZ+LqnPv6TeB4IHLPfeeP1X11VdV54i2vewtOhaITcQDdEgdthAdINAGNkgd0IEVbwR17rAmx1TniSVil6e0V0UKgJc8pWdfG5yvwqRhs1FQIyQ3trvcpxsVGd4xf/gelO24yV0usK1VB6APOMDMKSADBShIBWNwj1WwjClAzLEL0CMc86jj9cl/LgRs8ZjCJvexH5r/xAuJa260TatG6DosIee4lHO0ZnWDXH5bgYQ8uql122Iis2Xnj4JtRH675AIHMAQKuAsdrLV3dYXTwQ7Bir1jrzJ+Wyw698yVkLmgxgVg+PGjcPw8zD6JKcgFUcD94AoNa0eXkO600lVqXOPqPrPZNT6tjylYBRZfgp0n4GgFR7dDkEieeNys6ZPCBq4MCicXdFLWdIKBHtMFfC4JY8Hf/nxEyY0jmD+IM7Cgy77gxtto+jF4+NMj8+tY84YOLd8JnZJPpiDxI7shUigimJ2Gwxf5yLb9aryvffOj5+pXKbzJDxOVoIw7REkLhedno7+6CzffgdkpmE3z+5/Nrq7JJMfejc3bNj+TnT4tTyRtmLiAlARKliK09Uis2fsTPPcqvvwNdPlxuP4mbO+P4aQObz0cG958F77+mxMJh9kDcPh0IpCcUIerqycYuc8FGX7zczB/CI6O4cofAA9GjuRjUFhN2I0hmISlNFCUzCGMSkxqfFJw3XD6cXjuOt57A1YfpM/KSSEqY9zLo4w0psLsVnknyhkVYgNPMlRQh+qE63+GSsaBoK2VJcdTKTcKDLmhA/o1BDKFKzKl3aOJcoNBu3SHAAe4ahwwKgBkFRhdMMIc6Chzf+VphruT6urwZBh1aEQXjL/2fZidHoVP74/3xS8GP1IBLQ/hxuGonCLFxpZKjtTpDpRrSjYwYa2p4GcvoPm59ZFU5L54apvP4MZhY2XJMh1u+DB0Qk7ybaIR2EGHl34LV1/HGU5CsP8MbH4Gnvw1vPJX7G48pGw/OiLgyoEKf4PAYH0f6VtdIOAmlwCwugOrO0mDOHRodjrG/vGv9HvyQEprRwQjPB3rWg5oXBNR0A+Vf2wtJwDZaP5ZPL0fVv+B4zto5yJjKK6R0CUPMI7K6jUEsgpKGYqTwXpneGiExKnR4iuZ6d6Ggx/C7FNwdBuu/C6ROBmiUU/6PDuWhh9tEssQHxFQVlmP3wKfPY23Hotfe3+Eq8tYf/kJmE2jdK8pIPWJTDmJAFmq1We57iNnDQol9HWFDbr8rfg8eg+Wb8KV19DyrQjLa08hNal4/hD4l3D+LNr/HvhncPhC49+Ev5475DHfeUjjGrPt4jHY+upgvZOAPL0XRJ2fgwdPNS7IdvADuPgF7m15UDEDOhaXXKQT6RyAzTNw8DxMPxHWn/1p6ObMjc8+DjvfhVMvwOr9EcLjnwc6V38PL78Gq7vYG8PBxAqTzUbInJwoGtlbfA3tfCeEL/8G23sZEaAMIV99HW2egdW/m9Is/Mpf0O4bsPw7tY4os+OAbuUXHZKe8W3gVDLeEjr/uTgRHb0Hy7fyf0PUQd2YH4eZesrRmMBqDhF5gRFiIxSRQoGJxS0Kc8XNRSasHlqStOZ3Nf1aKVQzIKkMzeHTGZaRoEzWmyxIyw6zW6FRQ6c2NapC5pCjuAl5+Od7Tru41vxagEpTOeN0LAq7oe0lH0DeC6uVH0Lito03kbVUnmSrp+yE/967Y8dNfqEvdwD+L09a3BrgbqA/IZS4H4osLE3mHlDzYB30iUJ7XwwklrzPhXWk4ZIXdGxROM//g0ZbQrKXVza0EQcQiWNvcIuOXXbG6/n/AAwhLDO9HaqBAAAAAElFTkSuQmCC
  15. // @grant GM_xmlhttpRequest
  16. // @grant GM_registerMenuCommand
  17. // @grant GM_unregisterMenuCommand
  18. // @grant GM_openInTab
  19. // @grant GM_getValue
  20. // @grant GM_setValue
  21. // @grant GM_notification
  22. // @grant GM_info
  23. // @grant window.onurlchange
  24. // @sandbox JavaScript
  25. // @license GPL-3.0 License
  26. // @run-at document-end
  27. // @namespace https://greasyfork.org/scripts/4122051
  28. // @supportURL https://github.com/XIU2/UserScript
  29. // @homepageURL https://github.com/XIU2/UserScript
  30. // ==/UserScript==
  31.  
  32. 'use strict';
  33. var menu_ALL = [
  34. ['menu_defaultCollapsedAnswer', '默认收起回答', '默认收起回答', true],
  35. ['menu_collapsedAnswer', '一键收起回答/评论', '一键收起回答/评论', true],
  36. ['menu_collapsedNowAnswer', '快捷收起回答/评论 (点击两侧空白处)', '快捷收起回答/评论', true],
  37. ['menu_backToTop', '快捷回到顶部 (右键两侧空白处)', '快捷回到顶部', true],
  38. ['menu_blockUsers', '屏蔽指定用户', '屏蔽指定用户', true],
  39. ['menu_customBlockUsers', '自定义屏蔽用户', '自定义屏蔽用户', ['故事档案局', '盐选推荐', '盐选科普', '盐选成长计划', '知乎盐选会员', '知乎盐选创作者', '盐选心理', '盐选健康必修课', '盐选奇妙物语', '盐选生活馆', '盐选职场', '盐选文学甄选', '盐选作者小管家', '盐选博物馆', '盐选点金', '盐选测评室', '盐选科技前沿', '盐选会员精品']],
  40. ['menu_blockKeywords', '屏蔽指定关键词', '屏蔽指定关键词', true],
  41. ['menu_blockKeywordsComment', '屏蔽关键词 - 评论区', '屏蔽关键词 - 评论区', true],
  42. ['menu_customBlockKeywords', '自定义屏蔽关键词', '自定义屏蔽关键词', []],
  43. ['menu_blockType', '屏蔽指定类别 (视频/文章等)', '勾选 = 屏蔽该类别的信息流', ''],
  44. ['menu_blockTypeVideo', '视频 [首页、搜索页、问题页、关注页]', '视频(首页、搜索页、问题页、关注页)', true],
  45. ['menu_blockTypeArticle', '文章 [首页、搜索页、关注页]', '文章(首页、搜索页、关注页)', false],
  46. ['menu_blockTypePin', '想法 [首页、关注页]', '想法(首页、关注页)', false],
  47. ['menu_blockTypeFollowAgree', '赞同了XX [关注页]', '赞同了XX(关注页)', false],
  48. ['menu_blockTypeFollowQuestion', '关注了XX [关注页]', '关注了XX(关注页)', false],
  49. ['menu_blockTypeTopic', '话题 [搜索页]', '话题(搜索页)', false],
  50. ['menu_blockTypeSearch', '杂志文章、盐选专栏、相关搜索等 [搜索页]', '相关搜索、杂志、盐选等(搜索页)', false],
  51. ['menu_blockYanXuan', '盐选内容 [问题页]', '盐选内容(问题页)', false],
  52. ['menu_blockTypeLiveHot', '热榜文章、直播、广告等 [热榜]', '热榜文章、直播、广告等 [热榜]', true],
  53. ['menu_cleanSearch', '净化搜索热门 (默认搜索词及热门搜索)', '净化搜索热门', false],
  54. ['menu_cleanTitles', '净化标题消息 (标题中的私信/消息)', '净化标题提醒', false],
  55. ['menu_questionRichTextMore', '展开问题描述', '展开问题描述', false],
  56. ['menu_publishTop', '置顶显示时间', '置顶显示时间', true],
  57. ['menu_typeTips', '区分问题文章', '区分问题文章', true],
  58. ['menu_toQuestion', '直达问题按钮', '直达问题按钮', true]
  59. ], menu_ID = [];
  60. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  61. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  62. }
  63. registerMenuCommand();
  64.  
  65. // 注册脚本菜单
  66. function registerMenuCommand() {
  67. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  68. for (let i=0;i<menu_ID.length;i++){
  69. GM_unregisterMenuCommand(menu_ID[i]);
  70. }
  71. }
  72. for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
  73. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  74. if (menu_ALL[i][0] === 'menu_customBlockUsers') { // 只有 [屏蔽指定用户] 启用时,才注册菜单 [自定义屏蔽用户]
  75. if (menu_value('menu_blockUsers')) menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){customBlockUsers()});
  76. } else if (menu_ALL[i][0] === 'menu_customBlockKeywords') { // 只有 [屏蔽指定关键词] 启用时,才注册菜单 [自定义屏蔽关键词]
  77. if (menu_value('menu_blockKeywords')) menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){customBlockKeywords()});
  78. } else if (menu_ALL[i][0] === 'menu_blockKeywordsComment') { // 只有 [屏蔽指定关键词] 启用时,才注册菜单 [屏蔽关键词 - 评论区]
  79. if (menu_value('menu_blockKeywords')) menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][3]?'✅':'❌'} ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
  80. } else if (menu_ALL[i][0] === 'menu_blockType') { // 屏蔽指定类别 使用单独的设置界面
  81. menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){menu_setting('checkbox', menu_ALL[i][1], menu_ALL[i][2], true, [menu_ALL[i+1], menu_ALL[i+2], menu_ALL[i+3], menu_ALL[i+4], menu_ALL[i+5], menu_ALL[i+6], menu_ALL[i+7], menu_ALL[i+8], menu_ALL[i+9]])});
  82. } else if (menu_ALL[i][0].indexOf('menu_blockType') == -1 && menu_ALL[i][0] != 'menu_blockYanXuan') { // 排除使用单独设置界面的 屏蔽指定类别 项
  83. menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][3]?'✅':'❌'} ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
  84. }
  85. }
  86. menu_ID[menu_ID.length] = GM_registerMenuCommand('💬 反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/419081/feedback', {active: true,insert: true,setParent: true});});
  87. }
  88.  
  89.  
  90. // 菜单开关
  91. function menu_switch(menu_status, Name, Tips) {
  92. if (menu_status == 'true'){
  93. GM_setValue(`${Name}`, false);
  94. GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  95. }else{
  96. GM_setValue(`${Name}`, true);
  97. GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  98. }
  99. registerMenuCommand(); // 重新注册脚本菜单
  100. };
  101.  
  102.  
  103. // 返回菜单值
  104. function menu_value(menuName) {
  105. for (let menu of menu_ALL) {
  106. if (menu[0] == menuName) {
  107. return menu[3]
  108. }
  109. }
  110. }
  111.  
  112.  
  113. // 脚本设置
  114. function menu_setting(type, title, tips, line, menu) {
  115. let _br = '', _html = `<style class="zhihuE_SettingStyle">.zhihuE_SettingRoot {position: absolute;top: 50%;left: 50%;-webkit-transform: translate(-50%, -50%);-moz-transform: translate(-50%, -50%);-ms-transform: translate(-50%, -50%);-o-transform: translate(-50%, -50%);transform: translate(-50%, -50%);width: auto;min-width: 400px;max-width: 600px;height: auto;min-height: 150px;max-height: 400px;color: #535353;background-color: #fff;border-radius: 3px;}
  116. .zhihuE_SettingBackdrop_1 {position: fixed;top: 0;right: 0;bottom: 0;left: 0;z-index: 203;display: -webkit-box;display: -ms-flexbox;display: flex;-webkit-box-orient: vertical;-webkit-box-direction: normal;-ms-flex-direction: column;flex-direction: column;-webkit-box-pack: center;-ms-flex-pack: center;justify-content: center;overflow-x: hidden;overflow-y: auto;-webkit-transition: opacity .3s ease-out;transition: opacity .3s ease-out;}
  117. .zhihuE_SettingBackdrop_2 {position: absolute;top: 0;right: 0;bottom: 0;left: 0;z-index: 0;background-color: rgba(18,18,18,.65);-webkit-transition: background-color .3s ease-out;transition: background-color .3s ease-out;}
  118. .zhihuE_SettingRoot .zhihuE_SettingHeader {padding: 10px 20px;color: #fff;font-weight: bold;background-color: #3994ff;border-radius: 3px 3px 0 0;}
  119. .zhihuE_SettingRoot .zhihuE_SettingMain {padding: 10px 20px;border-radius: 0 0 3px 3px;}
  120. .zhihuE_SettingHeader span {float: right;cursor: pointer;}
  121. .zhihuE_SettingMain input {margin: 10px 6px 10px 0;cursor: pointer;vertical-align:middle}
  122. .zhihuE_SettingMain label {margin-right: 20px;user-select: none;cursor: pointer;vertical-align:middle}
  123. .zhihuE_SettingMain hr {border: 0.5px solid #f4f4f4;}
  124. [data-theme="dark"] .zhihuE_SettingRoot {color: #adbac7;background-color: #343A44;}
  125. [data-theme="dark"] .zhihuE_SettingHeader {color: #d0d0d0;background-color: #2D333B;}
  126. [data-theme="dark"] .zhihuE_SettingMain hr {border: 0.5px solid #2d333b;}</style>
  127. <div class="zhihuE_SettingBackdrop_1"><div class="zhihuE_SettingBackdrop_2"></div><div class="zhihuE_SettingRoot">
  128. <div class="zhihuE_SettingHeader">${title}<span class="zhihuE_SettingClose" title="点击关闭"><svg class="Zi Zi--Close Modal-closeIcon" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M13.486 12l5.208-5.207a1.048 1.048 0 0 0-.006-1.483 1.046 1.046 0 0 0-1.482-.005L12 10.514 6.793 5.305a1.048 1.048 0 0 0-1.483.005 1.046 1.046 0 0 0-.005 1.483L10.514 12l-5.208 5.207a1.048 1.048 0 0 0 .006 1.483 1.046 1.046 0 0 0 1.482.005L12 13.486l5.207 5.208a1.048 1.048 0 0 0 1.483-.006 1.046 1.046 0 0 0 .005-1.482L13.486 12z" fill-rule="evenodd"></path></svg></span></div>
  129. <div class="zhihuE_SettingMain"><p>${tips}</p><hr>`
  130. if (line) _br = '<br>'
  131. for (let i=0; i<menu.length; i++) {
  132. if (GM_getValue(menu[i][0])) {
  133. _html += `<label><input name="zhihuE_Setting" type="checkbox" value="${menu[i][0]}" checked="checked">${menu[i][1]}</label>${_br}`
  134. } else {
  135. _html += `<label><input name="zhihuE_Setting" type="checkbox" value="${menu[i][0]}">${menu[i][1]}</label>${_br}`
  136. }
  137. }
  138. _html += `</div></div></div>`
  139. document.body.insertAdjacentHTML('beforeend', _html); // 插入网页末尾
  140. setTimeout(function() { // 延迟 100 毫秒,避免太快
  141. // 关闭按钮 点击事件
  142. document.querySelector('.zhihuE_SettingClose').onclick = function(){this.parentElement.parentElement.parentElement.remove();document.querySelector('.zhihuE_SettingStyle').remove();}
  143. // 点击周围空白处 = 点击关闭按钮
  144. document.querySelector('.zhihuE_SettingBackdrop_2').onclick = function(event){if (event.target == this) {document.querySelector('.zhihuE_SettingClose').click();};}
  145. // 复选框 点击事件
  146. document.getElementsByName('zhihuE_Setting').forEach(function (checkBox) {
  147. checkBox.addEventListener('click', function(){if (this.checked) {GM_setValue(this.value, true);} else {GM_setValue(this.value, false);}});
  148. })
  149. }, 100)
  150. }
  151.  
  152.  
  153. // 添加收起回答观察器
  154. function getCollapsedAnswerObserver() {
  155. if (!window._collapsedAnswerObserver) {
  156. const observer = new MutationObserver(mutations => {
  157. for (const mutation of mutations) {
  158. if (mutation.target.hasAttribute('script-collapsed')) return
  159. // 短的回答
  160. if (mutation.target.classList.contains('RichContent')) {
  161. for (const addedNode of mutation.addedNodes) {
  162. if (addedNode.nodeType != Node.ELEMENT_NODE) continue
  163. if (addedNode.className != 'RichContent-inner') continue
  164. if (addedNode.offsetHeight < 400) break
  165. //console.log('111',addedNode, addedNode.classList, addedNode.classList.contains('RichContent-inner'), addedNode.offsetHeight, addedNode.textContent.length)
  166. const button = mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]');
  167. if (button) {
  168. mutation.target.setAttribute('script-collapsed', '');
  169. button.click();
  170. return
  171. }
  172. }
  173. // 长的回答
  174. } else if (mutation.target.tagName === 'DIV' && !mutation.target.style.cssText && !mutation.target.className) {
  175. if (mutation.target.parentElement.hasAttribute('script-collapsed')) return
  176. //console.log('222',mutation.target, mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]'))
  177. const button = mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]');
  178. if (button) {
  179. mutation.target.parentElement.setAttribute('script-collapsed', '');
  180. button.click();
  181. return
  182. }
  183. }
  184. }
  185. })
  186.  
  187. observer.start = function() {
  188. if (!this._active) {
  189. this.observe(document, { childList: true, subtree: true });
  190. this._active = true;
  191. }
  192. }
  193. observer.end = function() {
  194. if (this._active) {
  195. this.disconnect();
  196. }
  197. }
  198.  
  199. window.addEventListener('urlchange', function() {
  200. observer[location.href.indexOf('/answer/') === -1 ? 'start' : 'end']();
  201. })
  202. window._collapsedAnswerObserver = observer;
  203. }
  204. return window._collapsedAnswerObserver
  205. }
  206.  
  207.  
  208. // 默认收起回答
  209. function defaultCollapsedAnswer() {
  210. if (!menu_value('menu_defaultCollapsedAnswer')) return
  211. const observer = getCollapsedAnswerObserver();
  212. if (location.href.indexOf('/answer/') === -1) {
  213. observer.start();
  214. }
  215. }
  216.  
  217.  
  218. // 一键收起回答+评论(全部)
  219. function collapsedAnswer() {
  220. if (!menu_value('menu_collapsedAnswer')) return
  221. //console.log('1111', document.querySelector('.CornerAnimayedFlex'))
  222. if (document.querySelector('.CornerAnimayedFlex') && !document.getElementById('collapsed-button')) {
  223. // 向网页中插入收起全部回答按钮+样式+绑定点击事件
  224. document.head.appendChild(document.createElement('style')).textContent = '.CornerButton{margin-bottom:8px !important;}.CornerButtons{bottom:45px !important;}';
  225. 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>');
  226. document.getElementById('collapsed-button').onclick = function () {
  227.  
  228. // 收起所有评论(悬浮的 [收起评论])
  229. document.querySelectorAll('.Comments-container').forEach(function (el) {
  230. let commentCollapseButton = getXpath('//button[text()="收起评论"]', el)
  231. if (commentCollapseButton) commentCollapseButton.click();
  232. });
  233. // 收起所有评论(固定的 [收起评论])
  234. document.querySelectorAll('.RichContent >.ContentItem-actions>button:first-of-type').forEach(function (el) {
  235. if (el.textContent.indexOf('收起评论') > -1) el.click()
  236. });
  237.  
  238. if (location.pathname === '/' || location.pathname === '/hot' || location.pathname === '/follow') {// 对于首页的关注、推荐、热榜
  239. document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) {if (el.hasAttribute('data-zop-retract-question')) {el.click();};});
  240. } else {
  241. // 被 getCollapsedAnswerObserver 函数收起过的,固定 [收起] 按钮
  242. 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();});})
  243. // 被 getCollapsedAnswerObserver 函数收起过的,悬浮 [收起] 按钮(悬浮底部的横栏)
  244. document.querySelectorAll('.RichContent:not([script-collapsed]) .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) {
  245. let el = button.parentElement;
  246. while (!el.classList.contains('RichContent')) {el = el.parentElement;}
  247. if (el) el.setAttribute('script-collapsed', '');
  248. button.click();
  249. })
  250.  
  251. const observer = getCollapsedAnswerObserver();
  252. observer.start();
  253.  
  254. if (!menu_value('menu_defaultCollapsedAnswer') && !observer._disconnectListener) {
  255. window.addEventListener('urlchange', function() {
  256. observer.end();
  257. window._collapsedAnswerObserver = null;
  258. })
  259. observer._disconnectListener = true;
  260. }
  261. }
  262. }
  263. }
  264. }
  265.  
  266.  
  267. // 收起当前回答、评论(监听点击事件,点击网页两侧空白处)
  268. function collapsedNowAnswer(selectors) {
  269. backToTop(selectors) // 快捷回到顶部
  270. if (!menu_value('menu_collapsedNowAnswer')) return
  271. document.querySelector(selectors).onclick = function(event){
  272. if (event.target == this) {
  273. // 下面这段主要是 [收起回答],顺便 [收起评论](如果展开了的话)
  274. let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom')
  275. if (rightButton) { // 悬浮在底部的 [收起回答](此时正在浏览回答内容 [中间区域])
  276. // 固定的 [收起评论](先看看是否展开评论)
  277. let commentCollapseButton = rightButton.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  278. //console.log('111')
  279. if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
  280. // 再去收起回答
  281. rightButton = rightButton.querySelector('.ContentItem-rightButton[data-zop-retract-question]')
  282. //console.log('222')
  283. if (rightButton) rightButton.click();
  284.  
  285. } else { // 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域])
  286.  
  287. // 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域])
  288. //if (getXpath('//button[text()="收起评论"]',document.querySelector('.Comments-container'))) {getXpath('//button[text()="收起评论"]',document.querySelector('.Comments-container')).click();console.log('asfaf')}
  289.  
  290. let answerCollapseButton_ = false;
  291. for (let el of document.querySelectorAll('.ContentItem-rightButton[data-zop-retract-question]')) { // 遍历所有回答底部的 [收起] 按钮
  292. if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内
  293. // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
  294. let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  295. // 如果展开了评论,就收起评论
  296. //console.log('333')
  297. //if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
  298. if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) {
  299. commentCollapseButton.click();
  300. if (!isElementInViewport(commentCollapseButton)) scrollTo(0,el.offsetTop+50)
  301. }
  302. //console.log('444')
  303. el.click() // 再去收起回答
  304. answerCollapseButton_ = true; // 如果找到并点击收起了,就没必要执行下面的代码了(可视区域中没有 [收起回答] 时)
  305. break
  306. }
  307. }
  308. // 针对完全看不到 [收起回答] 按钮时(如 [头部区域],以及部分明明很长却不显示悬浮横条的回答)
  309. if (!answerCollapseButton_) {
  310. for (let el of document.querySelectorAll('.List-item, .Card.AnswerCard, .Card.TopstoryItem')) { // 遍历所有回答主体元素
  311. if (isElementInViewport_(el)) { // 判断该回答是否在可视区域内
  312. // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
  313. let commentCollapseButton = el.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  314. // 如果展开了评论,就收起评论
  315. //console.log('555',commentCollapseButton)
  316. if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) {
  317. commentCollapseButton.click();
  318. if (!isElementInViewport(commentCollapseButton)) scrollTo(0,el.offsetTop+50)
  319. }
  320. let answerCollapseButton__ = el.querySelector('.ContentItem-rightButton[data-zop-retract-question]');
  321. //console.log('666')
  322. if (answerCollapseButton__) answerCollapseButton__.click() // 再去收起回答
  323. break
  324. }
  325. }
  326. }
  327. }
  328.  
  329. // 下面这段只针对 [收起评论](如果展开了的话)
  330. let commentCollapseButton_ = false, commentCollapseButton__ = false;
  331. // 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域])
  332. let commentCollapseButton = getXpath('//button[text()="收起评论"]',document.querySelector('.Comments-container'))
  333. if (commentCollapseButton) {
  334. //console.log('777', commentCollapseButton)
  335. commentCollapseButton.click();
  336. } else { // 固定的 [收起评论](此时正在浏览评论内容 [头部区域])
  337. let commentCollapseButton_1 = document.querySelectorAll('.ContentItem-actions > button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type, .ContentItem-action > button.Button.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  338. if (commentCollapseButton_1.length > 0) {
  339. for (let el of commentCollapseButton_1) {
  340. if (el.textContent.indexOf('收起评论') > -1) {
  341. if (isElementInViewport(el)) {
  342. //console.log('888')
  343. el.click()
  344. commentCollapseButton_ = true // 如果找到并点击了,就没必要执行下面的代码了(可视区域中没有 [收起评论] 时)
  345. break
  346. }
  347. }
  348. }
  349. }
  350. if (commentCollapseButton_ == false) { // 可视区域中没有 [收起评论] 时(此时正在浏览评论内容 [头部区域] + [尾部区域](不上不下的,既看不到固定的 [收起评论] 又看不到悬浮的 [收起评论])),需要判断可视区域中是否存在评论元素
  351. let commentCollapseButton_1 = document.querySelectorAll('.Comments-container')
  352. if (commentCollapseButton_1.length > 0) {
  353. for (let el of commentCollapseButton_1) {
  354. if (isElementInViewport(el)) {
  355. let parentElement = findParentElement(el, 'List-item') || findParentElement(el, 'Card '),
  356. commentCollapseButton = parentElement.querySelector('.ContentItem-actions > button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  357. if (commentCollapseButton.textContent.indexOf('收起评论') > -1) {
  358. //console.log('999')
  359. commentCollapseButton.click()
  360. if (!isElementInViewport(commentCollapseButton)) {console.log(parentElement,parentElement.offsetTop,parentElement.offsetHeight);scrollTo(0,parentElement.offsetTop+parentElement.offsetHeight-50)}
  361. commentCollapseButton__ = true // 如果找到并点击了,就没必要执行下面的代码了(可视区域中没有 评论元素 时)
  362. break
  363. }
  364. }
  365. }
  366. }
  367. if (commentCollapseButton__ == false) { // 如果上面的都没找到,那么就尝试寻找评论末尾的 [评论回复框]
  368. let commentCollapseButton_2 = document.querySelectorAll('.Editable-content')
  369. if (commentCollapseButton_2.length > 0) {
  370. for (let el of commentCollapseButton_2) {
  371. if (isElementInViewport(el)) {
  372. let parentElement = findParentElement(el, 'List-item') || findParentElement(el, 'Card '),
  373. commentCollapseButton = parentElement.querySelector('.ContentItem-actions > button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  374. //console.log(commentCollapseButton)
  375. if (commentCollapseButton.textContent.indexOf('收起评论') > -1) {
  376. //console.log('101010')
  377. commentCollapseButton.click()
  378. if (!isElementInViewport(commentCollapseButton)) {console.log(parentElement,parentElement.offsetTop,parentElement.offsetHeight);scrollTo(0,parentElement.offsetTop+parentElement.offsetHeight-50)}
  379. break
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390.  
  391.  
  392. // 回到顶部(监听点击事件,鼠标右键点击网页两侧空白处)
  393. function backToTop(selectors) {
  394. if (!menu_value('menu_backToTop')) return
  395. document.querySelector(selectors).oncontextmenu = function(event){
  396. if (event.target == this) {
  397. event.preventDefault();
  398. window.scrollTo(0,0)
  399. }
  400. }
  401. }
  402.  
  403.  
  404. //获取元素是否在可视区域(完全可见)
  405. function isElementInViewport(el) {
  406. let rect = el.getBoundingClientRect();
  407. return (
  408. rect.top >= 0 &&
  409. rect.left >= 0 &&
  410. rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
  411. rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  412. );
  413. }
  414. //获取元素是否在可视区域(部分可见)
  415. function isElementInViewport_(el) {
  416. let rect = el.getBoundingClientRect();
  417. return (
  418. rect.top < (window.innerHeight || document.documentElement.clientHeight) &&
  419. rect.bottom > 0
  420. );
  421. }
  422.  
  423.  
  424. // 自定义屏蔽用户
  425. function customBlockUsers() {
  426. let nowBlockUsers = '';
  427. menu_value('menu_customBlockUsers').forEach(function(item){nowBlockUsers += '|' + item})
  428. //console.log(nowBlockUsers.replace('|',''))
  429. let newBlockUsers = prompt('编辑 [自定义屏蔽用户]\n(不同用户名之间使用 "|" 分隔,例如:用户A|用户B|用户C )', nowBlockUsers.replace('|',''));
  430. if (newBlockUsers === '') {
  431. GM_setValue('menu_customBlockUsers', []);
  432. registerMenuCommand(); // 重新注册脚本菜单
  433. } else if (newBlockUsers != null) {
  434. GM_setValue('menu_customBlockUsers', newBlockUsers.split('|'));
  435. registerMenuCommand(); // 重新注册脚本菜单
  436. }
  437. };
  438.  
  439.  
  440. // 屏蔽指定用户
  441. function blockUsers(type) {
  442. if (!menu_value('menu_blockUsers')) return
  443. if (!menu_value('menu_customBlockUsers') || menu_value('menu_customBlockUsers').length < 1) return
  444. switch(type) {
  445. case 'index':
  446. blockUsers_('.Card.TopstoryItem.TopstoryItem-isRecommend', 'Card TopstoryItem TopstoryItem-isRecommend');
  447. break;
  448. case 'question':
  449. blockUsers_question();
  450. break;
  451. case 'search':
  452. blockUsers_search();
  453. break;
  454. case 'topic':
  455. blockUsers_('.List-item.TopicFeedItem', 'List-item TopicFeedItem');
  456. break;
  457. case 'people':
  458. blockUsers_button_people(); // 添加屏蔽用户按钮(用户主页)
  459. break;
  460. }
  461. blockUsers_comment(); // 评论区
  462. blockUsers_button(); // 加入黑名单按钮(用户信息悬浮框中)
  463.  
  464. function blockUsers_(className1, className2) {
  465. // 前几条因为是直接加载的,而不是动态插入网页的,所以需要单独判断
  466. function blockKeywords_now() {
  467. document.querySelectorAll(className1).forEach(function(item1){
  468. let item = item1.querySelector('.ContentItem.AnswerItem, .ContentItem.ArticleItem'); // 用户名所在元素
  469. if (item) {
  470. for (const keyword of menu_value('menu_customBlockUsers')) { // 遍历用户名黑名单
  471. if (keyword != '' && item.dataset.zop.indexOf('authorName":"' + keyword + '",') > -1) { // 找到就删除该信息流
  472. console.log('已屏蔽:' + item.dataset.zop);
  473. item1.hidden = true;
  474. break;
  475. }
  476. }
  477. }
  478. })
  479. }
  480.  
  481. blockKeywords_now();
  482. window.addEventListener('urlchange', function(){
  483. setTimeout(blockKeywords_now, 1000); // 网页 URL 变化后再次执行
  484. })
  485.  
  486. // 这个是监听网页插入事件,用来判断后续网页动态插入的元素
  487. const callback = (mutationsList, observer) => {
  488. for (const mutation of mutationsList) {
  489. for (const target of mutation.addedNodes) {
  490. if (target.nodeType != 1) return
  491. if (target.className === className2) {
  492. let item = target.querySelector('.ContentItem.AnswerItem, .ContentItem.ArticleItem'); // 用户名所在元素
  493. if (item) {
  494. for (const keyword of menu_value('menu_customBlockUsers')) { // 遍历用户名黑名单
  495. if (keyword != '' && item.dataset.zop.indexOf('authorName":"' + keyword + '",') > -1) { // 找到就删除该信息流
  496. console.log('已屏蔽:' + item.dataset.zop);
  497. target.hidden = true;
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. };
  506. const observer = new MutationObserver(callback);
  507. observer.observe(document, { childList: true, subtree: true });
  508. }
  509.  
  510.  
  511. function blockUsers_question() {
  512. const blockUsers_question_ = (mutationsList, observer) => {
  513. for (const mutation of mutationsList) {
  514. for (const target of mutation.addedNodes) {
  515. if (target.nodeType != 1) return
  516. if (target.className === 'List-item' || target.className === 'Card AnswerCard') {
  517. let item1 = target.querySelector('.ContentItem.AnswerItem');
  518. if (item1) {
  519. menu_value('menu_customBlockUsers').forEach(function(item2){ // 遍历用户黑名单
  520. if (item1.dataset.zop.indexOf('authorName":"' + item2 + '",') > -1) { // 找到就删除该回答
  521. console.log('已屏蔽:' + item1.dataset.zop)
  522. target.hidden = true;
  523. }
  524. })
  525. }
  526. }
  527. }
  528. }
  529. };
  530.  
  531. const blockUsers_question_answer_ = (mutationsList, observer) => {
  532. for (const mutation of mutationsList) {
  533. for (const target of mutation.addedNodes) {
  534. if (target.nodeType != 1) return
  535. target.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  536. let item1 = item.querySelector('.ContentItem.AnswerItem');
  537. if (item1) {
  538. menu_value('menu_customBlockUsers').forEach(function(item2){ // 遍历用户黑名单
  539. if (item1.dataset.zop.indexOf('authorName":"' + item2 + '",') > -1) { // 找到就删除该回答
  540. console.log('已屏蔽:' + item1.dataset.zop)
  541. item.hidden = true;
  542. }
  543. })
  544. }
  545. })
  546. }
  547. }
  548. };
  549.  
  550. if (location.pathname.indexOf('/answer/') > -1) { // 回答页(就是只有三个回答的页面)
  551. const observer = new MutationObserver(blockUsers_question_answer_);
  552. observer.observe(document, { childList: true, subtree: true });
  553. } else { // 问题页(可以显示所有回答的页面)
  554. const observer = new MutationObserver(blockUsers_question_);
  555. observer.observe(document, { childList: true, subtree: true });
  556. }
  557.  
  558. // 针对的是打开网页后直接加载的前面几个回答(上面哪些是针对动态加载的回答)
  559. document.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  560. let item1 = item.querySelector('.ContentItem.AnswerItem');
  561. if (item1) {
  562. menu_value('menu_customBlockUsers').forEach(function(item2){ // 遍历用户黑名单
  563. if (item1.dataset.zop.indexOf('authorName":"' + item2 + '",') > -1) { // 找到就删除该回答
  564. console.log('已屏蔽:' + item1.dataset.zop)
  565. item.hidden = true;
  566. }
  567. })
  568. }
  569. })
  570. }
  571.  
  572. function blockUsers_search() {
  573. function blockUsers_now() {
  574. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  575. document.querySelectorAll('.Card.SearchResult-Card[data-za-detail-view-path-module="AnswerItem"], .Card.SearchResult-Card[data-za-detail-view-path-module="PostItem"]').forEach(function(item1){
  576. let item = item1.querySelector('.RichText.ztext.CopyrightRichText-richText b'); // 用户名所在元素
  577. if (item) {
  578. for (const keyword of menu_value('menu_customBlockUsers')) { // 遍历用户名黑名单
  579. if (keyword != '' && item.textContent === keyword) { // 找到就删除该信息流
  580. console.log('已屏蔽:' + item.textContent);
  581. item1.hidden = true;
  582. break;
  583. }
  584. }
  585. }
  586. })
  587. }
  588.  
  589. setTimeout(blockUsers_now, 2000);
  590. window.addEventListener('urlchange', function(){
  591. setTimeout(blockUsers_now, 1000); // 网页 URL 变化后再次执行
  592. })
  593.  
  594. const callback = (mutationsList, observer) => {
  595. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  596. for (const mutation of mutationsList) {
  597. for (const target of mutation.addedNodes) {
  598. if (target.nodeType != 1) return
  599. let item = target.querySelector('.Card.SearchResult-Card[data-za-detail-view-path-module="AnswerItem"] .RichText.ztext.CopyrightRichText-richText b, .Card.SearchResult-Card[data-za-detail-view-path-module="PostItem"] .RichText.ztext.CopyrightRichText-richText b');
  600. if (item) {
  601. for (const keyword of menu_value('menu_customBlockUsers')) { // 遍历用户名黑名单
  602. if (keyword != '' && item.textContent === keyword) { // 找到就删除该信息流
  603. console.log('已屏蔽:' + item.textContent);
  604. target.hidden = true;
  605. break;
  606. }
  607. }
  608. }
  609. }
  610. }
  611. };
  612. const observer = new MutationObserver(callback);
  613. observer.observe(document, { childList: true, subtree: true });
  614. }
  615.  
  616. function blockUsers_comment() {
  617. const callback = (mutationsList, observer) => {
  618. for (const mutation of mutationsList) {
  619. for (const target of mutation.addedNodes) {
  620. if (target.nodeType != 1) return
  621. //console.log(target)
  622. if (target.tagName == 'DIV' && target.className.indexOf('css-') == 0 && target.dataset.id == undefined) {
  623. let item = target.querySelector('a[href^="https://www.zhihu.com/people/"]>img.Avatar[alt][loading]')
  624. if (item) {
  625. //console.log(item)
  626. menu_value('menu_customBlockUsers').forEach(function(item1){ // 遍历用户黑名单
  627. if (item.alt === item1) { // 找到就删除该搜索结果
  628. //console.log(item.alt,item1)
  629. item.parentElement.parentElement.parentElement.parentElement.style.display = "none";
  630. }
  631. })
  632.  
  633. // 添加屏蔽用户按钮(点赞、回复等按钮后面)
  634. /*if (item) {
  635. let footer = findParentElement(item, 'CommentItemV2-meta', true).parentElement.querySelector('.CommentItemV2-metaSibling > .CommentItemV2-footer'),
  636. userid = item.parentElement;
  637. if (userid && footer && !footer.lastElementChild.dataset.name) {
  638. userid = userid.href.split('/')[4];
  639. footer.insertAdjacentHTML('beforeend',`<button type="button" data-name="${item.alt}" data-userid="${userid}" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;">&#8203;<svg class="Zi Zi--Like" fill="currentColor" viewBox="0 0 24 24" width="16" height="16" style="transform: rotate(180deg); margin-right: 5px;"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>`);
  640. footer.lastElementChild.onclick = function(){blockUsers_button_add(this.dataset.name, this.dataset.userid, false)}
  641. }
  642. }*/
  643. }
  644. }
  645. }
  646. }
  647. };
  648. const observer = new MutationObserver(callback);
  649. observer.observe(document, { childList: true, subtree: true });
  650. }
  651.  
  652.  
  653. // 添加屏蔽用户按钮(用户信息悬浮框中)
  654. function blockUsers_button() {
  655. const callback = (mutationsList, observer) => {
  656. for (const mutation of mutationsList) {
  657. for (const target of mutation.addedNodes) {
  658. if (target.nodeType != 1) return
  659. //console.log(target, target.className)
  660. if (target.tagName == 'DIV' && target.className && (target.className.indexOf('css-') == 0 || target.style == 'opacity: 1;')) {
  661. const item = target.querySelector('.MemberButtonGroup.ProfileButtonGroup.HoverCard-buttons'),
  662. item1 = target.querySelector('img.Avatar+div span.UserLink>a.UserLink-link[data-za-detail-view-element_name=User]');
  663. if (item1) {
  664. const name = item1.textContent, userid = item1.href.split('/')[4], users = menu_value('menu_customBlockUsers');
  665. for (let num = 0;num<users.length;num++) { // 判断是否已存在
  666. if (users[num] === name) { // 已存在
  667. target.querySelectorAll('.Button.Button--primary.Button--red').forEach(function(item){item.style.display = 'none';}) // 隐藏知乎自带的已屏蔽按钮
  668. item.insertAdjacentHTML('afterbegin', `<button type="button" data-name="${name}" data-userid="${userid}" class="Button FollowButton Button--primary Button--red"><span style="display: inline-flex; align-items: center;">​<svg width="1.2em" height="1.2em" viewBox="0 0 24 24" class="Zi Zi--Ban" fill="currentColor"><path fill-rule="evenodd" d="M16.346 18.113a7.5 7.5 0 0 1-10.46-10.46l10.46 10.46Zm1.767-1.767L7.654 5.886a7.5 7.5 0 0 1 10.46 10.46ZM22 12c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10Z" clip-rule="evenodd"></path></svg></span> 已屏蔽</button>`);
  669. item.firstElementChild.onclick = function(){this.disabled = true;blockUsers_button_del(this.dataset.name, this.dataset.userid, false)}
  670. return
  671. }
  672. };
  673. if (item && !target.querySelector('button[data-name][data-userid]')) {
  674. item.insertAdjacentHTML('beforeend', `<button type="button" data-name="${name}" data-userid="${userid}" class="Button FollowButton Button--primary Button--red" style="width: 100%;margin: 7px 0 0 0;"><span style="display: inline-flex; align-items: center;">​<svg width="1.2em" height="1.2em" viewBox="0 0 24 24" class="Zi Zi--Ban" fill="currentColor"><path fill-rule="evenodd" d="M16.346 18.113a7.5 7.5 0 0 1-10.46-10.46l10.46 10.46Zm1.767-1.767L7.654 5.886a7.5 7.5 0 0 1 10.46 10.46ZM22 12c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10Z" clip-rule="evenodd"></path></svg></span> 屏蔽用户</button>`);
  675. item.lastElementChild.onclick = function(){this.disabled = true;blockUsers_button_add(this.dataset.name, this.dataset.userid, false)}
  676. }
  677. }
  678. }
  679. }
  680. }
  681. };
  682. const observer = new MutationObserver(callback);
  683. observer.observe(document, { childList: true, subtree: true });
  684. }
  685.  
  686. // 添加屏蔽用户按钮(用户主页)
  687. function blockUsers_button_people() {
  688. let item = document.querySelector('.MemberButtonGroup.ProfileButtonGroup.ProfileHeader-buttons'), // 获取按钮元素位置
  689. name = document.querySelector('.ProfileHeader-name').firstChild.textContent, // 获取用户名
  690. users = menu_value('menu_customBlockUsers'), // 读取屏蔽列表
  691. userid = location.href.split('/')[4];
  692. for (let num = 0;num<users.length;num++) { // 判断是否已存在
  693. if (users[num] === name) { // 已存在
  694. document.querySelectorAll('.Button.Button--primary.Button--red').forEach(function(item){item.style.display = 'none';}) // 隐藏知乎自带的已屏蔽按钮
  695. item.insertAdjacentHTML('afterbegin', `<button type="button" data-name="${name}" data-userid="${userid}" class="Button FollowButton Button--primary Button--red" style="margin: 0 0 0 12px;"><span style="display: inline-flex; align-items: center;">​<svg width="1.2em" height="1.2em" viewBox="0 0 24 24" class="Zi Zi--Ban" fill="currentColor"><path fill-rule="evenodd" d="M16.346 18.113a7.5 7.5 0 0 1-10.46-10.46l10.46 10.46Zm1.767-1.767L7.654 5.886a7.5 7.5 0 0 1 10.46 10.46ZM22 12c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10Z" clip-rule="evenodd"></path></svg></span> 已屏蔽</button>`);
  696. item.firstElementChild.onclick = function(){this.disabled = true;blockUsers_button_del(this.dataset.name, this.dataset.userid, true)}
  697. return
  698. }
  699. };
  700. if (item) {
  701. item.insertAdjacentHTML('beforeend', `<button type="button" data-name="${name}" data-userid="${userid}" class="Button FollowButton Button--primary Button--red" style="margin: 0 0 0 12px;"><span style="display: inline-flex; align-items: center;">​<svg width="1.2em" height="1.2em" viewBox="0 0 24 24" class="Zi Zi--Ban" fill="currentColor"><path fill-rule="evenodd" d="M16.346 18.113a7.5 7.5 0 0 1-10.46-10.46l10.46 10.46Zm1.767-1.767L7.654 5.886a7.5 7.5 0 0 1 10.46 10.46ZM22 12c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10Z" clip-rule="evenodd"></path></svg></span> 屏蔽用户</button>`);
  702. item.lastElementChild.onclick = function(){this.disabled = true;blockUsers_button_add(this.dataset.name, this.dataset.userid, true)}
  703. }
  704. }
  705.  
  706. // 屏蔽用户按钮绑定事件(添加)
  707. function blockUsers_button_add(name, userid, reload) {
  708. if (!name || !userid) return
  709. let users = menu_value('menu_customBlockUsers'), // 读取屏蔽列表
  710. index = users.indexOf(name);
  711. if (index === -1) {
  712. users.push(name); // 追加用户名
  713. GM_setValue('menu_customBlockUsers', users); // 写入屏蔽列表
  714. // 加入知乎自带的黑名单(和本脚本互补~
  715. GM_xmlhttpRequest({url: `https://www.zhihu.com/api/v4/members/${userid}/actions/block`,method: 'POST',timeout: 2000});
  716. // 是否刷新本页
  717. if (reload) {
  718. setTimeout(function(){location.reload()}, 200); // 刷新网页,延迟 200 毫秒,避免知乎反应慢~
  719. } else {
  720. GM_notification({text: `该用户已被屏蔽~\n刷新网页后生效~`, timeout: 3000});
  721. }
  722. } else {
  723. GM_notification({text: `该用户已经被屏蔽啦,无需重复屏蔽~`, timeout: 3000});
  724. }
  725. }
  726.  
  727.  
  728. // 屏蔽用户按钮绑定事件(删除)
  729. function blockUsers_button_del(name, userid, reload) {
  730. if (!name || !userid) return
  731. let users = menu_value('menu_customBlockUsers'), // 读取屏蔽列表
  732. index = users.indexOf(name);
  733. if (index > -1) {
  734. users.splice(index, 1); // 移除用户名
  735. GM_setValue('menu_customBlockUsers', users); // 写入屏蔽列表
  736. // 移除知乎自带的黑名单
  737. GM_xmlhttpRequest({url: `https://www.zhihu.com/api/v4/members/${userid}/actions/block`,method: 'DELETE',timeout: 2000});
  738. // 是否刷新本页
  739. if (reload) {
  740. setTimeout(function(){location.reload()}, 200); // 刷新网页,延迟 200 毫秒,避免知乎反应慢~
  741. } else {
  742. GM_notification({text: `该用户已取消屏蔽啦~\n刷新网页后生效~`, timeout: 3000});
  743. }
  744. } else {
  745. GM_notification({text: `没有在屏蔽列表中找到该用户...`, timeout: 3000});
  746. }
  747. }
  748. }
  749.  
  750.  
  751. // 自定义屏蔽关键词(标题)
  752. function customBlockKeywords() {
  753. let nowBlockKeywords = '';
  754. menu_value('menu_customBlockKeywords').forEach(function(item){nowBlockKeywords += '|' + item})
  755. let newBlockKeywords = prompt('编辑 [自定义屏蔽关键词]\n(不同关键词之间使用 "|" 分隔,例如:关键词A|关键词B|关键词C \n(关键词不区分大小写,支持表情如:[捂脸]|[飙泪笑]', nowBlockKeywords.replace('|',''));
  756. if (newBlockKeywords === '') {
  757. GM_setValue('menu_customBlockKeywords', []);
  758. registerMenuCommand(); // 重新注册脚本菜单
  759. } else if (newBlockKeywords != null) {
  760. GM_setValue('menu_customBlockKeywords', newBlockKeywords.split('|'));
  761. registerMenuCommand(); // 重新注册脚本菜单
  762. }
  763. };
  764.  
  765.  
  766. // 屏蔽指定关键词
  767. function blockKeywords(type) {
  768. if (!menu_value('menu_blockKeywords')) return
  769. if (!menu_value('menu_customBlockKeywords') || menu_value('menu_customBlockKeywords').length < 1) return
  770. switch(type) {
  771. case 'index':
  772. blockKeywords_('.Card.TopstoryItem.TopstoryItem-isRecommend', 'Card TopstoryItem TopstoryItem-isRecommend');
  773. break;
  774. case 'follow':
  775. blockKeywords_('.Card.TopstoryItem.TopstoryItem-isFollow', 'Card TopstoryItem TopstoryItem-isFollow');
  776. break;
  777. case 'topic':
  778. blockKeywords_('.List-item.TopicFeedItem', 'List-item TopicFeedItem');
  779. break;
  780. case 'people':
  781. blockKeywords_('.List-item', 'List-item');
  782. break;
  783. case 'collection':
  784. blockKeywords_('.Card.CollectionDetailPageItem', 'Card CollectionDetailPageItem');
  785. break;
  786. case 'search':
  787. blockKeywords_search();
  788. break;
  789. case 'comment':
  790. if (!menu_value('menu_blockKeywordsComment')) return // 如果 [屏蔽关键词 - 评论区] 未启用则跳过
  791. blockKeywords_comment();
  792. break;
  793. }
  794.  
  795.  
  796. function blockKeywords_(className1, className2) {
  797. // 前几条因为是直接加载的,而不是动态插入网页的,所以需要单独判断
  798. function blockKeywords_now() {
  799. if (location.pathname === '/hot') {
  800. document.querySelectorAll('.HotItem').forEach(function(item1){blockKeywords_1(item1, 'h2.HotItem-title');})
  801. } else {
  802. document.querySelectorAll(className1).forEach(function(item1){blockKeywords_1(item1, 'h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]');})
  803. }
  804. }
  805.  
  806. blockKeywords_now();
  807. window.addEventListener('urlchange', function(){
  808. setTimeout(blockKeywords_now, 1000); // 网页 URL 变化后再次执行
  809. })
  810.  
  811. // 这个是监听网页插入事件,用来判断后续网页动态插入的元素
  812. const callback = (mutationsList, observer) => {
  813. for (const mutation of mutationsList) {
  814. for (const target of mutation.addedNodes) {
  815. if (target.nodeType != 1) return
  816. if (target.className === className2) {blockKeywords_1(target, 'h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]');}
  817. }
  818. }
  819. };
  820. const observer = new MutationObserver(callback);
  821. observer.observe(document, { childList: true, subtree: true });
  822. }
  823.  
  824.  
  825. function blockKeywords_search() {
  826. function blockKeywords_now() {
  827. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  828. document.querySelectorAll('.HotLanding-contentItem, .Card.SearchResult-Card[data-za-detail-view-path-module="AnswerItem"], .Card.SearchResult-Card[data-za-detail-view-path-module="PostItem"]').forEach(function(item1){blockKeywords_1(item1, 'a[data-za-detail-view-id]');})
  829. }
  830.  
  831. setTimeout(blockKeywords_now, 2000);
  832. window.addEventListener('urlchange', function(){
  833. setTimeout(blockKeywords_now, 1000); // 网页 URL 变化后再次执行
  834. })
  835.  
  836. const callback = (mutationsList, observer) => {
  837. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  838. for (const mutation of mutationsList) {
  839. for (const target of mutation.addedNodes) {
  840. if (target.nodeType != 1) return
  841. //console.log(target)
  842. if (target.tagName === 'DIV' && target.className === '') {
  843. let tt = target.querySelector('div[class="Card SearchResult-Card"][data-za-detail-view-path-module="AnswerItem"], div[class="Card SearchResult-Card"][data-za-detail-view-path-module="PostItem"]')
  844. if (tt) {blockKeywords_1(target.childNodes[0], 'a[data-za-detail-view-id]');}
  845. }
  846. }
  847. }
  848. };
  849. const observer = new MutationObserver(callback);
  850. observer.observe(document, { childList: true, subtree: true });
  851. }
  852.  
  853.  
  854. function blockKeywords_comment() {
  855. function filterComment(comment) {
  856. let content = comment.querySelector('.CommentContent'); // 寻找评论文字所在元素
  857. let text = content.textContent.toLowerCase(); // 全部转为小写(用来不区分大小写)
  858. content.querySelectorAll('img.sticker[alt]').forEach((img)=>{text += img.alt}) // 将评论中的表情添加到待遍历的评论文字中
  859.  
  860. let keywords = menu_value('menu_customBlockKeywords');
  861. for (const keyword of keywords) { // 遍历关键词黑名单
  862. if (keyword != '' && text.indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该评论
  863. console.log('已屏蔽评论:' + text);
  864. content.dataset.text = content.innerHTML
  865. content.onclick = (e)=>{if (e.target.dataset.text) {e.target.innerHTML = e.target.dataset.text;e.target.removeAttribute('data-text');}}
  866. content.textContent = '[该评论已屏蔽,可点击显示]';
  867. }
  868. }
  869. }
  870.  
  871. const callback = (mutationsList, observer) => {
  872. for (const mutation of mutationsList) {
  873. for (const target of mutation.addedNodes) {
  874. if (target.nodeType != 1) return
  875. //console.log(target);
  876. if (target.tagName == 'DIV' && target.className.indexOf('css-') == 0 && target.dataset.id == undefined) {
  877. let item = target.querySelector('a[href^="https://www.zhihu.com/people/"]>img.Avatar[alt][loading]')
  878. if (item) {
  879. //console.log(item)
  880. filterComment(item.parentElement.parentElement.parentElement.parentElement)
  881. }
  882. }
  883.  
  884. /*if (target.tagName == 'DIV' && target.dataset.id !== undefined) {
  885. console.log(target);
  886. for (const node of target.querySelectorAll('*')) {
  887. if (node.className === 'CommentItemV2-metaSibling') filterComment(node);
  888. }
  889. }*/
  890. }
  891. }
  892. };
  893. const observer = new MutationObserver(callback);
  894. observer.observe(document, { childList: true, subtree: true });
  895. }
  896.  
  897. function blockKeywords_1(item1, css) {
  898. let item = item1.querySelector(css); // 标题所在元素
  899. //console.log(item)
  900. if (item) {
  901. for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
  902. let text = item.content || item.textContent;
  903. //console.log(text,keyword)
  904. if (keyword != '' && text.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
  905. console.log('已屏蔽:' + text);
  906. item1.hidden = true;
  907. item1.style.display = 'none';
  908. break;
  909. }
  910. }
  911. }
  912. }
  913. }
  914.  
  915.  
  916. // 屏蔽指定类别(视频/文章等)
  917. function blockType(type) {
  918. let name;
  919. // 一开始加载的信息流 + 添加标签样式
  920. if (type === 'search') { // 搜索页
  921. if (!menu_value('menu_blockTypeVideo') && !menu_value('menu_blockTypeArticle') && !menu_value('menu_blockTypePin') && !menu_value('menu_blockTypeTopic') && !menu_value('menu_blockTypeSearch')) return
  922. if (menu_value('menu_blockTypeSearch') && location.pathname === '/search') setTimeout(function(){document.querySelectorAll('.RelevantQuery').forEach((r)=>{r.parentElement.parentElement.hidden = true});}, 2000)
  923. name = 'h2.ContentItem-title a:not(.zhihu_e_toQuestion), a.KfeCollection-PcCollegeCard-link, h2.SearchTopicHeader-Title a'
  924. addSetInterval_(name);
  925. } else if (type === 'question') { // 问题页
  926. if (!menu_value('menu_blockTypeVideo')) return
  927. document.lastChild.appendChild(document.createElement('style')).textContent = `.VideoAnswerPlayer, .VideoAnswerPlayer video, .VideoAnswerPlayer-video, .VideoAnswerPlayer-iframe {display: none !important;}`;
  928. name = '.VideoAnswerPlayer'
  929. document.querySelectorAll(name).forEach(function(item){blockType_(item);})
  930. } else if (type === 'follow') { // 首页 - 关注
  931. if (!menu_value('menu_blockTypeFollowAgree') && !menu_value('menu_blockTypeFollowQuestion')) return
  932. if (menu_value('menu_blockTypeFollowAgree')) name = '.TopstoryItem-isFollow .FeedSource-byline' // 赞同了XX
  933. if (menu_value('menu_blockTypeFollowQuestion')) {if (name) {name += ',.ContentItem[data-za-detail-view-path-module=QuestionItem]:not(.AnswerItem):not(.PinItem)'} else {name = '.ContentItem[data-za-detail-view-path-module=QuestionItem]:not(.AnswerItem):not(.PinItem)'}} // 关注了XX
  934. if (!name) return
  935. document.querySelectorAll(name).forEach(function(item){blockType_(item);})
  936. } else { // 首页
  937. if (!menu_value('menu_blockTypeVideo') && !menu_value('menu_blockTypeArticle') && !menu_value('menu_blockTypePin')) return
  938. if (menu_value('menu_blockTypeVideo')) document.lastChild.appendChild(document.createElement('style')).textContent = `.Card .ZVideoItem-video, .VideoAnswerPlayer video, nav.TopstoryTabs > a[aria-controls="Topstory-zvideo"] {display: none !important;}`;
  939. name = 'h2.ContentItem-title a:not(.zhihu_e_toQuestion)'
  940. if (menu_value('menu_blockTypePin')) name = 'h2.ContentItem-title a:not(.zhihu_e_toQuestion), .ContentItem.PinItem'
  941. document.querySelectorAll(name).forEach(function(item){blockType_(item);})
  942. }
  943.  
  944. // 后续加载的信息流
  945. const observer = new MutationObserver(mutationsList => {
  946. for (const mutation of mutationsList) {
  947. for (const target of mutation.addedNodes) {
  948. if (target.nodeType != 1) return
  949. if (target.className === "Card SearchResult-Card" && target.dataset.zaDetailViewPathModule === undefined) {
  950. // 移除相关搜索
  951. if (menu_value('menu_blockTypeSearch') && location.pathname === '/search' && location.search.indexOf('type=content') > -1) target.hidden = true;
  952. } else {
  953. blockType_(target.querySelector(name));
  954. }
  955. }
  956. }
  957. });
  958. observer.observe(document, { childList: true, subtree: true });
  959.  
  960. window.addEventListener('urlchange', function(){
  961. addSetInterval_(name);
  962. // 移除相关搜索
  963. if (menu_value('menu_blockTypeSearch') && location.pathname === '/search' && location.search.indexOf('type=content') > -1) setTimeout(function(){document.querySelectorAll('.RelevantQuery').forEach((r)=>{r.parentElement.parentElement.hidden = true});}, 1500)
  964. })
  965.  
  966. function blockType_(titleA) {
  967. if (!titleA) return // 判断是否为真
  968. //console.log(titleA.href)
  969. if (location.pathname === '/search') { // 搜索页
  970. if (location.search.indexOf('type=content') === -1) return // 仅限搜索页的 [综合]
  971. if (titleA.href.indexOf('/zvideo/') > -1 || titleA.href.indexOf('video.zhihu.com') > -1) { // 如果是视频
  972. if (menu_value('menu_blockTypeVideo')) findParentElement(titleA, 'Card').remove();
  973. } else if (titleA.href.indexOf('zhuanlan.zhihu.com') > -1) { // 如果是文章
  974. if (menu_value('menu_blockTypeArticle')) findParentElement(titleA, 'Card SearchResult-Card').hidden = true;
  975. } else if (titleA.href.indexOf('/topic/') > -1) { // 如果是话题
  976. if (menu_value('menu_blockTypeTopic')) findParentElement(titleA, 'Card SearchResult-Card').hidden = true;
  977. } else if (titleA.href.indexOf('/market/') > -1) { // 如果是杂志文章等乱七八糟的
  978. if (menu_value('menu_blockTypeSearch')) findParentElement(titleA, 'Card SearchResult-Card').hidden = true;
  979. }
  980. } else if (location.pathname.indexOf('/question/') > -1) { // 问题页
  981. if (menu_value('menu_blockTypeVideo')) findParentElement(titleA, 'List-item').hidden = true;
  982. } else if (location.pathname.indexOf('/follow') > -1) { // 首页 - 关注
  983. if (type === 'follow') {
  984. if ((menu_value('menu_blockTypeFollowAgree') && titleA.className.indexOf('FeedSource-byline') != -1) || (menu_value('menu_blockTypeFollowQuestion') && titleA.dataset.zaDetailViewPathModule == 'QuestionItem')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isFollow').hidden = true; // 赞同了XX + 关注了XX
  985. }
  986. if (titleA.className == 'ContentItem PinItem' && menu_value('menu_blockTypePin')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isFollow').hidden = true; // 如果是想法
  987. } else { // 首页
  988. if (titleA.className == 'ContentItem PinItem') { // 如果是想法(针对无标题)
  989. if (menu_value('menu_blockTypePin')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;
  990. /*} else if (titleA.href.indexOf('/pin/') > -1) { // 如果是想法
  991. if (menu_value('menu_blockTypePin')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;*/
  992. } else if (titleA.href.indexOf('/zvideo/') > -1 || titleA.href.indexOf('video.zhihu.com') > -1) { // 如果是视频
  993. if (menu_value('menu_blockTypeVideo')) {findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;}
  994. } else if (titleA.href.indexOf('/answer/') > -1) { // 如果是问题(视频回答)
  995. if (findParentElement(titleA, 'ContentItem AnswerItem').querySelector('.VideoAnswerPlayer')) {
  996. if (menu_value('menu_blockTypeVideo')) {findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true; findParentElement(titleA, 'ContentItem AnswerItem').remove();}
  997. }
  998. } else if (titleA.href.indexOf('/education/video-course/') > -1) { // 如果是视频课程
  999. if (menu_value('menu_blockTypeVideo')) {findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;}
  1000. } else if (titleA.href.indexOf('zhuanlan.zhihu.com') > -1) { // 如果是文章
  1001. if (menu_value('menu_blockTypeArticle')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;
  1002. }
  1003. }
  1004. }
  1005.  
  1006. function addSetInterval_(A) {
  1007. let timer = setInterval(function(){
  1008. let aTag = document.querySelectorAll(A);
  1009. if (aTag.length > 0) {
  1010. clearInterval(timer);
  1011. aTag.forEach(function(item){blockType_(item);})
  1012. }
  1013. });
  1014. }
  1015. }
  1016.  
  1017.  
  1018. // 寻找父元素
  1019. function findParentElement(item, className, type = false) {
  1020. if (item.parentElement) {
  1021. //console.log(item.parentElement)
  1022. if (type) { // true = 完全一致,false = 包含即可
  1023. if (item.parentElement.className && item.parentElement.className === className) {
  1024. //console.log(item.parentElement.className)
  1025. return item.parentElement;
  1026. } else {
  1027. let temp = findParentElement(item.parentElement, className, true)
  1028. if (temp) return temp
  1029. }
  1030. } else {
  1031. if (item.parentElement.className && item.parentElement.className.indexOf(className) > -1) {
  1032. return item.parentElement;
  1033. } else {
  1034. let temp = findParentElement(item.parentElement, className)
  1035. if (temp) return temp
  1036. }
  1037. }
  1038. }
  1039. return
  1040. }
  1041.  
  1042.  
  1043. // 移除高亮链接
  1044. function removeHighlightLink() {
  1045. const callback = (mutationsList, observer) => {
  1046. for (const mutation of mutationsList) {
  1047. for (const target of mutation.addedNodes) {
  1048. if (target.nodeType != 1 || target.tagName != 'A') break
  1049. if (target.dataset.zaNotTrackLink && target.href.indexOf('https://zhida.zhihu.com/search?q=') > -1) {
  1050. target.parentElement.replaceWith(target.textContent);
  1051. }
  1052. }
  1053. }
  1054. };
  1055. const observer = new MutationObserver(callback);
  1056. observer.observe(document, { childList: true, subtree: true });
  1057.  
  1058. // 针对的是打开网页后直接加载的前面几个回答(上面哪些是针对动态加载的回答)
  1059. document.querySelectorAll('span > a[data-za-not-track-link][href^="https://zhida.zhihu.com/search?q="]').forEach(e => e.parentElement.replaceWith(e.textContent))
  1060. }
  1061.  
  1062.  
  1063. // 屏蔽盐选内容
  1064. function blockYanXuan() {
  1065. if (!menu_value('menu_blockYanXuan')) return
  1066. const blockYanXuan_question = (mutationsList, observer) => {
  1067. for (const mutation of mutationsList) {
  1068. for (const target of mutation.addedNodes) {
  1069. if (target.nodeType != 1) return
  1070. if (target.className === 'List-item' || target.className === 'Card AnswerCard') {
  1071. if (target.querySelector('.KfeCollection-AnswerTopCard-Container, .KfeCollection-PurchaseBtn')) {
  1072. target.hidden = true;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. };
  1078.  
  1079. const blockYanXuan_question_answer = (mutationsList, observer) => {
  1080. for (const mutation of mutationsList) {
  1081. for (const target of mutation.addedNodes) {
  1082. if (target.nodeType != 1) return
  1083. target.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  1084. if (item.querySelector('.KfeCollection-AnswerTopCard-Container, .KfeCollection-PurchaseBtn')) {
  1085. item.hidden = true;
  1086. }
  1087. })
  1088. }
  1089. }
  1090. };
  1091.  
  1092. if (location.pathname.indexOf('/answer/') > -1) { // 回答页(就是只有三个回答的页面)
  1093. const observer = new MutationObserver(blockYanXuan_question_answer);
  1094. observer.observe(document, { childList: true, subtree: true });
  1095. } else { // 问题页(可以显示所有回答的页面)
  1096. const observer = new MutationObserver(blockYanXuan_question);
  1097. observer.observe(document, { childList: true, subtree: true });
  1098. }
  1099.  
  1100. // 针对的是打开网页后直接加载的前面几个回答(上面哪些是针对动态加载的回答)
  1101. document.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  1102. if (item.querySelector('.KfeCollection-AnswerTopCard-Container, .KfeCollection-PurchaseBtn')) {
  1103. item.hidden = true;
  1104. }
  1105. })
  1106. }
  1107.  
  1108.  
  1109. // 区分问题文章
  1110. function addTypeTips() {
  1111. if (!menu_value('menu_typeTips')) return
  1112. let style = `font-weight: bold;font-size: 13px;padding: 1px 4px 0;border-radius: 2px;display: inline-block;vertical-align: top;margin: ${(location.pathname === '/search') ? '2' : '4'}px 4px 0 0;`
  1113. document.body.appendChild(document.createElement('style')).textContent = `/* 区分问题文章 */
  1114. .AnswerItem .ContentItem-title a:not(.zhihu_e_toQuestion)::before {content:'问题';color: #f68b83;background-color: #f68b8333;${style}}
  1115. .TopstoryQuestionAskItem .ContentItem-title a:not(.zhihu_e_toQuestion)::before {content:'问题';color: #ff5a4e;background-color: #ff5a4e33;${style}}
  1116. .ZVideoItem .ContentItem-title a::before, .ZvideoItem .ContentItem-title a::before {content:'视频';color: #00BCD4;background-color: #00BCD433;${style}}
  1117. .PinItem .ContentItem-title a::before {content:'想法';color: #4CAF50;background-color: #4CAF5033;${style}}
  1118. .ArticleItem .ContentItem-title a::before {content:'文章';color: #2196F3;background-color: #2196F333;${style}}`;
  1119. }
  1120.  
  1121.  
  1122. // 直达问题按钮
  1123. function addToQuestion() {
  1124. if (!menu_value('menu_toQuestion')) return
  1125.  
  1126. // 一开始加载的信息流 + 添加按钮样式
  1127. if (location.pathname === '/search') {
  1128. document.lastChild.appendChild(document.createElement('style')).textContent = `a.zhihu_e_toQuestion {font-size: 13px !important;font-weight: normal !important;padding: 1px 6px 0 !important;border-radius: 2px !important;display: inline-block !important;vertical-align: top !important;height: 20.67px !important;line-height: 20.67px !important;margin-top: 2px !important;}`;
  1129. addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
  1130. } else {
  1131. document.lastChild.appendChild(document.createElement('style')).textContent = `a.zhihu_e_toQuestion {font-size: 13px !important;font-weight: normal !important;padding: 1px 6px 0 !important;border-radius: 2px !important;display: inline-block !important;vertical-align: top !important;margin-top: 4px !important;}`;
  1132. document.querySelectorAll('h2.ContentItem-title a:not(.zhihu_e_tips)').forEach(function(item){addTypeTips_(item);})
  1133. }
  1134.  
  1135. // 后续加载的信息流
  1136. const observer = new MutationObserver(mutationsList => {
  1137. for (const mutation of mutationsList) {
  1138. for (const target of mutation.addedNodes) {
  1139. if (target.nodeType != 1) return
  1140. addTypeTips_(target.querySelector('h2.ContentItem-title a:not(.zhihu_e_tips)'));
  1141. }
  1142. }
  1143. });
  1144. observer.observe(document, { childList: true, subtree: true });
  1145.  
  1146. window.addEventListener('urlchange', function(){
  1147. addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
  1148. })
  1149.  
  1150. function addTypeTips_(titleA) {
  1151. if (!titleA) return // 判断是否为真
  1152. if (titleA.parentElement.querySelector('a.zhihu_e_toQuestion')) return // 判断是否已添加
  1153. if (titleA.textContent.indexOf('?') != -1) { // 把问题末尾英文问好 [?] 的替换为中文问好 [?],这样按钮与标题之间的间距就刚刚好~
  1154. titleA.innerHTML = titleA.innerHTML.replace('?', "?")
  1155. }
  1156. if (/answer\/\d+/.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
  1157. titleA.insertAdjacentHTML('afterend', `<a class="zhihu_e_toQuestion VoteButton" href="${titleA.parentElement.querySelector('meta[itemprop="url"]').content}" target="_blank">直达问题</a>`);
  1158. }
  1159. }
  1160.  
  1161. function addSetInterval_(A) {
  1162. let timer = setInterval(function(){
  1163. let aTag = document.querySelectorAll(A);
  1164. if (aTag.length > 0) {
  1165. clearInterval(timer);
  1166. aTag.forEach(function(item){addTypeTips_(item);})
  1167. }
  1168. });
  1169. }
  1170. }
  1171.  
  1172.  
  1173. // 展开问题描述
  1174. function questionRichTextMore() {
  1175. if (!menu_value('menu_questionRichTextMore')) return
  1176. let button = document.querySelector('button.QuestionRichText-more');
  1177. if (button) button.click()
  1178. }
  1179.  
  1180.  
  1181. // 净化标题消息
  1182. function cleanTitles() {
  1183. if (!menu_value('menu_cleanTitles')) return
  1184.  
  1185. // 方案一
  1186. const elTitle = document.head.querySelector('title');
  1187. const original = elTitle.textContent;
  1188. const observer = new MutationObserver(function() {
  1189. if (elTitle.textContent != original) { // 避免重复执行
  1190. elTitle.textContent = original;
  1191. }
  1192. });
  1193. observer.observe(elTitle, { childList: true });
  1194.  
  1195. // 方案二
  1196. // if (Reflect.getOwnPropertyDescriptor(document, 'title')) {
  1197. // const elTitle = document.head.querySelector('title');
  1198. // const original = elTitle.textContent;
  1199. // const observer = new MutationObserver(function() {
  1200. // if (elTitle.textContent != original) { // 避免重复执行
  1201. // elTitle.textContent = original;
  1202. // }
  1203. // });
  1204. // observer.observe(elTitle, { childList: true });
  1205. // } else {
  1206. // const title = document.title;
  1207. // Reflect.defineProperty(document, 'title', {
  1208. // set: () => {},
  1209. // get: () => title,
  1210. // });
  1211. // }
  1212. }
  1213.  
  1214.  
  1215. // 净化搜索热门
  1216. function cleanSearch() {
  1217. if (!menu_value('menu_cleanSearch')) return
  1218.  
  1219. const el = document.querySelector('.SearchBar-input > input');
  1220. const observer = new MutationObserver((mutationsList, observer) => {
  1221. if (mutationsList[0].attributeName === 'placeholder' && mutationsList[0].target.placeholder != '') mutationsList[0].target.placeholder = '';
  1222. });
  1223. el.placeholder = '';
  1224. observer.observe(el, { attributes: true });
  1225. document.documentElement.appendChild(document.createElement('style')).textContent = '.AutoComplete-group > .SearchBar-label:not(.SearchBar-label--history), .AutoComplete-group > [id^="AutoComplete2-topSearch-"], .AutoComplete-group > [id^="AutoComplete3-topSearch-"] {display: none !important;}';
  1226. }
  1227.  
  1228.  
  1229. // 快捷关闭悬浮评论(监听点击事件,点击网页两侧空白处)
  1230. function closeFloatingComments() {
  1231. const closeFloatingCommentsModal = (mutationsList, observer) => {
  1232. for (const mutation of mutationsList) {
  1233. for (const target of mutation.addedNodes) {
  1234. if (target.nodeType != 1) return
  1235. let button = document.querySelector('button[aria-label="关闭"]');
  1236. if (button) {button.parentElement.parentElement.onclick = function(event){if (event.target.parentElement == this) {button.click();}}}
  1237. }
  1238. }
  1239. };
  1240. const observer = new MutationObserver(closeFloatingCommentsModal);
  1241. observer.observe(document, { childList: true, subtree: true });
  1242. }
  1243.  
  1244.  
  1245. // 监听 XMLHttpRequest 事件
  1246. /*function EventXMLHttpRequest() {
  1247. var _send = window.XMLHttpRequest.prototype.send
  1248. function sendReplacement(data) {
  1249. addTypeTips();
  1250. return _send.apply(this, arguments);
  1251. }
  1252. window.XMLHttpRequest.prototype.send = sendReplacement;
  1253. }*/
  1254.  
  1255.  
  1256. // 自定义 urlchange 事件(用来监听 URL 变化)
  1257. function addUrlChangeEvent() {
  1258. history.pushState = ( f => function pushState(){
  1259. var ret = f.apply(this, arguments);
  1260. window.dispatchEvent(new Event('pushstate'));
  1261. window.dispatchEvent(new Event('urlchange'));
  1262. return ret;
  1263. })(history.pushState);
  1264.  
  1265. history.replaceState = ( f => function replaceState(){
  1266. var ret = f.apply(this, arguments);
  1267. window.dispatchEvent(new Event('replacestate'));
  1268. window.dispatchEvent(new Event('urlchange'));
  1269. return ret;
  1270. })(history.replaceState);
  1271.  
  1272. window.addEventListener('popstate',()=>{
  1273. window.dispatchEvent(new Event('urlchange'))
  1274. });
  1275. }
  1276.  
  1277.  
  1278. function getXpath(xpath, contextNode, doc = document) {
  1279. contextNode = contextNode || doc;
  1280. try {
  1281. const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  1282. // 应该总是返回一个元素节点
  1283. return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
  1284. } catch (err) {
  1285. throw new Error(`无效 Xpath: ${xpath}`);
  1286. }
  1287. }
  1288.  
  1289.  
  1290. // 显示问题作者
  1291. function question_author() {
  1292. if (document.querySelector('.BrandQuestionSymbol, .QuestionAuthor')) return
  1293. let qJson = JSON.parse(document.querySelector('#js-initialData').textContent).initialState.entities.questions[/\d+/.exec(location.pathname)[0]].author,
  1294. html = `<div class="BrandQuestionSymbol"><a class="BrandQuestionSymbol-brandLink" href="/people/${qJson.urlToken}"><img role="presentation" src="${qJson.avatarUrl}" class="BrandQuestionSymbol-logo" alt=""><span class="BrandQuestionSymbol-name">${qJson.name}</span></a><div class="BrandQuestionSymbol-divider" style="margin-left: 5px;margin-right: 10px;"></div></div>`;
  1295. //html = `<div class="QuestionAuthor"><div class="AuthorInfo AuthorInfo--plain" itemprop="author" itemscope="" itemtype="http://schema.org/Person"><div class="AuthorInfo"><span class="UserLink AuthorInfo-avatarWrapper"><div class="Popover"><div id="Popover18-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover18-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="${qJson.urlToken}"><img class="Avatar AuthorInfo-avatar" width="24" height="24" src="${qJson.avatarUrl}"></a></div></div></span><div class="AuthorInfo-content"><div class="AuthorInfo-head"><span class="UserLink AuthorInfo-name"><div class="Popover"><div id="Popover19-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover19-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="${qJson.urlToken}">${qJson.name}</a></div></div></span></div></div></div></div></div>`
  1296. document.querySelector('.QuestionHeader-topics').insertAdjacentHTML('beforebegin', html);
  1297. //document.querySelector('.QuestionPage h1.QuestionHeader-title').insertAdjacentHTML('afterend', html);
  1298. }
  1299.  
  1300.  
  1301. // [完整显示时间 + 置顶显示时间] 功能修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1302. // 完整显示时间 + 置顶显示时间
  1303. function topTime_(css, classs) {
  1304. document.querySelectorAll(css).forEach(function(_this) {
  1305. let t = _this.querySelector('.ContentItem-time'); if (!t) return
  1306. if (!(t.classList.contains('full')) && t.querySelector('span') && t.querySelector('span').textContent != null) {
  1307. // 完整显示时间
  1308. topTime_allTime(t)
  1309. // 发布时间置顶
  1310. topTime_publishTop(t, _this, classs)
  1311. }
  1312. });
  1313. }
  1314.  
  1315.  
  1316. // 完整显示时间 + 置顶显示时间 - 文章
  1317. function topTime_post() {
  1318. let t = document.querySelector('.ContentItem-time:not(.xiu-time)'); if (!t) return
  1319. // 完整显示时间
  1320. if (t.textContent.indexOf('编辑于') > -1 && !(t.classList.contains('xiu-time'))) {
  1321. let tt = t.textContent;
  1322. t.click();
  1323. t.textContent = (t.textContent + ' ,' + tt)
  1324. t.classList.add('xiu-time');
  1325. }
  1326.  
  1327. //发布时间置顶
  1328. if (menu_value('menu_publishTop') && !(document.querySelector('.Post-Header > .ContentItem-time')) && !(document.querySelector('.ContentItem-meta > .ContentItem-time'))) {
  1329. let temp_time = t.cloneNode(true);
  1330. temp_time.style.padding = '0px';
  1331. document.querySelector('.Post-Header').insertAdjacentElement('beforeEnd', temp_time);
  1332. }
  1333. }
  1334.  
  1335.  
  1336. // 完整显示时间
  1337. function topTime_allTime(t) {
  1338. if (t.textContent.indexOf('发布于') > -1 && t.textContent.indexOf('编辑于') == -1) {
  1339. t.querySelector('span').textContent = (t.querySelector('span').dataset.tooltip);
  1340. t.classList.add('full');
  1341. } else if (t.textContent.indexOf('发布于') == -1 && t.textContent.indexOf('编辑于') > -1) {
  1342. t.querySelector('span').textContent = (t.querySelector('span').dataset.tooltip) + ' ,' + (t.querySelector('span').textContent);
  1343. t.classList.add('full');
  1344. }
  1345. }
  1346.  
  1347.  
  1348. // 发布时间置顶
  1349. function topTime_publishTop(t, _this, _class) {
  1350. if (!menu_value('menu_publishTop')) return
  1351. if (!t.parentNode.classList.contains(_class)) {
  1352. let temp_time = t.cloneNode(true);
  1353. temp_time.style.padding = '0px';
  1354. // 对于较短的回答,隐藏回答底部的时间
  1355. if (_this.offsetHeight < 600) t.style.display = 'none';
  1356. _this.querySelector('.' + _class).insertAdjacentElement('beforeEnd', temp_time);
  1357. }
  1358. }
  1359.  
  1360.  
  1361. // 问题创建时间
  1362. function question_time() {
  1363. if (!(document.querySelector('.QuestionPage .QuestionHeader-side .QuestionTime-xiu'))) {
  1364. document.querySelector('.QuestionPage .QuestionHeader-side').insertAdjacentHTML('beforeEnd', '<div class="QuestionTime-xiu" style="color: #9098ac; margin-top: 5px; font-size: 13px; font-style: italic;"><p>创建时间:' + getUTC8(new Date(document.querySelector('.QuestionPage > meta[itemprop=dateCreated]').content)) + '</p><p>最后编辑:' + getUTC8(new Date(document.querySelector('.QuestionPage > meta[itemprop=dateModified]').content)) + '</p></div>');
  1365. }
  1366. }
  1367.  
  1368.  
  1369. // UTC 标准时转 UTC+8 北京时间,修改自:https://greasyfork.org/zh-CN/scripts/402808(精简)
  1370. function getUTC8(t) {
  1371. return (t.getFullYear() + '-' + (((t.getMonth() + 1) < 10) ? ('0' + (t.getMonth() + 1)) : (t.getMonth() + 1)) + '-' + ((t.getDate() < 10) ? ('0' + t.getDate()) : t.getDate()) + '\xa0\xa0' + ((t.getHours() < 10) ? ('0' + t.getHours()) : t.getHours()) + ':' + ((t.getMinutes() < 10) ? ('0' + t.getMinutes()) : t.getMinutes()) + ':' + ((t.getSeconds() < 10) ? ('0' + t.getSeconds()) : t.getSeconds()));
  1372. }
  1373.  
  1374.  
  1375. // 默认高清原图(无水印)
  1376. function originalPic(){
  1377. document.querySelectorAll('img[data-original][data-original-token][data-lazy-status]:not([data-original-xiu]):not(.comment_sticker):not(.Avatar)').forEach(function(one){one.src = 'https://' + one.dataset.original.split('/')[2] + '/' + one.dataset.originalToken + '.webp'; one.dataset.originalXiu = 'true';});
  1378. }
  1379.  
  1380.  
  1381. // 默认站外直链,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1382. function directLink () {
  1383. document.querySelectorAll('a.external[href*="link.zhihu.com/?target="], a.LinkCard[href*="link.zhihu.com/?target="]:not(.MCNLinkCard):not(.ZVideoLinkCard):not(.ADLinkCardContainer)').forEach(function (_this) {_this.href = decodeURIComponent(_this.href.substring(_this.href.indexOf('link.zhihu.com/?target=') + 23));});
  1384. }
  1385.  
  1386.  
  1387. // 默认折叠邀请,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1388. function questionInvitation(){
  1389. let time = setInterval(function(){
  1390. let q = document.querySelector('.QuestionInvitation-content'); if (!q) return
  1391. clearInterval(time);
  1392. q.style.display = 'none';
  1393. document.querySelector('.QuestionInvitation-title').innerHTML = document.querySelector('.QuestionInvitation-title').innerText + '<span style="cursor: pointer; font-size: 14px; color: #919aae;"> 展开/折叠</span>'
  1394. // 点击事件(展开/折叠)
  1395. document.querySelector('.Topbar').onclick = function(){
  1396. let q = document.querySelector('.QuestionInvitation-content')
  1397. if (q.style.display == 'none') {
  1398. q.style.display = ''
  1399. } else {
  1400. q.style.display = 'none'
  1401. }
  1402. }
  1403. });
  1404. }
  1405.  
  1406. // 屏蔽热榜杂项
  1407. function blockHotOther() {
  1408. if (!menu_value('menu_blockTypeLiveHot')) return;
  1409.  
  1410. const isQuestionItem = (hotItem) => {
  1411. const linkItem = hotItem.querySelector('.HotItem-content a');
  1412. if (linkItem === null) return false;
  1413. return /\/question\/\d+/.test(linkItem.href);
  1414. }
  1415.  
  1416. const block = () => {
  1417. removeLiveItems();
  1418. fixItemRank();
  1419. };
  1420.  
  1421. // 移除非问题的内容
  1422. const removeLiveItems = () => {
  1423. const hotItems = document.querySelectorAll('.HotList-list .HotItem');
  1424. for (const item of hotItems) {
  1425. if (!isQuestionItem(item)) item.remove();
  1426. }
  1427. }
  1428.  
  1429. // 修复排行榜序号
  1430. const fixItemRank = () => {
  1431. const hotItems = document.querySelectorAll('.HotList-list .HotItem:not([hidden])');
  1432. hotItems.forEach((item, index) => {
  1433. const rank = item.querySelector('.HotItem-index .HotItem-rank');
  1434. if (rank !== null) rank.innerText = index + 1;
  1435. });
  1436. }
  1437.  
  1438. const blockLive_content = (mutationsList, observer) => {
  1439. for (const mutation of mutationsList) {
  1440. for (const target of mutation.addedNodes) {
  1441. if (target.classList.contains('.HotItem')) {
  1442. block();
  1443. }
  1444. }
  1445. }
  1446. }
  1447.  
  1448. const observer = new MutationObserver(blockLive_content);
  1449. observer.observe(document, { childList: true, subtree: true });
  1450.  
  1451. // 初始移除
  1452. block();
  1453. }
  1454.  
  1455.  
  1456. (function() {
  1457. if (window.onurlchange === undefined) {addUrlChangeEvent();} // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
  1458. window.addEventListener('urlchange', function(){ // 针对的是从单个回答页跳转到完整回答页时
  1459. if (location.pathname.indexOf('question') > -1 && location.pathname.indexOf('waiting') === -1 && location.pathname.indexOf('/answer/') === -1) { // 回答页 //
  1460. setTimeout(function(){
  1461. collapsedNowAnswer('.QuestionPage'); // 收起当前回答 + 快捷返回顶部
  1462. collapsedNowAnswer('.Question-main'); // 收起当前回答 + 快捷返回顶部
  1463. questionRichTextMore(); // 展开问题描述
  1464. blockUsers('question'); // 屏蔽指定用户
  1465. blockYanXuan(); // 屏蔽盐选内容
  1466. }, 300);
  1467. } else if (location.pathname == '/') { // 推荐
  1468. setTimeout(()=>{
  1469. blockUsers('index'); // 屏蔽指定用户
  1470. blockKeywords('index'); // 屏蔽指定关键词
  1471. blockType(); // 屏蔽指定类别(视频/文章等)
  1472. }, 500);
  1473. } else if (location.pathname == '/hot') { // 热榜
  1474. setTimeout(()=>{
  1475. blockKeywords('index'); // 屏蔽指定关键词
  1476. blockHotOther(); // 移除热播杂项
  1477. }, 500);
  1478. } else if (location.pathname == '/follow') { // 关注
  1479. setTimeout(()=>{
  1480. blockKeywords('follow'); // 屏蔽指定关键词
  1481. blockType(); // 屏蔽指定类别(视频/文章等)
  1482. blockType('follow'); // 屏蔽指定类别(赞同了XX/关注了XX等)
  1483. }, 500);
  1484. }
  1485. })
  1486.  
  1487. cleanTitles(); // 净化标题消息,不能延迟执行
  1488. // Violentmonkey 比 Tampermonkey 加载更早,会导致一些元素还没加载,因此需要延迟一会儿
  1489. // Tampermonkey 4.18.0 版本可能需要延迟一会执行
  1490. if (GM_info.scriptHandler === 'Violentmonkey' || (GM_info.scriptHandler === 'Tampermonkey' && parseFloat(GM_info.version.slice(0,4)) >= 4.18)) {
  1491. setTimeout(start, 300);
  1492. } else {
  1493. start();
  1494. }
  1495.  
  1496. function start(){
  1497. removeHighlightLink(); // 移除高亮链接
  1498. originalPic();directLink(); // 先立即执行一次
  1499. setInterval(originalPic,500); // 默认高清原图(无水印)
  1500. setInterval(directLink, 500); // 默认站外直链
  1501. if (location.hostname != 'zhuanlan.zhihu.com') {
  1502. if (location.pathname.indexOf('/column/') === -1) cleanSearch(); //净化搜索热门
  1503. collapsedAnswer(); // 一键收起回答
  1504. }
  1505. closeFloatingComments(); // 快捷关闭悬浮评论(监听点击事件,点击网页两侧空白处)
  1506. blockKeywords('comment'); // 屏蔽指定关键词(评论)
  1507.  
  1508.  
  1509. if (location.pathname.indexOf('question') > -1 && location.href.indexOf('/log') == -1) { // 回答页 //
  1510. if (location.pathname.indexOf('waiting') == -1) {
  1511. collapsedNowAnswer('.QuestionPage'); // 收起当前回答 + 快捷返回顶部
  1512. collapsedNowAnswer('.Question-main'); // 收起当前回答 + 快捷返回顶部
  1513. questionRichTextMore(); // 展开问题描述
  1514. blockUsers('question'); // 屏蔽指定用户
  1515. blockYanXuan(); // 屏蔽盐选内容
  1516. blockType('question'); // 屏蔽指定类别(视频/文章等)
  1517. defaultCollapsedAnswer(); // 默认收起回答
  1518. }
  1519. setInterval(function(){topTime_('.ContentItem.AnswerItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1520. setTimeout(function(){question_time(); question_author()}, 100); //问题创建时间 + 显示问题作者
  1521. questionInvitation(); // 默认折叠邀请
  1522.  
  1523. } else if (location.pathname === '/search') { // 搜索结果页 //
  1524. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1525. collapsedNowAnswer('.Search-container'); // 收起当前回答 + 快捷返回顶部
  1526. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'SearchItem-meta')}, 300); // 置顶显示时间
  1527. addTypeTips(); // 区分问题文章
  1528. addToQuestion(); // 直达问题按钮
  1529. blockUsers('search'); // 屏蔽指定用户
  1530. blockKeywords('search'); // 屏蔽指定关键词
  1531. blockType('search'); // 屏蔽指定类别(视频/文章等)
  1532.  
  1533.  
  1534. } else if (location.pathname.indexOf('/topic/') > -1) { // 话题页 //
  1535. if (location.pathname.indexOf('/hot') > -1 || location.href.indexOf('/top-answers') > -1) { // 仅限 [讨论] [精华]
  1536. collapsedNowAnswer('main.App-main'); // 收起当前回答 + 快捷返回顶部
  1537. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1538. addTypeTips(); // 区分问题文章
  1539. addToQuestion(); // 直达问题按钮
  1540. blockUsers('topic'); // 屏蔽指定用户
  1541. blockKeywords('topic'); // 屏蔽指定关键词
  1542. }
  1543.  
  1544. } else if (location.hostname === 'zhuanlan.zhihu.com'){ // 文章 //
  1545. backToTop('article.Post-Main.Post-NormalMain'); // 快捷返回顶部
  1546. backToTop('div.Post-Sub.Post-NormalSub'); // 快捷返回顶部
  1547. setTimeout(topTime_post, 300); // 置顶显示时间
  1548. blockUsers(); // 屏蔽指定用户
  1549.  
  1550.  
  1551. } else if (location.pathname.indexOf('/column/') > -1) { // 专栏 //
  1552. setTimeout(function(){
  1553. collapsedAnswer(); // 一键收起回答
  1554. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1555. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1556. blockUsers(); // 屏蔽指定用户
  1557. }, 300);
  1558.  
  1559.  
  1560. } else if (location.pathname.indexOf('/people/') > -1 || location.href.indexOf('org') > -1) { // 用户主页 //
  1561. if (location.pathname.split('/').length === 3) addTypeTips();addToQuestion(); // 区分问题文章、直达问题按钮
  1562. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1563. collapsedNowAnswer('.Profile-main'); // 收起当前回答 + 快捷返回顶部
  1564. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1565. blockUsers('people'); // 屏蔽指定用户
  1566. blockKeywords('people'); // 屏蔽指定关键词
  1567.  
  1568.  
  1569. } else if (location.pathname.indexOf('/collection/') > -1) { // 收藏夹 //
  1570. addTypeTips(); // 区分问题文章
  1571. addToQuestion(); // 直达问题按钮
  1572. collapsedNowAnswer('main'); // 收起当前回答 + 快捷返回顶部
  1573. collapsedNowAnswer('.CollectionsDetailPage'); // 收起当前回答 + 快捷返回顶部
  1574. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1575. blockKeywords('collection'); // 屏蔽指定关键词
  1576.  
  1577. } else if (location.pathname.indexOf('/pin/') > -1) { // 想法 //
  1578. backToTop('main[role=main]'); // 快捷返回顶部
  1579. setInterval(function(){topTime_('.ContentItem.PinItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1580.  
  1581. } else { // 首页 //
  1582. // 解决屏蔽类别后,因为首页信息流太少而没有滚动条导致无法加载更多内容的问题
  1583. document.lastElementChild.appendChild(document.createElement('style')).textContent = '.Topstory-container {min-height: 1500px;}';
  1584. if (menu_value('menu_blockTypeVideo')) document.lastChild.appendChild(document.createElement('style')).textContent = `.Card .ZVideoItem-video, nav.TopstoryTabs > a[aria-controls="Topstory-zvideo"] {display: none !important;}`;
  1585.  
  1586. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1587. collapsedNowAnswer('.Topstory-container'); // 收起当前回答 + 快捷返回顶部
  1588. setInterval(function(){topTime_('.TopstoryItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1589. addTypeTips(); // 区分问题文章
  1590. addToQuestion(); // 直达问题按钮
  1591. if (location.pathname == '/') { // 推荐
  1592. blockUsers('index'); // 屏蔽指定用户
  1593. blockKeywords('index'); // 屏蔽指定关键词
  1594. blockType(); // 屏蔽指定类别(视频/文章等)
  1595. } else if (location.pathname == '/hot') { // 热榜
  1596. blockKeywords('index'); // 屏蔽指定关键词
  1597. blockHotOther(); // 屏蔽热榜杂项
  1598. } else if (location.pathname == '/follow') { // 关注
  1599. blockKeywords('follow'); // 屏蔽指定关键词
  1600. blockType(); // 屏蔽指定类别(视频/文章等)
  1601. blockType('follow'); // 屏蔽指定类别(赞同了XX/关注了XX等)
  1602. } else {
  1603. blockUsers();
  1604. }
  1605. }
  1606. }
  1607. })();