WeiboBlocker

try to take over the world!

  1. // ==UserScript==
  2. // @name WeiboBlocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description try to take over the world!
  6. // @author yuyue9284@gmail.com
  7. // @include http://weibo.com*
  8. // @grant none
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
  10. // ==/UserScript==
  11.  
  12.  
  13. var block = [];
  14.  
  15.  
  16. // 增加关键词
  17. function add_word() {
  18. var word = prompt('请输入');
  19. switch (word) {
  20. case "clear":
  21. localStorage.removeItem('block');
  22. block = [];
  23. break;
  24. case "show":
  25. alert(localStorage.block);
  26. break;
  27. default:
  28. block.push(word);
  29. localStorage.block = JSON.stringify(block);
  30. }
  31. action();
  32. }
  33.  
  34. // 初始化
  35. var init = function() {
  36. if (localStorage.block !== undefined) {
  37. block = JSON.parse(localStorage.block);
  38. } else {
  39. block = ['tf', 'TF', 'Tf', '王源', '易烊千玺', '王俊凯'];
  40. localStorage.block = JSON.stringify(block);
  41. }
  42. $(".gn_header.clearfix").off('click', add_word).on('click', add_word);
  43. action();
  44. };
  45.  
  46.  
  47. $(document).ready(init);
  48.  
  49. // 屏蔽函数本体
  50. var action = function() {
  51. var lst = $(".WB_cardwrap.WB_feed_type.S_bg2").map(function() {
  52. return $(this);
  53. });
  54. for (var i = lst.length - 1; i >= 0; i--) {
  55. for (var j = block.length - 1; j >= 0; j--) {
  56. var content = lst[i].text();
  57. if (content.includes(block[j])) {
  58. lst[i].css("display", "none");
  59. // console.log(content);
  60. break;
  61. }
  62. }
  63. }
  64. };
  65. waitForKeyElements(".WB_cardwrap.WB_feed_type.S_bg2", action);
  66.  
  67.  
  68.  
  69.  
  70.  
  71. // Script from https://gist.github.com/raw/2625891/waitForKeyElements.js
  72. // used to wait for the element seleted by jQuery loading
  73.  
  74. function waitForKeyElements(
  75. selectorTxt,
  76. /* Required: The jQuery selector string that
  77. specifies the desired element(s).
  78. */
  79. actionFunction,
  80. /* Required: The code to run when elements are
  81. found. It is passed a jNode to the matched
  82. element.
  83. */
  84. bWaitOnce,
  85. /* Optional: If false, will continue to scan for
  86. new elements even after the first match is
  87. found.
  88. */
  89. iframeSelector
  90. /* Optional: If set, identifies the iframe to
  91. search.
  92. */
  93. ) {
  94. var targetNodes, btargetsFound;
  95.  
  96. if (typeof iframeSelector == "undefined")
  97. targetNodes = $(selectorTxt);
  98. else
  99. targetNodes = $(iframeSelector).contents()
  100. .find(selectorTxt);
  101.  
  102. if (targetNodes && targetNodes.length > 0) {
  103. btargetsFound = true;
  104. /*--- Found target node(s). Go through each and act if they
  105. are new.
  106. */
  107. targetNodes.each(function() {
  108. var jThis = $(this);
  109. var alreadyFound = jThis.data('alreadyFound') || false;
  110.  
  111. if (!alreadyFound) {
  112. //--- Call the payload function.
  113. var cancelFound = actionFunction(jThis);
  114. if (cancelFound)
  115. btargetsFound = false;
  116. else
  117. jThis.data('alreadyFound', true);
  118. }
  119. });
  120. } else {
  121. btargetsFound = false;
  122. }
  123.  
  124. //--- Get the timer-control variable for this selector.
  125. var controlObj = waitForKeyElements.controlObj || {};
  126. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  127. var timeControl = controlObj[controlKey];
  128.  
  129. //--- Now set or clear the timer as appropriate.
  130. if (btargetsFound && bWaitOnce && timeControl) {
  131. //--- The only condition where we need to clear the timer.
  132. clearInterval(timeControl);
  133. delete controlObj[controlKey];
  134. } else {
  135. //--- Set a timer, if needed.
  136. if (!timeControl) {
  137. timeControl = setInterval(function() {
  138. waitForKeyElements(selectorTxt,
  139. actionFunction,
  140. bWaitOnce,
  141. iframeSelector
  142. );
  143. },
  144. 300
  145. );
  146. controlObj[controlKey] = timeControl;
  147. }
  148. }
  149. waitForKeyElements.controlObj = controlObj;
  150. }