知乎免登录-关怀模式

老子就不登录

  1. // ==UserScript==
  2. // @name 知乎免登录-关怀模式
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-06-25
  5. // @description 老子就不登录
  6. // @author maoanran@gmail.com
  7. // @match https://www.zhihu.com/explore
  8. // @grant GM_addStyle
  9. // @match *://www.zhihu.com/*
  10. // @match *://zhuanlan.zhihu.com/*
  11. // @exclude https://www.zhihu.com/signin*
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. var viewAllButton = false;
  18.  
  19. // Function to process links and update their href
  20. function processLinks(links) {
  21. links.forEach(link => {
  22. if (link.href.startsWith("https://www.zhihu.com/question")){
  23. link.href = link.href.replace("https://www.zhihu.com/question", "https://www.zhihu.com/aria/question");
  24. }
  25. if (window.location.href.startsWith("https://www.zhihu.com/aria/question") && isZhihuAriaQuestionAnswerUrl(link.href)) {
  26. const newLink = document.createElement("button");
  27. newLink.textContent = "跳转看评论";
  28. newLink.onclick = function(){
  29. window.open(link.href.replace("https://www.zhihu.com/aria/question", "https://www.zhihu.com/question"))
  30. };
  31.  
  32. // Insert the new link after the existing one
  33. link.parentNode.insertBefore(newLink, link.nextSibling);
  34. link.parentNode.insertBefore(document.createElement("br"), link.nextSibling);
  35. link.parentNode.insertBefore(document.createElement("br"), link.nextSibling);
  36. }
  37. });
  38. }
  39.  
  40. function isZhihuAriaQuestionAnswerUrl(url) {
  41. // Define the regular expression pattern
  42. const pattern = /^https:\/\/www\.zhihu\.com\/aria\/question\/\d+\/answer\/\d+$/;
  43. // Test the URL against the pattern
  44. return pattern.test(url);
  45. }
  46.  
  47. // Initial processing of existing links
  48. var div_list = document.querySelectorAll('A'); // returns NodeList
  49. processLinks([...div_list]); // converts NodeList to Array and processes links
  50.  
  51. // Create a MutationObserver to watch for new nodes being added to the document
  52. const observer = new MutationObserver(mutations => {
  53. mutations.forEach(mutation => {
  54. mutation.addedNodes.forEach(node => {
  55. if (node.nodeType === 1) { // Ensure the added node is an element
  56. if (node.tagName === 'A') {
  57. processLinks([node]); // Process the new link element
  58. } else {
  59. // Process all new links within the added node
  60. var newLinks = node.querySelectorAll('A');
  61. processLinks([...newLinks]); // converts NodeList to Array and processes links
  62. }
  63.  
  64.  
  65. var viewAll = document.querySelector('div.ViewAll');
  66. if (viewAll != null && !viewAllButton) {
  67. viewAllButton = true;
  68. viewAll.textContent = "跳转到关怀模式查看全部";
  69. viewAll.onclick = function(){
  70. window.open(window.location.href.replace("https://www.zhihu.com/question", "https://www.zhihu.com/aria/question"))
  71. };
  72. }
  73. var header = document.querySelector('header.AppHeader');
  74. if (header != null) {
  75. document.querySelector('header.AppHeader').style = "display: block !important"
  76. }
  77. }
  78. });
  79. });
  80. });
  81.  
  82. // Configure the observer to watch for child nodes being added to the body
  83. observer.observe(document.body, { childList: true, subtree: true });
  84.  
  85.  
  86. GM_addStyle(`
  87. html{
  88. overflow: scroll !important;
  89. }
  90.  
  91. header.AppHeader {
  92. display: none !important;
  93. }
  94. .Modal-wrapper,.Modal-enter-done{
  95. display: none !important;
  96. }
  97. `)
  98. })();