知乎浏览体验优化

优化PC端浏览未登录(不可用)状态下浏览知乎体验:动态移除登录(不可用)窗口、规避新标签页打开链接、首页登录(不可用)页重定向至热门页面;

  1. // ==UserScript==
  2. // @name 知乎浏览体验优化
  3. // @namespace https://gf.qytechs.cn/zh-CN/users/893587-limbopro
  4. // @version 0.0.7
  5. // @license CC BY-NC-SA 4.0
  6. // @description 优化PC端浏览未登录(不可用)状态下浏览知乎体验:动态移除登录(不可用)窗口、规避新标签页打开链接、首页登录(不可用)页重定向至热门页面;
  7. // @author limbopro
  8. // @match https://zhuanlan.zhihu.com/*
  9. // @match https://www.zhihu.com/*
  10. // @run-at document-end
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. var zhihu_id = "zhihux"; // 定义一个 CSS ID
  15. var zhihuAds = "div.css-1izy64v,[class='Card AppBanner'],.Footer,.Banner-link,div.Pc-word {display:none ! important; pointer-events: none !important;}"; // 广告样式优先级覆盖
  16. buttonClick("[class='Button Modal-closeButton Button--plain']", 10); // 模拟点击行为
  17. //buttonAppend("header[role='banner']", "清理中! ♻️", "undefined", "position:fixed; right:0px;", zhihu_id); // 在页面某个元素后面插入一个按钮
  18. cssAdsRemove(zhihuAds, 100, "hloyx"); // 在 HTML HEAD 标签部分插入一个样式, 用以覆盖广告元素
  19. rewriteToExplore(); // 若点击知乎首页按钮,则自动跳转到 Explore 页面 // 避免登录(不可用)操作
  20.  
  21. /*
  22. window.onload = hrefAttributeSet(500, zhihu_id); // 将所有属性为 _blank 的 a 标签,替换为 _self;避免点击当前页面内的链接在新标签页打开;
  23. window.onload = addListener("a[class*='css-'],button[class='Button ContentItem-action Button--plain Button--withIcon Button--withLabel']", () => { hrefAttributeSet(500, zhihu_id) }); // 为热门页面的分类添加监听器,触发 hrefAttributeSet 动作;
  24. // 循环监控整个页面可滚动高度 scrollHeight 是否变化
  25. var body_scrollHeightCheck = setInterval(() => {
  26. var body_scrollHeight_then = document.body.scrollHeight;
  27. setTimeout(() => {
  28. var body_scrollHeight_now = document.body.scrollHeight;
  29. if (body_scrollHeight_now > body_scrollHeight_then) {
  30. hrefAttributeSet(500, zhihu_id);
  31. }
  32. }, 500);
  33. }, 500);
  34.  
  35.  
  36. // 循环监控答案页面可滚动高度 scrollHeight 是否变化
  37. var comment_scrollHeightCheck = setInterval(() => {
  38. let comment = document.querySelectorAll("div.CommentListV2"); // div.CommentListV2 是相应触发按钮选择器
  39. if (comment.length > 0) {
  40. var comment_scrollHeight_then = comment[0].scrollHeight;
  41. setTimeout(() => {
  42. var comment_scrollHeight_now = comment[0].scrollHeight;
  43. if (comment_scrollHeight_now > comment_scrollHeight_then) {
  44. hrefAttributeSet(500, zhihu_id);
  45. }
  46. }, 500)
  47. }
  48. }, 500)
  49. */
  50.  
  51. // 循环模拟模拟点击
  52. function buttonClick(selector, times) {
  53. var initCount = 0;
  54. var loop = setInterval(() => {
  55. var ele = document.querySelectorAll(selector);
  56. if (ele.length > 0) {
  57. ele[0].click()
  58. }
  59. initCount += 1;
  60. if (initCount == times) {
  61. clearInterval(loop);
  62. }
  63. }, 0)
  64. }
  65.  
  66. // 在页面动态插入按钮并赋予 onclick 属性
  67. function buttonAppend(ele, text, onclick, position, id) {
  68. var button = document.createElement("button");
  69. button.innerHTML = text;
  70. button.setAttribute("onclick", onclick);
  71. button.setAttribute("id", id);
  72. var button_style_values = position + "padding: 6px 6px 6px 6px; display: inline-block; " +
  73. "font-size: 15px; color:white; z-index:114154; border-right: 6px solid #38a3fd !important; " +
  74. "border-left: #292f33 !important; border-top: #292f33 !important; " +
  75. "border-bottom: #292f33 !important; background: black; " +
  76. "border-radius: 0px 0px 0px 0px; margin-bottom: 10px; " +
  77. "font-weight: 800 !important; " +
  78. "text-align: right !important;"
  79. button.setAttribute("style", button_style_values);
  80. var here = document.querySelectorAll(ele);
  81. if (here.length > 0) {
  82. here[0].insertBefore(button, here[0].childNodes[3])
  83. //here[0].appendChild(button);
  84. }
  85. }
  86.  
  87. // 动态创建引用内部资源 内嵌式样式 内嵌式脚本
  88. function cssAdsRemove(newstyle, delaytime, id) {
  89. setTimeout(() => {
  90. var creatcss = document.createElement("style");
  91. creatcss.id = id;
  92. creatcss.innerHTML = newstyle;
  93. document.getElementsByTagName('head')[0].appendChild(creatcss)
  94. }, delaytime);
  95. }
  96.  
  97. function rewriteToExplore() {
  98. let url = document.location.href;
  99. let cssSelector = "a[href='//www.zhihu.com/'],a[href='//www.zhihu.com'],a[href='https://www.zhihu.com']";
  100. let rewrite_url = "https://www.zhihu.com/knowledge-plan/hot-question/hot/0/hour";
  101. let reg = /^https:\/\/www.zhihu.com\/signin.*/gi;
  102. if (url.search(reg) !== -1) {
  103. window.location = rewrite_url;
  104. }
  105.  
  106. setTimeout(() => { // 延时执行函数优化
  107. var ele = document.querySelectorAll(cssSelector)
  108. if (ele.length > 0) {
  109. let i;
  110. for (i = 0; i < ele.length; i++) {
  111. ele[i].href = rewrite_url;
  112. }
  113. }
  114. }, 300);
  115. }
  116.  
  117. /*
  118. function rewriteToExplore() { //跳转至热门话题 Explore 或 随机
  119. var url = document.location.href;
  120. var url_list = [
  121. "https://www.zhihu.com/knowledge-plan/hot-question/hot/",
  122. ]
  123. var rand = Math.floor(Math.random() * url_list.length);
  124. var url_random = url_list[rand];
  125. var reg = /^https:\/\/www.zhihu.com\/signin/gi;
  126. if (url.search(reg) !== -1) {
  127. window.location = url_random;
  128. }
  129. }
  130. */
  131.  
  132. // 禁止新页面跳转另一种实现 循环
  133. function hrefAttributeSet(time, id) {
  134. document.getElementById(id).style.background = "black";
  135. document.getElementById(id).innerHTML = "清理中! ♻️";
  136. setTimeout(() => {
  137. // 监控页面是否有新的 button
  138. let selector = "button[class*='Button PaginationButton']";
  139. let ele_button = document.querySelectorAll(selector);
  140. if (ele_button.length > 0) {
  141. window.onload = addListener(selector, () => { hrefAttributeSet(time, id) });
  142. }
  143. let times = 0;
  144. let loop = setInterval(() => {
  145. // 修改属性
  146. times += 1;
  147. let href = document.querySelectorAll("a");
  148. let i;
  149. for (i = 0; i < href.length; i++) {
  150. if (href[i].target == "_blank") {
  151. href[i].setAttribute("target", "_self");
  152. }
  153. }
  154. let href_Length = document.querySelectorAll("a[target='_blank']").length;
  155. if (href_Length === 0 && times >= 2) {
  156. clearInterval(loop);
  157. if (document.getElementById(id)) {
  158. document.getElementById(id).innerHTML = "100%! ♻️";
  159. document.getElementById(id).style.background = "green";
  160. }
  161. }
  162. }, time)
  163. }, time)
  164. }
  165.  
  166. /* 添加监听器 */
  167. function addListener(selector, funx) {
  168. setTimeout(() => {
  169. var ele = document.querySelectorAll(selector);
  170. for (let index = 0; index < ele.length; index++) {
  171. ele[index].addEventListener("click", funx, false)
  172. }
  173. }, 1000)
  174. }

QingJ © 2025

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