WeiboBlocker

try to take over the world!

当前为 2017-03-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WeiboBlocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  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(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. };
  44.  
  45.  
  46. $(document).ready(init);
  47.  
  48. // 屏蔽函数本体
  49. var action = function() {
  50. var lst = $(".WB_cardwrap.WB_feed_type.S_bg2").map(function() {
  51. return $(this);
  52. });
  53. for (var i = lst.length - 1; i >= 0; i--) {
  54. for (var j = block.length - 1; j >= 0; j--) {
  55. var content = lst[i].text();
  56. if (content.includes(block[j])) {
  57. lst[i].css("display", "none");
  58. // console.log(content);
  59. break;
  60. }
  61. }
  62. }
  63. };
  64. waitForKeyElements(".WB_cardwrap.WB_feed_type.S_bg2", action);
  65.  
  66.  
  67.  
  68.  
  69.  
  70. // Script from https://gist.github.com/raw/2625891/waitForKeyElements.js
  71. // used to wait for the element seleted by jQuery loading
  72.  
  73. function waitForKeyElements(
  74. selectorTxt,
  75. /* Required: The jQuery selector string that
  76. specifies the desired element(s).
  77. */
  78. actionFunction,
  79. /* Required: The code to run when elements are
  80. found. It is passed a jNode to the matched
  81. element.
  82. */
  83. bWaitOnce,
  84. /* Optional: If false, will continue to scan for
  85. new elements even after the first match is
  86. found.
  87. */
  88. iframeSelector
  89. /* Optional: If set, identifies the iframe to
  90. search.
  91. */
  92. ) {
  93. var targetNodes, btargetsFound;
  94.  
  95. if (typeof iframeSelector == "undefined")
  96. targetNodes = $(selectorTxt);
  97. else
  98. targetNodes = $(iframeSelector).contents()
  99. .find(selectorTxt);
  100.  
  101. if (targetNodes && targetNodes.length > 0) {
  102. btargetsFound = true;
  103. /*--- Found target node(s). Go through each and act if they
  104. are new.
  105. */
  106. targetNodes.each(function() {
  107. var jThis = $(this);
  108. var alreadyFound = jThis.data('alreadyFound') || false;
  109.  
  110. if (!alreadyFound) {
  111. //--- Call the payload function.
  112. var cancelFound = actionFunction(jThis);
  113. if (cancelFound)
  114. btargetsFound = false;
  115. else
  116. jThis.data('alreadyFound', true);
  117. }
  118. });
  119. } else {
  120. btargetsFound = false;
  121. }
  122.  
  123. //--- Get the timer-control variable for this selector.
  124. var controlObj = waitForKeyElements.controlObj || {};
  125. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  126. var timeControl = controlObj[controlKey];
  127.  
  128. //--- Now set or clear the timer as appropriate.
  129. if (btargetsFound && bWaitOnce && timeControl) {
  130. //--- The only condition where we need to clear the timer.
  131. clearInterval(timeControl);
  132. delete controlObj[controlKey];
  133. } else {
  134. //--- Set a timer, if needed.
  135. if (!timeControl) {
  136. timeControl = setInterval(function() {
  137. waitForKeyElements(selectorTxt,
  138. actionFunction,
  139. bWaitOnce,
  140. iframeSelector
  141. );
  142. },
  143. 300
  144. );
  145. controlObj[controlKey] = timeControl;
  146. }
  147. }
  148. waitForKeyElements.controlObj = controlObj;
  149. }