Greasy Fork镜像 支持简体中文。

知乎增強

移除登錄彈窗、屏蔽首頁視頻、默認收起回答、快捷收起回答/評論(左鍵兩側)、快捷回到頂部(右鍵兩側)、屏蔽用戶、屏蔽關鍵詞、移除高亮鏈接、屏蔽鹽選內容、淨化標題消息、展開問題描述、顯示問題作者、置頂顯示時間、完整問題時間、區分問題文章、直達問題按鈕、默認高清原圖、默認站外直鏈

目前為 2022-02-20 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name 知乎增强
  3. // @name:en Zhihu enhancement
  4. // @name:zh-CN 知乎增强
  5. // @name:zh-TW 知乎增強
  6. // @version 2.0.0
  7. // @author X.I.U
  8. // @description 移除登录(不可用)弹窗、屏蔽首页视频、默认收起回答、快捷收起回答/评论(左键两侧)、快捷回到顶部(右键两侧)、屏蔽用户、屏蔽关键词、移除高亮链接、屏蔽盐选内容、净化标题消息、净化搜索热门、展开问题描述、显示问题作者、置顶显示时间、完整问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
  9. // @description:en Remove the login popup, block the homepage video, close the answer by default, quickly close the answer/comment (both sides of the left button), quickly return to the top (both sides of the right button), block users, block keywords, remove highlighted links , Shield salt selection, purify title message, expand problem description, display problem author, display time on top, complete problem time, distinguish problem article, direct problem button, default high-definition original image, default off-site direct link
  10. // @description:zh-CN 移除登录(不可用)弹窗、屏蔽首页视频、默认收起回答、快捷收起回答/评论(左键两侧)、快捷回到顶部(右键两侧)、屏蔽用户、屏蔽关键词、移除高亮链接、屏蔽盐选内容、净化标题消息、展开问题描述、显示问题作者、置顶显示时间、完整问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
  11. // @description:zh-TW 移除登錄彈窗、屏蔽首頁視頻、默認收起回答、快捷收起回答/評論(左鍵兩側)、快捷回到頂部(右鍵兩側)、屏蔽用戶、屏蔽關鍵詞、移除高亮鏈接、屏蔽鹽選內容、淨化標題消息、展開問題描述、顯示問題作者、置頂顯示時間、完整問題時間、區分問題文章、直達問題按鈕、默認高清原圖、默認站外直鏈
  12. // @match *://www.zhihu.com/*
  13. // @match *://zhuanlan.zhihu.com/*
  14. // @icon https://static.zhihu.com/heifetz/favicon.ico
  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. // @license GPL-3.0 License
  25. // @run-at document-end
  26. // @incompatible safari
  27. // @namespace https://gf.qytechs.cn/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_customBlockKeywords', '自定义屏蔽关键词', '自定义屏蔽关键词', []],
  42. ['menu_blockType', '屏蔽指定类别 (视频/文章等)', '勾选 = 屏蔽该类别的信息流', ''],
  43. ['menu_blockTypeVideo', '视频 [首页、搜索页、问题页]', '视频(首页、搜索页、问题页)', true],
  44. ['menu_blockTypeArticle', '文章 [首页、搜索页]', '文章(首页、搜索页)', false],
  45. ['menu_blockTypeTopic', '话题 [搜索页]', '话题(搜索页)', false],
  46. ['menu_blockTypeSearch', '杂志文章、相关搜索等 [搜索页]', '相关搜索、杂志等(搜索页)', false],
  47. ['menu_blockYanXuan', '屏蔽盐选内容', '屏蔽盐选内容', false],
  48. ['menu_cleanTitles', '净化标题消息 (标题中的私信/消息)', '净化标题提醒', false],
  49. ['menu_cleanSearch', '净化搜索热门 (默认搜索词及热门搜索)', '净化搜索热门', false],
  50. ['menu_questionRichTextMore', '展开问题描述', '展开问题描述', false],
  51. ['menu_publishTop', '置顶显示时间', '置顶显示时间', true],
  52. ['menu_typeTips', '区分问题文章', '区分问题文章', true],
  53. ['menu_toQuestion', '直达问题按钮', '直达问题按钮', true]
  54. ], menu_ID = [];
  55. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  56. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  57. }
  58. registerMenuCommand();
  59.  
  60. // 注册(不可用)脚本菜单
  61. function registerMenuCommand() {
  62. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  63. for (let i=0;i<menu_ID.length;i++){
  64. GM_unregisterMenuCommand(menu_ID[i]);
  65. }
  66. }
  67. for (let i=0;i<menu_ALL.length;i++){ // 循环注册(不可用)脚本菜单
  68. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  69. if (menu_ALL[i][0] === 'menu_customBlockUsers') {
  70. if (menu_value('menu_blockUsers')) menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){customBlockUsers()});
  71. } else if (menu_ALL[i][0] === 'menu_customBlockKeywords') {
  72. if (menu_value('menu_blockKeywords')) menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){customBlockKeywords()});
  73. } else if (menu_ALL[i][0] === 'menu_blockType') {
  74. 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]])});
  75. } else if (menu_ALL[i][0] != 'menu_blockTypeVideo' && menu_ALL[i][0] != 'menu_blockTypeArticle' && menu_ALL[i][0] != 'menu_blockTypeTopic' && menu_ALL[i][0] != 'menu_blockTypeSearch') {
  76. 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]}`)});
  77. }
  78. }
  79. 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://gf.qytechs.cn/zh-CN/scripts/419081/feedback', {active: true,insert: true,setParent: true});});
  80. }
  81.  
  82.  
  83. // 菜单开关
  84. function menu_switch(menu_status, Name, Tips) {
  85. if (menu_status == 'true'){
  86. GM_setValue(`${Name}`, false);
  87. GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  88. }else{
  89. GM_setValue(`${Name}`, true);
  90. GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  91. }
  92. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  93. };
  94.  
  95.  
  96. // 返回菜单值
  97. function menu_value(menuName) {
  98. for (let menu of menu_ALL) {
  99. if (menu[0] == menuName) {
  100. return menu[3]
  101. }
  102. }
  103. }
  104.  
  105.  
  106. // 脚本设置
  107. function menu_setting(type, title, tips, line, menu) {
  108. 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;}
  109. .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;}
  110. .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;}
  111. .zhihuE_SettingRoot .zhihuE_SettingHeader {padding: 10px 20px;color: #fff;font-weight: bold;background-color: #3994ff;border-radius: 3px 3px 0 0;}
  112. .zhihuE_SettingRoot .zhihuE_SettingMain {padding: 10px 20px;border-radius: 0 0 3px 3px;}
  113. .zhihuE_SettingHeader span {float: right;cursor: pointer;}
  114. .zhihuE_SettingMain input {margin: 10px 6px 10px 0;cursor: pointer;vertical-align:middle}
  115. .zhihuE_SettingMain label {margin-right: 20px;user-select: none;cursor: pointer;vertical-align:middle}
  116. .zhihuE_SettingMain hr {border: 0.5px solid #f4f4f4;}
  117. [data-theme="dark"] .zhihuE_SettingRoot {color: #adbac7;background-color: #343A44;}
  118. [data-theme="dark"] .zhihuE_SettingHeader {color: #d0d0d0;background-color: #2D333B;}
  119. [data-theme="dark"] .zhihuE_SettingMain hr {border: 0.5px solid #2d333b;}</style>
  120. <div class="zhihuE_SettingBackdrop_1"><div class="zhihuE_SettingBackdrop_2"></div><div class="zhihuE_SettingRoot">
  121. <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>
  122. <div class="zhihuE_SettingMain"><p>${tips}</p><hr>`
  123. if (line) _br = '<br>'
  124. for (let i=0; i<menu.length; i++) {
  125. if (GM_getValue(menu[i][0])) {
  126. _html += `<label><input name="zhihuE_Setting" type="checkbox" value="${menu[i][0]}" checked="checked">${menu[i][1]}</label>${_br}`
  127. } else {
  128. _html += `<label><input name="zhihuE_Setting" type="checkbox" value="${menu[i][0]}">${menu[i][1]}</label>${_br}`
  129. }
  130. }
  131. _html += `</div></div></div>`
  132. document.body.insertAdjacentHTML('beforeend', _html); // 插入网页末尾
  133. setTimeout(function() { // 延迟 100 毫秒,避免太快
  134. // 关闭按钮 点击事件
  135. document.querySelector('.zhihuE_SettingClose').onclick = function(){this.parentElement.parentElement.parentElement.remove();document.querySelector('.zhihuE_SettingStyle').remove();}
  136. // 点击周围空白处 = 点击关闭按钮
  137. document.querySelector('.zhihuE_SettingBackdrop_2').onclick = function(event){if (event.target == this) {document.querySelector('.zhihuE_SettingClose').click();};}
  138. // 复选框 点击事件
  139. document.getElementsByName('zhihuE_Setting').forEach(function (checkBox) {
  140. checkBox.addEventListener('click', function(){if (this.checked) {GM_setValue(this.value, true);} else {GM_setValue(this.value, false);}});
  141. })
  142. }, 100)
  143. }
  144.  
  145.  
  146. // 添加收起回答观察器
  147. function getCollapsedAnswerObserver() {
  148. if (!window._collapsedAnswerObserver) {
  149. const observer = new MutationObserver(mutations => {
  150. for (const mutation of mutations) {
  151. if (mutation.target.hasAttribute('script-collapsed')) return
  152. // 短的回答
  153. if (mutation.target.classList.contains('RichContent')) {
  154. for (const addedNode of mutation.addedNodes) {
  155. if (addedNode.nodeType != Node.ELEMENT_NODE) continue
  156. if (addedNode.className != 'RichContent-inner') continue
  157. if (addedNode.offsetHeight < 400) break
  158. //console.log('111',addedNode, addedNode.classList, addedNode.classList.contains('RichContent-inner'), addedNode.offsetHeight, addedNode.textContent.length)
  159. const button = mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]');
  160. if (button) {
  161. mutation.target.setAttribute('script-collapsed', '');
  162. button.click();
  163. return
  164. }
  165. }
  166. // 长的回答
  167. } else if (mutation.target.tagName === 'DIV' && !mutation.target.style.cssText && !mutation.target.className) {
  168. if (mutation.target.parentElement.hasAttribute('script-collapsed')) return
  169. //console.log('222',mutation.target, mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]'))
  170. const button = mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]');
  171. if (button) {
  172. mutation.target.parentElement.setAttribute('script-collapsed', '');
  173. button.click();
  174. return
  175. }
  176. }
  177. }
  178. })
  179.  
  180. observer.start = function() {
  181. if (!this._active) {
  182. this.observe(document, { childList: true, subtree: true });
  183. this._active = true;
  184. }
  185. }
  186. observer.end = function() {
  187. if (this._active) {
  188. this.disconnect();
  189. }
  190. }
  191.  
  192. window.addEventListener('urlchange', function() {
  193. observer[location.href.indexOf('/answer/') === -1 ? 'start' : 'end']();
  194. })
  195. window._collapsedAnswerObserver = observer;
  196. }
  197. return window._collapsedAnswerObserver
  198. }
  199.  
  200.  
  201. // 默认收起回答
  202. function defaultCollapsedAnswer() {
  203. if (!menu_value('menu_defaultCollapsedAnswer')) return
  204. const observer = getCollapsedAnswerObserver();
  205. if (location.href.indexOf('/answer/') === -1) {
  206. observer.start();
  207. }
  208. }
  209.  
  210.  
  211. // 一键收起回答(全部)
  212. function collapsedAnswer() {
  213. if (!menu_value('menu_collapsedAnswer')) return
  214. //console.log('1111', document.querySelector('.CornerAnimayedFlex'))
  215. if (document.querySelector('.CornerAnimayedFlex') && !document.getElementById('collapsed-button')) {
  216. //console.log('2222')
  217. document.head.appendChild(document.createElement('style')).textContent = '.CornerButton{margin-bottom:8px !important;}.CornerButtons{bottom:45px !important;}';
  218. 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="Button CornerButton Button--plain"><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.getElementById('collapsed-button').onclick = function () {
  220. if (location.pathname === '/' || location.pathname === '/hot' || location.pathname === '/follow') {
  221. document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) {
  222. if (el.hasAttribute('data-zop-retract-question')) {
  223. el.click()
  224. }
  225. });
  226. } else {
  227. document.querySelectorAll('[script-collapsed]').forEach(function(scriptCollapsed) {
  228. scriptCollapsed.querySelectorAll('.ContentItem-actions [data-zop-retract-question], .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) {
  229. button.click();
  230. })
  231. })
  232. document.querySelectorAll('.RichContent:not([script-collapsed]) .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) {
  233. let el = button.parentElement;
  234. while (!el.classList.contains('RichContent')) {
  235. el = el.parentElement;
  236. }
  237. if (el) {
  238. el.setAttribute('script-collapsed', '');
  239. }
  240. button.click();
  241. })
  242. const observer = getCollapsedAnswerObserver();
  243. observer.start();
  244. if (!menu_value('menu_defaultCollapsedAnswer') && !observer._disconnectListener) {
  245. window.addEventListener('urlchange', function() {
  246. observer.end();
  247. window._collapsedAnswerObserver = null;
  248. })
  249. observer._disconnectListener = true;
  250. }
  251. }
  252. }
  253. }
  254. }
  255.  
  256.  
  257. // 收起当前回答、评论(监听点击事件,点击网页两侧空白处)
  258. function collapsedNowAnswer(selectors) {
  259. backToTop(selectors) // 快捷回到顶部
  260. if (!menu_value('menu_collapsedNowAnswer')) return
  261. document.querySelector(selectors).onclick = function(event){
  262. if (event.target == this) {
  263. // 下面这段主要是 [收起回答],顺便 [收起评论](如果展开了的话)
  264. let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom')
  265. // 悬浮在底部的 [收起回答](此时正在浏览回答内容 [中间区域])
  266. if (rightButton) {
  267. // 固定的 [收起评论](先看看是否展开评论)
  268. let commentCollapseButton = rightButton.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  269. //console.log('111')
  270. if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
  271. // 再去收起回答
  272. rightButton = rightButton.querySelector('.ContentItem-rightButton[data-zop-retract-question]')
  273. //console.log('222')
  274. if (rightButton) rightButton.click();
  275. // 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域])
  276. } else {
  277. let answerCollapseButton_ = false;
  278. for (let el of document.querySelectorAll('.ContentItem-rightButton[data-zop-retract-question]')) { // 遍历所有回答底部的 [收起] 按钮
  279. if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内
  280. // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
  281. let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  282. // 如果展开了评论,就收起评论
  283. //console.log('333')
  284. if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
  285. //console.log('444')
  286. el.click() // 再去收起回答
  287. answerCollapseButton_ = true; // 如果找到并点击收起了,就没必要执行下面的代码了(可视区域中没有 [收起回答] 时)
  288. break
  289. }
  290. }
  291. // 针对完全看不到 [收起回答] 按钮时(如 [头部区域],以及部分明明很长却不显示悬浮横条的回答)
  292. if (!answerCollapseButton_) {
  293. for (let el of document.querySelectorAll('.List-item, .Card.AnswerCard')) { // 遍历所有回答主体元素
  294. if (isElementInViewport_(el)) { // 判断该回答是否在可视区域内
  295. // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
  296. let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  297. // 如果展开了评论,就收起评论
  298. //console.log('555')
  299. if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
  300. let answerCollapseButton__ = el.querySelector('.ContentItem-rightButton[data-zop-retract-question]');
  301. //console.log('666')
  302. if (answerCollapseButton__) answerCollapseButton__.click() // 再去收起回答
  303. break
  304. }
  305. }
  306. }
  307. }
  308.  
  309. // 下面这段只针对 [收起评论](如果展开了的话)
  310. let commentCollapseButton_ = false, commentCollapseButton__ = false;
  311. // 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域])
  312. let commentCollapseButton = document.querySelector('.CommentCollapseButton')
  313. if (commentCollapseButton) {
  314. //console.log('777')
  315. commentCollapseButton.click();
  316. } else { // 固定的 [收起评论](此时正在浏览评论内容 [头部区域])
  317. 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')
  318. if (commentCollapseButton_1.length > 0) {
  319. for (let el of commentCollapseButton_1) {
  320. if (el.textContent.indexOf('收起评论') > -1) {
  321. if (isElementInViewport(el)) {
  322. //console.log('888')
  323. el.click()
  324. commentCollapseButton_ = true // 如果找到并点击了,就没必要执行下面的代码了(可视区域中没有 [收起评论] 时)
  325. break
  326. }
  327. }
  328. }
  329. }
  330. if (commentCollapseButton_ == false) { // 可视区域中没有 [收起评论] 时(此时正在浏览评论内容 [头部区域] + [尾部区域](不上不下的,既看不到固定的 [收起评论] 又看不到悬浮的 [收起评论])),需要判断可视区域中是否存在评论元素
  331. let commentCollapseButton_1 = document.querySelectorAll('.NestComment')
  332. if (commentCollapseButton_1.length > 0) {
  333. for (let el of commentCollapseButton_1) {
  334. if (isElementInViewport(el)) {
  335. let commentCollapseButton = findParentElement(el, 'ContentItem AnswerItem').querySelector('.ContentItem-actions > button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  336. if (commentCollapseButton.textContent.indexOf('收起评论') > -1) {
  337. //console.log('999')
  338. commentCollapseButton.click()
  339. commentCollapseButton__ = true // 如果找到并点击了,就没必要执行下面的代码了(可视区域中没有 评论元素 时)
  340. break
  341. }
  342. }
  343. }
  344. }
  345. if (commentCollapseButton__ == false) { // 如果上面的都没找到,那么就尝试寻找评论末尾的 [评论回复框]
  346. let commentCollapseButton_2 = document.querySelectorAll('.CommentsV2-footer.CommentEditorV2--normal .CommentEditorV2-inputWrap')
  347. if (commentCollapseButton_2.length > 0) {
  348. for (let el of commentCollapseButton_2) {
  349. if (isElementInViewport(el)) {
  350. let commentCollapseButton = findParentElement(el, 'ContentItem AnswerItem').querySelector('.ContentItem-actions > button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
  351. //console.log(commentCollapseButton)
  352. if (commentCollapseButton.textContent.indexOf('收起评论') > -1) {
  353. //console.log('101010')
  354. commentCollapseButton.click()
  355. break
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366.  
  367.  
  368. // 回到顶部(监听点击事件,鼠标右键点击网页两侧空白处)
  369. function backToTop(selectors) {
  370. if (!menu_value('menu_backToTop')) return
  371. document.querySelector(selectors).oncontextmenu = function(event){
  372. if (event.target == this) {
  373. event.preventDefault();
  374. window.scrollTo(0,0)
  375. }
  376. }
  377. }
  378.  
  379.  
  380. //获取元素是否在可视区域(完全可见)
  381. function isElementInViewport(el) {
  382. let rect = el.getBoundingClientRect();
  383. return (
  384. rect.top >= 0 &&
  385. rect.left >= 0 &&
  386. rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
  387. rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  388. );
  389. }
  390. //获取元素是否在可视区域(部分可见)
  391. function isElementInViewport_(el) {
  392. let rect = el.getBoundingClientRect();
  393. return (
  394. rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
  395. rect.left <= (window.innerWidth || document.documentElement.clientWidth)
  396. );
  397. }
  398.  
  399.  
  400. // 自定义屏蔽用户
  401. function customBlockUsers() {
  402. let nowBlockUsers = '';
  403. menu_value('menu_customBlockUsers').forEach(function(item){nowBlockUsers += '|' + item})
  404. //console.log(nowBlockUsers.replace('|',''))
  405. let newBlockUsers = prompt('编辑 [自定义屏蔽用户]\n(不同用户名之间使用 "|" 分隔,例如:用户A|用户B|用户C )', nowBlockUsers.replace('|',''));
  406. if (newBlockUsers === '') {
  407. GM_setValue('menu_customBlockUsers', []);
  408. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  409. } else if (newBlockUsers != null) {
  410. GM_setValue('menu_customBlockUsers', newBlockUsers.split('|'));
  411. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  412. }
  413. };
  414.  
  415.  
  416. // 屏蔽指定用户
  417. function blockUsers(type) {
  418. if (!menu_value('menu_blockUsers')) return
  419. if (!menu_value('menu_customBlockUsers') || menu_value('menu_customBlockUsers').length < 1) return
  420. switch(type) {
  421. case 'index':
  422. blockUsers_('.Card.TopstoryItem.TopstoryItem-isRecommend', 'Card TopstoryItem TopstoryItem-isRecommend');
  423. break;
  424. case 'question':
  425. blockUsers_question();
  426. break;
  427. case 'search':
  428. blockUsers_search();
  429. break;
  430. case 'topic':
  431. blockUsers_('.List-item.TopicFeedItem', 'List-item TopicFeedItem');
  432. break;
  433. case 'people':
  434. blockUsers_button_people(); // 添加屏蔽用户按钮(用户主页)
  435. break;
  436. }
  437. blockUsers_comment(); // 评论区
  438. blockUsers_button(); // 加入黑名单按钮
  439.  
  440. function blockUsers_(className1, className2) {
  441. // 前几条因为是直接加载的,而不是动态插入网页的,所以需要单独判断
  442. function blockKeywords_now() {
  443. document.querySelectorAll(className1).forEach(function(item1){
  444. let item = item1.querySelector('.ContentItem.AnswerItem, .ContentItem.ArticleItem'); // 用户名所在元素
  445. if (item) {
  446. for (const keyword of menu_value('menu_customBlockUsers')) { // 遍历用户名黑名单
  447. if (keyword != '' && item.dataset.zop.indexOf('authorName":"' + keyword + '",') > -1) { // 找到就删除该信息流
  448. console.log(item.dataset.zop);
  449. item1.hidden = true;
  450. break;
  451. }
  452. }
  453. }
  454. })
  455. }
  456.  
  457. blockKeywords_now();
  458. window.addEventListener('urlchange', function(){
  459. setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行
  460. })
  461.  
  462. // 这个是监听网页插入事件,用来判断后续网页动态插入的元素
  463. const callback = (mutationsList, observer) => {
  464. for (const mutation of mutationsList) {
  465. for (const target of mutation.addedNodes) {
  466. if (target.nodeType != 1) return
  467. if (target.className === className2) {
  468. let item = target.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. target.hidden = true;
  474. break;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. };
  482. const observer = new MutationObserver(callback);
  483. observer.observe(document, { childList: true, subtree: true });
  484. }
  485.  
  486.  
  487. function blockUsers_question() {
  488. const blockUsers_question_ = (mutationsList, observer) => {
  489. for (const mutation of mutationsList) {
  490. for (const target of mutation.addedNodes) {
  491. if (target.nodeType != 1) return
  492. if (target.className === 'List-item' || target.className === 'Card AnswerCard') {
  493. let item1 = target.querySelector('.ContentItem.AnswerItem');
  494. if (item1) {
  495. menu_value('menu_customBlockUsers').forEach(function(item2){ // 遍历用户黑名单
  496. if (item1.dataset.zop.indexOf('authorName":"' + item2 + '",') > -1) { // 找到就删除该回答
  497. console.log(item1.dataset.zop)
  498. target.hidden = true;
  499. }
  500. })
  501. }
  502. }
  503. }
  504. }
  505. };
  506.  
  507. const blockUsers_question_answer_ = (mutationsList, observer) => {
  508. for (const mutation of mutationsList) {
  509. for (const target of mutation.addedNodes) {
  510. if (target.nodeType != 1) return
  511. target.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  512. let item1 = item.querySelector('.ContentItem.AnswerItem');
  513. if (item1) {
  514. menu_value('menu_customBlockUsers').forEach(function(item2){ // 遍历用户黑名单
  515. if (item1.dataset.zop.indexOf('authorName":"' + item2 + '",') > -1) { // 找到就删除该回答
  516. console.log(item1.dataset.zop)
  517. item.hidden = true;
  518. }
  519. })
  520. }
  521. })
  522. }
  523. }
  524. };
  525.  
  526. if (location.pathname.indexOf('/answer/') > -1) { // 回答页(就是只有三个回答的页面)
  527. const observer = new MutationObserver(blockUsers_question_answer_);
  528. observer.observe(document, { childList: true, subtree: true });
  529. } else { // 问题页(可以显示所有回答的页面)
  530. const observer = new MutationObserver(blockUsers_question_);
  531. observer.observe(document, { childList: true, subtree: true });
  532. }
  533.  
  534. // 针对的是打开网页后直接加载的前面几个回答(上面哪些是针对动态加载的回答)
  535. document.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. function blockUsers_search() {
  549. function blockUsers_now() {
  550. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  551. 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){
  552. let item = item1.querySelector('.RichText.ztext.CopyrightRichText-richText b'); // 标题所在元素
  553. if (item) {
  554. for (const keyword of menu_value('menu_customBlockUsers')) { // 遍历关键词黑名单
  555. if (keyword != '' && item.textContent === keyword) { // 找到就删除该信息流
  556. console.log(item.textContent);
  557. item1.hidden = true;
  558. break;
  559. }
  560. }
  561. }
  562. })
  563. }
  564.  
  565. setTimeout(blockUsers_now, 500);
  566. window.addEventListener('urlchange', function(){
  567. setTimeout(blockUsers_now, 500); // 网页 URL 变化后再次执行
  568. })
  569.  
  570. const callback = (mutationsList, observer) => {
  571. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  572. for (const mutation of mutationsList) {
  573. for (const target of mutation.addedNodes) {
  574. if (target.nodeType != 1) return
  575. if (target.className === 'Card SearchResult-Card' && (target.dataset.zaDetailViewPathModule === 'AnswerItem' || target.dataset.zaDetailViewPathModule === 'PostItem')) {
  576. let item = target.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. target.hidden = true;
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. };
  590. const observer = new MutationObserver(callback);
  591. observer.observe(document, { childList: true, subtree: true });
  592. }
  593.  
  594. function blockUsers_comment() {
  595. const callback = (mutationsList, observer) => {
  596. for (const mutation of mutationsList) {
  597. for (const target of mutation.addedNodes) {
  598. if (target.nodeType != 1) return
  599. let item = target.querySelector('img.Avatar.UserLink-avatar')
  600. if (item) {
  601. menu_value('menu_customBlockUsers').forEach(function(item1){ // 遍历用户黑名单
  602. if (item.alt === item1) { // 找到就删除该搜索结果
  603. if (findParentElement(item, 'NestComment--rootComment', true)) {
  604. findParentElement(item, 'NestComment--rootComment', true).hidden = true;;
  605. } else if (findParentElement(item, 'NestComment--child', true)){
  606. findParentElement(item, 'NestComment--child', true).hidden = true;;
  607. } else if (findParentElement(item, 'NestComment', true)){
  608. findParentElement(item, 'NestComment', true).hidden = true;;
  609. } else if (findParentElement(item, 'CommentItemV2', true)){
  610. findParentElement(item, 'CommentItemV2', true).hidden = true;;
  611. } else if (findParentElement(item, 'CommentItemV2 CommentItemV2--highlighted', true)){
  612. findParentElement(item, 'CommentItemV2 CommentItemV2--highlighted', true).hidden = true;;
  613. }
  614. }
  615. })
  616.  
  617. // 添加屏蔽用户按钮(点赞、回复等按钮后面)
  618. if (item) {
  619. let footer = findParentElement(item, 'CommentItemV2-meta', true).parentElement.querySelector('.CommentItemV2-metaSibling > .CommentItemV2-footer'),
  620. userid = item.parentElement;
  621. if (userid && footer && !footer.lastElementChild.dataset.name) {
  622. userid = userid.href.split('/')[4];
  623. 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>`);
  624. footer.lastElementChild.onclick = function(){blockUsers_button_add(this.dataset.name, this.dataset.userid, false)}
  625. }
  626. }
  627. }
  628. }
  629. }
  630. };
  631. const observer = new MutationObserver(callback);
  632. observer.observe(document, { childList: true, subtree: true });
  633. }
  634.  
  635. // 添加屏蔽用户按钮(用户信息悬浮框中)
  636. function blockUsers_button() {
  637. const callback = (mutationsList, observer) => {
  638. for (const mutation of mutationsList) {
  639. for (const target of mutation.addedNodes) {
  640. if (target.nodeType != 1) return
  641. //console.log(target, target.className)
  642. if (target.className && (target.className.indexOf('Popover-content Popover-content--top HoverCard-popoverTarget') > -1 || target.className.indexOf('Popover-content Popover-content--bottom HoverCard-popoverTarget') > -1) || target.querySelector('.Popover-content.Popover-content--top.HoverCard-popoverTarget') || target.querySelector('.Popover-content.Popover-content--bottom.HoverCard-popoverTarget')) {
  643. let item = target.querySelector('.MemberButtonGroup.ProfileButtonGroup.HoverCard-buttons'),
  644. item1 = target.querySelector('a.UserLink-link'),
  645. name = item1.textContent,
  646. userid = item1.href.split('/')[4];
  647. if (item && !target.querySelector('button[data-name][data-userid]')) {
  648. 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 class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><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>`);
  649. item.lastElementChild.onclick = function(){blockUsers_button_add(this.dataset.name, this.dataset.userid, false)}
  650. }
  651. }
  652. }
  653. }
  654. };
  655. const observer = new MutationObserver(callback);
  656. observer.observe(document, { childList: true, subtree: true });
  657. }
  658.  
  659. // 添加屏蔽用户按钮(用户主页)
  660. function blockUsers_button_people() {
  661. let item = document.querySelector('.MemberButtonGroup.ProfileButtonGroup.ProfileHeader-buttons'), // 获取按钮元素位置
  662. name = document.querySelector('.ProfileHeader-name').firstChild.textContent, // 获取用户名
  663. users = menu_value('menu_customBlockUsers'), // 读取屏蔽列表
  664. userid = location.href.split('/')[4];
  665. for (let num = 0;num<users.length;num++) { // 判断是否已存在
  666. if (users[num] === name) { // 已存在
  667. document.querySelectorAll('.Button.Button--primary.Button--red').forEach(function(item){item.style.display = 'none';}) // 隐藏知乎自带的已屏蔽按钮
  668. 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 class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><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>`);
  669. item.lastElementChild.onclick = function(){blockUsers_button_del(this.dataset.name, this.dataset.userid, true)}
  670. return
  671. }
  672. };
  673. if (item) {
  674. 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 class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><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>`);
  675. item.lastElementChild.onclick = function(){blockUsers_button_add(this.dataset.name, this.dataset.userid, true)}
  676. }
  677. }
  678.  
  679. // 屏蔽用户按钮绑定事件(添加)
  680. function blockUsers_button_add(name, userid, reload) {
  681. if (!name || !userid) return
  682. let users = menu_value('menu_customBlockUsers'), // 读取屏蔽列表
  683. index = users.indexOf(name);
  684. if (index === -1) {
  685. users.push(name); // 追加用户名
  686. GM_setValue('menu_customBlockUsers', users); // 写入屏蔽列表
  687. // 加入知乎自带的黑名单(和本脚本互补~
  688. GM_xmlhttpRequest({url: `https://www.zhihu.com/api/v4/members/${userid}/actions/block`,method: 'POST',timeout: 2000});
  689. // 是否刷新本页
  690. if (reload) {
  691. setTimeout(function(){location.reload()}, 200); // 刷新网页,延迟 200 毫秒,避免知乎反应慢~
  692. } else {
  693. GM_notification({text: `该用户已被屏蔽~\n刷新网页后生效~`, timeout: 3000});
  694. }
  695. } else {
  696. GM_notification({text: `该用户已经被屏蔽啦,无需重复屏蔽~`, timeout: 3000});
  697. }
  698. }
  699.  
  700.  
  701. // 屏蔽用户按钮绑定事件(删除)
  702. function blockUsers_button_del(name, userid, reload) {
  703. if (!name || !userid) return
  704. let users = menu_value('menu_customBlockUsers'), // 读取屏蔽列表
  705. index = users.indexOf(name);
  706. if (index > -1) {
  707. users.splice(index, 1); // 移除用户名
  708. GM_setValue('menu_customBlockUsers', users); // 写入屏蔽列表
  709. // 移除知乎自带的黑名单
  710. GM_xmlhttpRequest({url: `https://www.zhihu.com/api/v4/members/${userid}/actions/block`,method: 'DELETE',timeout: 2000});
  711. // 是否刷新本页
  712. if (reload) {
  713. setTimeout(function(){location.reload()}, 200); // 刷新网页,延迟 200 毫秒,避免知乎反应慢~
  714. } else {
  715. GM_notification({text: `该用户已取消屏蔽啦~\n刷新网页后生效~`, timeout: 3000});
  716. }
  717. } else {
  718. GM_notification({text: `没有在屏蔽列表中找到该用户...`, timeout: 3000});
  719. }
  720. }
  721. }
  722.  
  723.  
  724. // 自定义屏蔽关键词(标题)
  725. function customBlockKeywords() {
  726. let nowBlockKeywords = '';
  727. menu_value('menu_customBlockKeywords').forEach(function(item){nowBlockKeywords += '|' + item})
  728. let newBlockKeywords = prompt('编辑 [自定义屏蔽关键词]\n(不同关键词之间使用 "|" 分隔,例如:关键词A|关键词B|关键词C \n(关键词不区分大小写,支持表情如:[捂脸]|[飙泪笑]', nowBlockKeywords.replace('|',''));
  729. if (newBlockKeywords === '') {
  730. GM_setValue('menu_customBlockKeywords', []);
  731. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  732. } else if (newBlockKeywords != null) {
  733. GM_setValue('menu_customBlockKeywords', newBlockKeywords.split('|'));
  734. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  735. }
  736. };
  737.  
  738.  
  739. // 屏蔽指定关键词
  740. function blockKeywords(type) {
  741. if (!menu_value('menu_blockKeywords')) return
  742. if (!menu_value('menu_customBlockKeywords') || menu_value('menu_customBlockKeywords').length < 1) return
  743. switch(type) {
  744. case 'index':
  745. blockKeywords_('.Card.TopstoryItem.TopstoryItem-isRecommend', 'Card TopstoryItem TopstoryItem-isRecommend');
  746. break;
  747. case 'topic':
  748. blockKeywords_('.List-item.TopicFeedItem', 'List-item TopicFeedItem');
  749. break;
  750. case 'people':
  751. blockKeywords_('.List-item', 'List-item');
  752. break;
  753. case 'collection':
  754. blockKeywords_('.Card.CollectionDetailPageItem', 'Card CollectionDetailPageItem');
  755. break;
  756. case 'search':
  757. blockKeywords_search();
  758. break;
  759. case 'comment':
  760. blockKeywords_comment();
  761. break;
  762. }
  763.  
  764.  
  765. function blockKeywords_(className1, className2) {
  766. // 前几条因为是直接加载的,而不是动态插入网页的,所以需要单独判断
  767. function blockKeywords_now() {
  768. if (location.pathname === '/hot') {
  769. document.querySelectorAll('.HotItem').forEach(function(item1){blockKeywords_1(item1, 'h2.HotItem-title');})
  770. } else {
  771. document.querySelectorAll(className1).forEach(function(item1){blockKeywords_1(item1, 'h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]');})
  772. }
  773. }
  774.  
  775. blockKeywords_now();
  776. window.addEventListener('urlchange', function(){
  777. setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行
  778. })
  779.  
  780. // 这个是监听网页插入事件,用来判断后续网页动态插入的元素
  781. const callback = (mutationsList, observer) => {
  782. for (const mutation of mutationsList) {
  783. for (const target of mutation.addedNodes) {
  784. if (target.nodeType != 1) return
  785. if (target.className === className2) {blockKeywords_1(target, 'h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]');}
  786. }
  787. }
  788. };
  789. const observer = new MutationObserver(callback);
  790. observer.observe(document, { childList: true, subtree: true });
  791. }
  792.  
  793.  
  794. function blockKeywords_search() {
  795. function blockKeywords_now() {
  796. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  797. 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]');})
  798. }
  799.  
  800. setTimeout(blockKeywords_now, 500);
  801. window.addEventListener('urlchange', function(){
  802. setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行
  803. })
  804.  
  805. const callback = (mutationsList, observer) => {
  806. if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
  807. for (const mutation of mutationsList) {
  808. for (const target of mutation.addedNodes) {
  809. if (target.nodeType != 1) return
  810. if (target.className === 'Card SearchResult-Card' && (target.dataset.zaDetailViewPathModule === 'AnswerItem' || target.dataset.zaDetailViewPathModule === 'PostItem')) {blockKeywords_1(target, 'a[data-za-detail-view-id]');}
  811. }
  812. }
  813. };
  814. const observer = new MutationObserver(callback);
  815. observer.observe(document, { childList: true, subtree: true });
  816. }
  817.  
  818.  
  819. function blockKeywords_comment() {
  820. function filterComment(comment) {
  821. let content = comment.querySelector('.RichText'); // 寻找评论文字所在元素
  822. let texts = [content.textContent.toLowerCase()]; // 因为要针对评论中的表情,所以需要整个数组并全部转为小写(用来不区分大小写)
  823. for (let i = 0; i < content.children.length; i++) { // 该条针对的是评论中的表情
  824. let emoticonValue = content.children[i].getAttribute('data-zhihu-emoticon'); // 确定是表情就将其添加到稍后遍历的数组中
  825. if (emoticonValue) {
  826. texts.push(emoticonValue)
  827. }
  828. }
  829.  
  830. let keywords = menu_value('menu_customBlockKeywords');
  831. for (const text of texts) {
  832. for (const keyword of keywords) { // 遍历关键词黑名单
  833. if (keyword != '' && text.indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该评论
  834. console.log('已屏蔽评论:' + text);
  835. content.textContent = '[该评论已屏蔽]';
  836. break;
  837. }
  838. }
  839. }
  840. }
  841.  
  842. const callback = (mutationsList, observer) => {
  843. for (const mutation of mutationsList) {
  844. for (const target of mutation.addedNodes) {
  845. if (target.nodeType != 1) return
  846. for (const node of target.querySelectorAll('*')) {
  847. if (node.className === 'CommentItemV2-metaSibling') filterComment(node);
  848. }
  849. }
  850. }
  851. };
  852. const observer = new MutationObserver(callback);
  853. observer.observe(document, { childList: true, subtree: true });
  854. }
  855.  
  856. function blockKeywords_1(item1, css) {
  857. let item = item1.querySelector(css); // 标题所在元素
  858. if (item) {
  859. for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
  860. let text = item.content || item.textContent;
  861. if (keyword != '' && text.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
  862. console.log(text);
  863. item1.hidden = true;
  864. item1.style.display = 'none';
  865. break;
  866. }
  867. }
  868. }
  869. }
  870. }
  871.  
  872.  
  873. // 屏蔽指定类别(视频/文章等)
  874. function blockType(type) {
  875. let name;
  876. // 一开始加载的信息流 + 添加标签样式
  877. if (type === 'search') { // 搜索页
  878. if (!menu_value('menu_blockTypeVideo') && !menu_value('menu_blockTypeArticle') && !menu_value('menu_blockTypeTopic') && !menu_value('menu_blockTypeSearch')) return
  879. if (menu_value('menu_blockTypeSearch') && location.pathname === '/search') setTimeout(function(){document.querySelector('.RelevantQuery').parentElement.parentElement.hidden = true;;}, 1000)
  880. name = 'h2.ContentItem-title a:not(.zhihu_e_toQuestion), a.KfeCollection-PcCollegeCard-link, h2.SearchTopicHeader-Title a'
  881. addSetInterval_(name);
  882. } else if (type === 'question') { // 问题页
  883. if (!menu_value('menu_blockTypeVideo')) return
  884. document.lastChild.appendChild(document.createElement('style')).textContent = `.VideoAnswerPlayer, .VideoAnswerPlayer-video, .VideoAnswerPlayer-iframe {display: none !important;}`;
  885. name = '.VideoAnswerPlayer'
  886. document.querySelectorAll(name).forEach(function(item){blockType_(item);})
  887. } else { // 首页
  888. if (!menu_value('menu_blockTypeVideo') && !menu_value('menu_blockTypeArticle')) return
  889. 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;}`;
  890. name = 'h2.ContentItem-title a:not(.zhihu_e_toQuestion)'
  891. document.querySelectorAll(name).forEach(function(item){blockType_(item);})
  892. }
  893.  
  894. // 后续加载的信息流
  895. const observer = new MutationObserver(mutationsList => {
  896. for (const mutation of mutationsList) {
  897. for (const target of mutation.addedNodes) {
  898. if (target.nodeType != 1) return
  899. blockType_(target.querySelector(name));
  900. }
  901. }
  902. });
  903. observer.observe(document, { childList: true, subtree: true });
  904.  
  905. window.addEventListener('urlchange', function(){
  906. addSetInterval_(name);
  907. // 移除相关搜索
  908. if (menu_value('menu_blockTypeSearch') && location.pathname === '/search' && location.search.indexOf('type=content') > -1) setTimeout(function(){document.querySelector('.RelevantQuery').parentElement.parentElement.hidden = true;}, 1500)
  909. })
  910.  
  911. function blockType_(titleA) {
  912. if (!titleA) return // 判断是否为真
  913. //console.log(titleA.href)
  914. if (location.pathname === '/search') { // 搜索页
  915. if (location.search.indexOf('type=content') === -1) return // 仅限搜索页的 [综合]
  916. if (titleA.href.indexOf('/zvideo/') > -1 || titleA.href.indexOf('video.zhihu.com') > -1) { // 如果是视频
  917. if (menu_value('menu_blockTypeVideo')) findParentElement(titleA, 'Card').hidden = true;
  918. } else if (titleA.href.indexOf('zhuanlan.zhihu.com') > -1) { // 如果是文章
  919. if (menu_value('menu_blockTypeArticle')) findParentElement(titleA, 'Card SearchResult-Card').hidden = true;
  920. } else if (titleA.href.indexOf('/topic/') > -1) { // 如果是话题
  921. if (menu_value('menu_blockTypeTopic')) findParentElement(titleA, 'Card SearchResult-Card').hidden = true;
  922. } else if (titleA.href.indexOf('/market/') > -1) { // 如果是杂志文章等乱七八糟的
  923. if (menu_value('menu_blockTypeArticle')) findParentElement(titleA, 'Card SearchResult-Card').hidden = true;
  924. }
  925. } else if (location.pathname.indexOf('/question/') > -1) { // 问题页
  926. if (menu_value('menu_blockTypeVideo')) findParentElement(titleA, 'List-item').hidden = true;
  927. } else { // 首页
  928. if (titleA.href.indexOf('/zvideo/') > -1 || titleA.href.indexOf('video.zhihu.com') > -1) { // 如果是视频
  929. if (menu_value('menu_blockTypeVideo')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;
  930. } else if (titleA.href.indexOf('/answer/') > -1) { // 如果是问题(视频回答)
  931. if (findParentElement(titleA, 'ContentItem AnswerItem').querySelector('.VideoAnswerPlayer')) {
  932. if (menu_value('menu_blockTypeVideo')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;
  933. }
  934. } else if (titleA.href.indexOf('zhuanlan.zhihu.com') > -1) { // 如果是文章
  935. if (menu_value('menu_blockTypeArticle')) findParentElement(titleA, 'Card TopstoryItem TopstoryItem-isRecommend').hidden = true;
  936. }
  937. }
  938. }
  939.  
  940. function addSetInterval_(A) {
  941. let timer = setInterval(function(){
  942. let aTag = document.querySelectorAll(A);
  943. if (aTag.length > 0) {
  944. clearInterval(timer);
  945. aTag.forEach(function(item){blockType_(item);})
  946. }
  947. });
  948. }
  949. }
  950.  
  951.  
  952. // 寻找父元素
  953. function findParentElement(item, className, type = false) {
  954. if (item.parentElement) {
  955. //console.log(item.parentElement)
  956. if (type) { // true = 完全一致,false = 包含即可
  957. if (item.parentElement.className && item.parentElement.className === className) {
  958. //console.log(item.parentElement.className)
  959. return item.parentElement;
  960. } else {
  961. let temp = findParentElement(item.parentElement, className, true)
  962. if (temp) return temp
  963. }
  964. } else {
  965. if (item.parentElement.className && item.parentElement.className.indexOf(className) > -1) {
  966. return item.parentElement;
  967. } else {
  968. let temp = findParentElement(item.parentElement, className)
  969. if (temp) return temp
  970. }
  971. }
  972. }
  973. return
  974. }
  975.  
  976.  
  977. // 移除高亮链接
  978. function removeHighlightLink() {
  979. const callback = (mutationsList, observer) => {
  980. for (const mutation of mutationsList) {
  981. for (const target of mutation.addedNodes) {
  982. if (target.nodeType != 1 || target.tagName != 'A') break
  983. if (target.dataset.zaNotTrackLink && target.href.indexOf('https://www.zhihu.com/search?q=') > -1) {
  984. target.parentElement.replaceWith(target.textContent);
  985. }
  986. }
  987. }
  988. };
  989. const observer = new MutationObserver(callback);
  990. observer.observe(document, { childList: true, subtree: true });
  991.  
  992. // 针对的是打开网页后直接加载的前面几个回答(上面哪些是针对动态加载的回答)
  993. document.querySelectorAll('span > a[data-za-not-track-link][href^="https://www.zhihu.com/search?q="]').forEach(e => e.parentElement.replaceWith(e.textContent))
  994. }
  995.  
  996.  
  997. // 屏蔽盐选内容
  998. function blockYanXuan() {
  999. if (!menu_value('menu_blockYanXuan')) return
  1000. const blockYanXuan_question = (mutationsList, observer) => {
  1001. for (const mutation of mutationsList) {
  1002. for (const target of mutation.addedNodes) {
  1003. if (target.nodeType != 1) return
  1004. if (target.className === 'List-item' || target.className === 'Card AnswerCard') {
  1005. if (target.querySelector('.KfeCollection-AnswerTopCard-Container, .KfeCollection-PurchaseBtn')) {
  1006. target.hidden = true;
  1007. }
  1008. }
  1009. }
  1010. }
  1011. };
  1012.  
  1013. const blockYanXuan_question_answer = (mutationsList, observer) => {
  1014. for (const mutation of mutationsList) {
  1015. for (const target of mutation.addedNodes) {
  1016. if (target.nodeType != 1) return
  1017. target.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  1018. if (item.querySelector('.KfeCollection-AnswerTopCard-Container, .KfeCollection-PurchaseBtn')) {
  1019. item.hidden = true;
  1020. }
  1021. })
  1022. }
  1023. }
  1024. };
  1025.  
  1026. if (location.pathname.indexOf('/answer/') > -1) { // 回答页(就是只有三个回答的页面)
  1027. const observer = new MutationObserver(blockYanXuan_question_answer);
  1028. observer.observe(document, { childList: true, subtree: true });
  1029. } else { // 问题页(可以显示所有回答的页面)
  1030. const observer = new MutationObserver(blockYanXuan_question);
  1031. observer.observe(document, { childList: true, subtree: true });
  1032. }
  1033.  
  1034. // 针对的是打开网页后直接加载的前面几个回答(上面哪些是针对动态加载的回答)
  1035. document.querySelectorAll('.List-item, .Card.AnswerCard').forEach(function(item){
  1036. if (item.querySelector('.KfeCollection-AnswerTopCard-Container, .KfeCollection-PurchaseBtn')) {
  1037. item.hidden = true;
  1038. }
  1039. })
  1040. }
  1041.  
  1042.  
  1043. // 区分问题文章
  1044. function addTypeTips() {
  1045. if (!menu_value('menu_typeTips')) return
  1046. 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;`
  1047. document.body.appendChild(document.createElement('style')).textContent = `/* 区分问题文章 */
  1048. .AnswerItem .ContentItem-title a:not(.zhihu_e_toQuestion)::before {content:'问题';color: #f68b83;background-color: #f68b8333;${style}}
  1049. .TopstoryQuestionAskItem .ContentItem-title a:not(.zhihu_e_toQuestion)::before {content:'问题';color: #ff5a4e;background-color: #ff5a4e33;${style}}
  1050. .ZVideoItem .ContentItem-title a::before, .ZvideoItem .ContentItem-title a::before {content:'视频';color: #00BCD4;background-color: #00BCD433;${style}}
  1051. .ArticleItem .ContentItem-title a::before {content:'文章';color: #2196F3;background-color: #2196F333;${style}}`;
  1052. }
  1053.  
  1054.  
  1055. // 直达问题按钮
  1056. function addToQuestion() {
  1057. if (!menu_value('menu_toQuestion')) return
  1058.  
  1059. // 一开始加载的信息流 + 添加按钮样式
  1060. if (location.pathname === '/search') {
  1061. 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;}`;
  1062. addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
  1063. } else {
  1064. 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;}`;
  1065. document.querySelectorAll('h2.ContentItem-title a:not(.zhihu_e_tips)').forEach(function(item){addTypeTips_(item);})
  1066. }
  1067.  
  1068. // 后续加载的信息流
  1069. const observer = new MutationObserver(mutationsList => {
  1070. for (const mutation of mutationsList) {
  1071. for (const target of mutation.addedNodes) {
  1072. if (target.nodeType != 1) return
  1073. addTypeTips_(target.querySelector('h2.ContentItem-title a:not(.zhihu_e_tips)'));
  1074. }
  1075. }
  1076. });
  1077. observer.observe(document, { childList: true, subtree: true });
  1078.  
  1079. window.addEventListener('urlchange', function(){
  1080. addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
  1081. })
  1082.  
  1083. function addTypeTips_(titleA) {
  1084. if (!titleA) return // 判断是否为真
  1085. if (titleA.parentElement.querySelector('a.zhihu_e_toQuestion')) return // 判断是否已添加
  1086. if (titleA.textContent.indexOf('?') != -1) { // 把问题末尾英文问好 [?] 的替换为中文问好 [?],这样按钮与标题之间的间距就刚刚好~
  1087. titleA.innerHTML = titleA.innerHTML.replace('?', "?")
  1088. }
  1089. if (/answer\/\d+/.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
  1090. titleA.insertAdjacentHTML('afterend', `<a class="zhihu_e_toQuestion VoteButton" href="${titleA.parentElement.querySelector('meta[itemprop="url"]').content}" target="_blank">直达问题</a>`);
  1091. }
  1092. }
  1093.  
  1094. function addSetInterval_(A) {
  1095. let timer = setInterval(function(){
  1096. let aTag = document.querySelectorAll(A);
  1097. if (aTag.length > 0) {
  1098. clearInterval(timer);
  1099. aTag.forEach(function(item){addTypeTips_(item);})
  1100. }
  1101. });
  1102. }
  1103. }
  1104.  
  1105.  
  1106. // 展开问题描述
  1107. function questionRichTextMore() {
  1108. if (!menu_value('menu_questionRichTextMore')) return
  1109. let button = document.querySelector('button.QuestionRichText-more');
  1110. if (button) button.click()
  1111. }
  1112.  
  1113.  
  1114. // 知乎免登录(不可用)
  1115. function removeLogin() {
  1116. const removeLoginModal = (mutationsList, observer) => {
  1117. for (const mutation of mutationsList) {
  1118. for (const target of mutation.addedNodes) {
  1119. if (target.nodeType != 1) return
  1120. if (target.querySelector('.signFlowModal')) {
  1121. let button = target.querySelector('.Button.Modal-closeButton.Button--plain');
  1122. if (button) button.click();
  1123. }
  1124. }
  1125. }
  1126. };
  1127.  
  1128. // 未登录(不可用)时才会监听并移除登录(不可用)弹窗
  1129. if(location.hostname === 'zhuanlan.zhihu.com') { // 如果是文章页
  1130. if (!document.querySelector('button.ColumnPageHeader-MenuToggler')) { // 未登录(不可用)
  1131. const observer = new MutationObserver(removeLoginModal);
  1132. observer.observe(document, { childList: true, subtree: true });
  1133. } else {
  1134. cleanTitles(); // 净化标题消息
  1135. }
  1136. } else { // 不是文章页
  1137. if (document.querySelector('button.AppHeader-login')) { // 未登录(不可用)
  1138. const observer = new MutationObserver(removeLoginModal);
  1139. observer.observe(document, { childList: true, subtree: true });
  1140. document.lastElementChild.appendChild(document.createElement('style')).textContent = '.Question-mainColumnLogin, button.AppHeader-login {display: none !important;}'; // 屏蔽问题页中间的登录(不可用)提示
  1141. document.querySelector('button.AppHeader-login').insertAdjacentHTML('afterend', '<a class="Button AppHeader-login Button--blue" href="https://www.zhihu.com/signin" target="_blank">登录(不可用)</a>'); // [登录(不可用)] 按钮跳转至登录(不可用)页面
  1142. } else {
  1143. cleanTitles(); // 净化标题消息
  1144. }
  1145. }
  1146. }
  1147.  
  1148.  
  1149. // 净化标题消息
  1150. function cleanTitles() {
  1151. if (!menu_value('menu_cleanTitles')) return
  1152.  
  1153. // 方案一
  1154. const elTitle = document.head.querySelector('title');
  1155. const original = elTitle.textContent;
  1156. const observer = new MutationObserver(function() {
  1157. if (elTitle.textContent != original) { // 避免重复执行
  1158. elTitle.textContent = original;
  1159. }
  1160. });
  1161. observer.observe(elTitle, { childList: true });
  1162.  
  1163. // 方案二
  1164. // if (Reflect.getOwnPropertyDescriptor(document, 'title')) {
  1165. // const elTitle = document.head.querySelector('title');
  1166. // const original = elTitle.textContent;
  1167. // const observer = new MutationObserver(function() {
  1168. // if (elTitle.textContent != original) { // 避免重复执行
  1169. // elTitle.textContent = original;
  1170. // }
  1171. // });
  1172. // observer.observe(elTitle, { childList: true });
  1173. // } else {
  1174. // const title = document.title;
  1175. // Reflect.defineProperty(document, 'title', {
  1176. // set: () => {},
  1177. // get: () => title,
  1178. // });
  1179. // }
  1180. }
  1181.  
  1182.  
  1183. // 净化搜索热门
  1184. function cleanSearch() {
  1185. if (!menu_value('menu_cleanSearch')) return
  1186.  
  1187. const el = document.querySelector('.SearchBar-input > input');
  1188. const observer = new MutationObserver((mutationsList, observer) => {
  1189. if (mutationsList[0].attributeName === 'placeholder' && mutationsList[0].target.placeholder != '') mutationsList[0].target.placeholder = '';
  1190. });
  1191. el.placeholder = '';
  1192. observer.observe(el, { attributes: true });
  1193. document.documentElement.appendChild(document.createElement('style')).textContent = '.AutoComplete-group > .SearchBar-label:not(.SearchBar-label--history), .AutoComplete-group > [id^="AutoComplete2-topSearch-"] {display: none !important;}';
  1194. }
  1195.  
  1196.  
  1197. // 快捷关闭悬浮评论(监听点击事件,点击网页两侧空白处)
  1198. function closeFloatingComments() {
  1199. const closeFloatingCommentsModal = (mutationsList, observer) => {
  1200. for (const mutation of mutationsList) {
  1201. for (const target of mutation.addedNodes) {
  1202. if (target.nodeType != 1) return
  1203. if (target.querySelector('.Modal-backdrop')) {
  1204. document.querySelector('.Modal-backdrop').onclick = function(event){
  1205. if (event.target == this) {
  1206. let button = document.querySelector('.Button.Modal-closeButton.Button--plain');
  1207. if (button) button.click();
  1208. }
  1209. }
  1210. }
  1211. }
  1212. }
  1213. };
  1214. const observer = new MutationObserver(closeFloatingCommentsModal);
  1215. observer.observe(document, { childList: true, subtree: true });
  1216. }
  1217.  
  1218.  
  1219. // 监听 XMLHttpRequest 事件
  1220. /*function EventXMLHttpRequest() {
  1221. var _send = window.XMLHttpRequest.prototype.send
  1222. function sendReplacement(data) {
  1223. addTypeTips();
  1224. return _send.apply(this, arguments);
  1225. }
  1226. window.XMLHttpRequest.prototype.send = sendReplacement;
  1227. }*/
  1228.  
  1229.  
  1230. // 自定义 urlchange 事件(用来监听 URL 变化)
  1231. function addUrlChangeEvent() {
  1232. history.pushState = ( f => function pushState(){
  1233. var ret = f.apply(this, arguments);
  1234. window.dispatchEvent(new Event('pushstate'));
  1235. window.dispatchEvent(new Event('urlchange'));
  1236. return ret;
  1237. })(history.pushState);
  1238.  
  1239. history.replaceState = ( f => function replaceState(){
  1240. var ret = f.apply(this, arguments);
  1241. window.dispatchEvent(new Event('replacestate'));
  1242. window.dispatchEvent(new Event('urlchange'));
  1243. return ret;
  1244. })(history.replaceState);
  1245.  
  1246. window.addEventListener('popstate',()=>{
  1247. window.dispatchEvent(new Event('urlchange'))
  1248. });
  1249. }
  1250.  
  1251.  
  1252. // 显示问题作者
  1253. function question_author() {
  1254. if (document.querySelector('.BrandQuestionSymbol, .QuestionAuthor')) return
  1255. let qJson = JSON.parse(document.querySelector('#js-initialData').textContent).initialState.entities.questions[/\d+/.exec(location.pathname)[0]].author,
  1256. 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>`;
  1257. //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>`
  1258. document.querySelector('.QuestionHeader-topics').insertAdjacentHTML('beforebegin', html);
  1259. //document.querySelector('.QuestionPage h1.QuestionHeader-title').insertAdjacentHTML('afterend', html);
  1260. }
  1261.  
  1262.  
  1263. // [完整显示时间 + 置顶显示时间] 功能修改自:https://gf.qytechs.cn/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1264. // 完整显示时间 + 置顶显示时间
  1265. function topTime_(css, classs) {
  1266. document.querySelectorAll(css).forEach(function(_this) {
  1267. let t = _this.querySelector('.ContentItem-time'); if (!t) return
  1268. if (!(t.classList.contains('full')) && t.querySelector('span') && t.querySelector('span').textContent != null) {
  1269. // 完整显示时间
  1270. topTime_allTime(t)
  1271. // 发布时间置顶
  1272. topTime_publishTop(t, _this, classs)
  1273. }
  1274. });
  1275. }
  1276.  
  1277.  
  1278. // 完整显示时间 + 置顶显示时间 - 文章
  1279. function topTime_post() {
  1280. let t = document.querySelector('.ContentItem-time:not(.xiu-time)'); if (!t) return
  1281. // 完整显示时间
  1282. if (t.textContent.indexOf('编辑于') > -1 && !(t.classList.contains('xiu-time'))) {
  1283. let tt = t.textContent;
  1284. t.click();
  1285. t.textContent = (t.textContent + ' ,' + tt)
  1286. t.classList.add('xiu-time');
  1287. }
  1288.  
  1289. //发布时间置顶
  1290. if (menu_value('menu_publishTop') && !(document.querySelector('.Post-Header > .ContentItem-time')) && !(document.querySelector('.ContentItem-meta > .ContentItem-time'))) {
  1291. let temp_time = t.cloneNode(true);
  1292. temp_time.style.padding = '0px';
  1293. document.querySelector('.Post-Header').insertAdjacentElement('beforeEnd', temp_time);
  1294. }
  1295. }
  1296.  
  1297.  
  1298. // 完整显示时间
  1299. function topTime_allTime(t) {
  1300. if (t.textContent.indexOf('发布于') > -1 && t.textContent.indexOf('编辑于') == -1) {
  1301. t.querySelector('span').textContent = (t.querySelector('span').dataset.tooltip);
  1302. t.classList.add('full');
  1303. } else if (t.textContent.indexOf('发布于') == -1 && t.textContent.indexOf('编辑于') > -1) {
  1304. t.querySelector('span').textContent = (t.querySelector('span').dataset.tooltip) + ' ,' + (t.querySelector('span').textContent);
  1305. t.classList.add('full');
  1306. }
  1307. }
  1308.  
  1309.  
  1310. // 发布时间置顶
  1311. function topTime_publishTop(t, _this, _class) {
  1312. if (!menu_value('menu_publishTop')) return
  1313. if (!t.parentNode.classList.contains(_class)) {
  1314. let temp_time = t.cloneNode(true);
  1315. temp_time.style.padding = '0px';
  1316. // 对于较短的回答,隐藏回答底部的时间
  1317. if (_this.offsetHeight < 400) t.style.display = 'none';
  1318. _this.querySelector('.' + _class).insertAdjacentElement('beforeEnd', temp_time);
  1319. }
  1320. }
  1321.  
  1322.  
  1323. // 问题创建时间
  1324. function question_time() {
  1325. if (!(document.querySelector('.QuestionPage .QuestionHeader-side .QuestionTime-xiu'))) {
  1326. 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>');
  1327. }
  1328. }
  1329.  
  1330.  
  1331. // UTC 标准时转 UTC+8 北京时间,修改自:https://gf.qytechs.cn/zh-CN/scripts/402808(精简)
  1332. function getUTC8(t) {
  1333. 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()));
  1334. }
  1335.  
  1336.  
  1337. // 默认站外直链,修改自:https://gf.qytechs.cn/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1338. function directLink () {
  1339. 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));});
  1340. }
  1341.  
  1342.  
  1343. // 默认高清原图,修改自:https://gf.qytechs.cn/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1344. function originalPic(){
  1345. document.querySelectorAll('img[data-original]:not(.comment_sticker):not(.Avatar)').forEach(function(one){if (one.src != one.dataset.original) {one.src = one.dataset.original}});
  1346. }
  1347.  
  1348.  
  1349. // 默认折叠邀请,修改自:https://gf.qytechs.cn/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码)
  1350. function questionInvitation(){
  1351. let time = setInterval(function(){
  1352. let q = document.querySelector('.QuestionInvitation-content'); if (!q) return
  1353. clearInterval(time);
  1354. q.style.display = 'none';
  1355. document.querySelector('.QuestionInvitation-title').innerHTML = document.querySelector('.QuestionInvitation-title').innerText + '<span style="cursor: pointer; font-size: 14px; color: #919aae;"> 展开/折叠</span>'
  1356. // 点击事件(展开/折叠)
  1357. document.querySelector('.Topbar').onclick = function(){
  1358. let q = document.querySelector('.QuestionInvitation-content')
  1359. if (q.style.display == 'none') {
  1360. q.style.display = ''
  1361. } else {
  1362. q.style.display = 'none'
  1363. }
  1364. }
  1365. });
  1366. }
  1367.  
  1368.  
  1369. (function() {
  1370. if (window.onurlchange === undefined) {addUrlChangeEvent();} // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
  1371. removeLogin(); // 移除登录(不可用)弹窗
  1372. setInterval(originalPic,100); // 默认高清原图
  1373. setInterval(directLink, 100); // 默认站外直链
  1374. window.addEventListener('urlchange', function(){ // 针对的是从单个回答页跳转到完整回答页时
  1375. if (location.pathname.indexOf('question') > -1 && location.pathname.indexOf('waiting') === -1 && location.pathname.indexOf('/answer/') === -1) { // 回答页 //
  1376. setTimeout(function(){
  1377. collapsedNowAnswer('.QuestionPage'); // 收起当前回答 + 快捷返回顶部
  1378. collapsedNowAnswer('.Question-main'); // 收起当前回答 + 快捷返回顶部
  1379. questionRichTextMore(); // 展开问题描述
  1380. blockUsers('question'); // 屏蔽指定用户
  1381. blockYanXuan(); // 屏蔽盐选内容
  1382. }, 300);
  1383. }
  1384. })
  1385.  
  1386. if (GM_info.scriptHandler === 'Violentmonkey') { // Violentmonkey 比 Tampermonkey 加载更早,会导致一些元素还没加载,因此需要延迟一会儿
  1387. setTimeout(start, 300);
  1388. } else {
  1389. start();
  1390. }
  1391.  
  1392. function start(){
  1393. cleanSearch(); // 净化搜索热门
  1394. removeHighlightLink(); // 移除高亮链接
  1395. if (location.hostname != 'zhuanlan.zhihu.com') {collapsedAnswer();} // 一键收起回答
  1396. closeFloatingComments(); // 快捷关闭悬浮评论(监听点击事件,点击网页两侧空白处)
  1397. blockKeywords('comment'); // 屏蔽指定关键词(评论)
  1398.  
  1399.  
  1400. if (location.pathname.indexOf('question') > -1 && location.href.indexOf('/log') == -1) { // 回答页 //
  1401. if (location.pathname.indexOf('waiting') == -1) {
  1402. collapsedNowAnswer('.QuestionPage'); // 收起当前回答 + 快捷返回顶部
  1403. collapsedNowAnswer('.Question-main'); // 收起当前回答 + 快捷返回顶部
  1404. questionRichTextMore(); // 展开问题描述
  1405. blockUsers('question'); // 屏蔽指定用户
  1406. blockYanXuan(); // 屏蔽盐选内容
  1407. blockType('question'); // 屏蔽指定类别(视频/文章等)
  1408. defaultCollapsedAnswer(); // 默认收起回答
  1409. }
  1410. setInterval(function(){topTime_('.ContentItem.AnswerItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1411. setTimeout(function(){question_time(); question_author()}, 100); //问题创建时间 + 显示问题作者
  1412. questionInvitation(); // 默认折叠邀请
  1413.  
  1414. } else if (location.pathname === '/search') { // 搜索结果页 //
  1415. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1416. collapsedNowAnswer('.Search-container'); // 收起当前回答 + 快捷返回顶部
  1417. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'SearchItem-meta')}, 300); // 置顶显示时间
  1418. addTypeTips(); // 区分问题文章
  1419. addToQuestion(); // 直达问题按钮
  1420. blockUsers('search'); // 屏蔽指定用户
  1421. blockKeywords('search'); // 屏蔽指定关键词
  1422. blockType('search'); // 屏蔽指定类别(视频/文章等)
  1423.  
  1424.  
  1425. } else if (location.pathname.indexOf('/topic/') > -1) { // 话题页 //
  1426. if (location.pathname.indexOf('/hot') > -1 || location.href.indexOf('/top-answers') > -1) { // 仅限 [讨论] [精华]
  1427. collapsedNowAnswer('main.App-main'); // 收起当前回答 + 快捷返回顶部
  1428. collapsedNowAnswer('.ContentLayout'); // 收起当前回答 + 快捷返回顶部
  1429. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1430. addTypeTips(); // 区分问题文章
  1431. addToQuestion(); // 直达问题按钮
  1432. blockUsers('topic'); // 屏蔽指定用户
  1433. blockKeywords('topic'); // 屏蔽指定关键词
  1434. }
  1435.  
  1436. } else if (location.hostname === 'zhuanlan.zhihu.com'){ // 文章 //
  1437. backToTop('article.Post-Main.Post-NormalMain'); // 快捷返回顶部
  1438. backToTop('div.Post-Sub.Post-NormalSub'); // 快捷返回顶部
  1439. setTimeout(topTime_post, 300); // 置顶显示时间
  1440. blockUsers(); // 屏蔽指定用户
  1441.  
  1442.  
  1443. } else if (location.pathname.indexOf('/column/') > -1) { // 专栏 //
  1444. setTimeout(function(){
  1445. collapsedAnswer(); // 一键收起回答
  1446. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1447. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1448. blockUsers(); // 屏蔽指定用户
  1449. }, 300);
  1450.  
  1451.  
  1452. } else if (location.pathname.indexOf('/people/') > -1 || location.href.indexOf('org') > -1) { // 用户主页 //
  1453. if (location.pathname.split('/').length === 3) addTypeTips();addToQuestion(); // 区分问题文章、直达问题按钮
  1454. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1455. collapsedNowAnswer('.Profile-main'); // 收起当前回答 + 快捷返回顶部
  1456. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1457. blockUsers('people'); // 屏蔽指定用户
  1458. blockKeywords('people'); // 屏蔽指定关键词
  1459.  
  1460.  
  1461. } else if (location.pathname.indexOf('/collection/') > -1) { // 收藏夹 //
  1462. addTypeTips(); // 区分问题文章
  1463. addToQuestion(); // 直达问题按钮
  1464. collapsedNowAnswer('main'); // 收起当前回答 + 快捷返回顶部
  1465. collapsedNowAnswer('.CollectionsDetailPage'); // 收起当前回答 + 快捷返回顶部
  1466. setInterval(function(){topTime_('.ContentItem.AnswerItem, .ContentItem.ArticleItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1467. blockKeywords('collection'); // 屏蔽指定关键词
  1468.  
  1469.  
  1470. } else { // 首页 //
  1471. collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
  1472. collapsedNowAnswer('.Topstory-container'); // 收起当前回答 + 快捷返回顶部
  1473. setInterval(function(){topTime_('.TopstoryItem', 'ContentItem-meta')}, 300); // 置顶显示时间
  1474. addTypeTips(); // 区分问题文章
  1475. addToQuestion(); // 直达问题按钮
  1476. blockUsers('index'); // 屏蔽指定用户
  1477. blockKeywords('index'); // 屏蔽指定关键词
  1478. blockType(); // 屏蔽指定类别(视频/文章等)
  1479. // 解决屏蔽视频后,因为首页信息流太少而没有滚动条导致无法加载更多内容的问题
  1480. if (menu_value('menu_blockTypeVideo')) document.lastElementChild.appendChild(document.createElement('style')).textContent = '.Topstory-container{min-height: 1500px;}';
  1481. }
  1482. }
  1483. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址