Zhihu Original Filter

知乎原创过滤器

  1. // ==UserScript==
  2. // @name Zhihu Original Filter
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 知乎原创过滤器
  6. // @author xi2008wang
  7. // @match https://www.zhihu.com/follow
  8. // @require https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
  9. // @run-at document-end
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. var $ = $ || window.$;
  14. var original_set = ['回答了', '发表了', '添加了', '专栏更新了'];
  15. var zhihu_filter_sw = false;
  16.  
  17. function waitForKeyElements(
  18. selectorTxt,
  19. /* Required: The jQuery selector string that
  20. specifies the desired element(s).
  21. */
  22. actionFunction,
  23. /* Required: The code to run when elements are
  24. found. It is passed a jNode to the matched
  25. element.
  26. */
  27. bWaitOnce,
  28. /* Optional: If false, will continue to scan for
  29. new elements even after the first match is
  30. found.
  31. */
  32. iframeSelector
  33. /* Optional: If set, identifies the iframe to
  34. search.
  35. */
  36. ) {
  37. var targetNodes, btargetsFound;
  38.  
  39. if (typeof iframeSelector == "undefined") {
  40. targetNodes = $(selectorTxt);
  41. } else {
  42. targetNodes = $(iframeSelector).contents().find(selectorTxt);
  43. }
  44.  
  45. if (targetNodes && targetNodes.length > 0) {
  46. btargetsFound = true;
  47. /*--- Found target node(s). Go through each and act if they
  48. are new.
  49. */
  50. targetNodes.each(function() {
  51. var jThis = $(this);
  52. var alreadyFound = jThis.data('alreadyFound') || false;
  53.  
  54. if (!alreadyFound) {
  55. //--- Call the payload function.
  56. var cancelFound = actionFunction(jThis);
  57. if (cancelFound) {
  58. btargetsFound = false;
  59. } else {
  60. jThis.data('alreadyFound', true);
  61. }
  62. }
  63. });
  64. } else {
  65. btargetsFound = false;
  66. }
  67.  
  68. //--- Get the timer-control variable for this selector.
  69. var controlObj = waitForKeyElements.controlObj || {};
  70. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  71. var timeControl = controlObj[controlKey];
  72.  
  73. //--- Now set or clear the timer as appropriate.
  74. if (btargetsFound && bWaitOnce && timeControl) {
  75. //--- The only condition where we need to clear the timer.
  76. clearInterval(timeControl);
  77. delete controlObj[controlKey]
  78. } else {
  79. //--- Set a timer, if needed.
  80. if (!timeControl) {
  81. timeControl = setInterval(function() {
  82. waitForKeyElements(selectorTxt,
  83. actionFunction,
  84. bWaitOnce,
  85. iframeSelector
  86. );
  87. },
  88. 300
  89. );
  90. controlObj[controlKey] = timeControl;
  91. }
  92. }
  93. waitForKeyElements.controlObj = controlObj;
  94. }
  95.  
  96. function zhihu_filter_all(feeds) {
  97. feeds.each(function() {
  98. var feed = $(this);
  99. zhihu_filter(feed);
  100. });
  101. }
  102.  
  103. function zhihu_filter(feed) {
  104. var feed_data = feed.find('.FeedSource-firstline').text();
  105. var found = false;
  106. $.each(original_set, function(idx, original) {
  107. // 遍历找出原创feed
  108. if (feed_data.indexOf(original) >= 0) {
  109. found = true;
  110. return;
  111. }
  112. });
  113.  
  114. // 隐藏不关心feed
  115. if (zhihu_filter_sw && !found) {
  116. feed.parent().hide();
  117. } else {
  118. feed.parent().show();
  119. }
  120.  
  121. // 广告feed也隐藏
  122. $('.TopstoryItem--advertCard').hide();
  123. }
  124.  
  125. function append_switch() {
  126. var b = $('<button id="zhihu_filter_sw" style="position: fixed;top: 60px;right: 20px;">全部</button>');
  127. b.prependTo('body');
  128.  
  129. $('#zhihu_filter_sw').click(function() {
  130. if ($(this).text() == "全部") {
  131. // 单击时全部过滤
  132. $(this).text("原创");
  133. zhihu_filter_sw = true;
  134. zhihu_filter_all($('.Feed'));
  135. } else {
  136. $(this).text("全部");
  137. zhihu_filter_sw = false;
  138. zhihu_filter_all($('.Feed'));
  139. }
  140. });
  141. }
  142.  
  143. (function() {
  144. 'use strict';
  145.  
  146. // 附加过滤切换按钮
  147. append_switch();
  148.  
  149. // 开始实时过滤
  150. waitForKeyElements(
  151. ".Feed", zhihu_filter
  152. );
  153.  
  154. })();

QingJ © 2025

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