tieba_hide_someone

屏蔽某些人的帖子

  1. // ==UserScript==
  2. // @name tieba_hide_someone
  3. // @description 屏蔽某些人的帖子
  4. // @include http://tieba.baidu.com/*
  5. // @exclude http://tieba.baidu.com/tb*
  6. // @exclude http://tieba.baidu.com/mo/*
  7. // @icon http://imgsrc.baidu.com/forum/pic/item/6fd108fb43166d229cb84fac452309f79152d2e2.png
  8. // @author congxz6688
  9. // @version 2014.8.14.2
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_registerMenuCommand
  13. // @namespace https://greatest.deepsurf.us/scripts/148
  14. // ==/UserScript==
  15.  
  16.  
  17. //脚本双存储相互恢复
  18. if (!localStorage.tiebaHideBlackList && GM_getValue("tiebaHideBlackList", "") != "") {
  19. localStorage.tiebaHideBlackList = GM_getValue("tiebaHideBlackList");
  20. }
  21. if (GM_getValue("tiebaHideBlackList", "") == "" && localStorage.tiebaHideBlackList) {
  22. GM_setValue("tiebaHideBlackList", localStorage.tiebaHideBlackList);
  23. }
  24. if (!localStorage.whiteUserIds && GM_getValue("whiteUserIds", "") != "") {
  25. localStorage.whiteUserIds = GM_getValue("whiteUserIds");
  26. }
  27. if (GM_getValue("whiteUserIds", "") == "" && localStorage.whiteUserIds) {
  28. GM_setValue("whiteUserIds", localStorage.whiteUserIds);
  29. }
  30.  
  31. //从存储的数据中提取黑白名单
  32. var getBlackList = GM_getValue("tiebaHideBlackList", "") != "" ? GM_getValue("tiebaHideBlackList").split(",") : [];
  33. var whiteUsIds = GM_getValue("whiteUserIds", "") != "" ? GM_getValue("whiteUserIds").split(",") : [];
  34.  
  35. //今天的日期
  36. var yuy = new Date();
  37. var fulltime = yuy.toLocaleDateString();
  38. var $ = unsafeWindow.$;
  39.  
  40. //当前日期、用户、已屏蔽之数据
  41. var userData = unsafeWindow.PageData;
  42. var userName = userData.user.name ? userData.user.name : userData.user.user_name;
  43. var HideToday = JSON.parse((localStorage["HideToday"]) ? localStorage["HideToday"] : "{}");
  44. HideToday[userName] = HideToday[userName] ? HideToday[userName] : [];
  45.  
  46. function addStyle(css) {
  47. document.head.appendChild(document.createElement("style")).textContent = css;
  48. }
  49.  
  50. function addNodeInsertedListener(elCssPath, handler, executeOnce, noStyle) {
  51. var animName = "anilanim",
  52. prefixList = ["-o-", "-ms-", "-khtml-", "-moz-", "-webkit-", ""],
  53. eventTypeList = ["animationstart", "webkitAnimationStart", "MSAnimationStart", "oAnimationStart"],
  54. forEach = function (array, func) {
  55. for (var i = 0, l = array.length; i < l; i++) {
  56. func(array[i]);
  57. }
  58. };
  59. if (!noStyle) {
  60. var css = elCssPath + "{",
  61. css2 = "";
  62. forEach(prefixList, function (prefix) {
  63. css += prefix + "animation-duration:.001s;" + prefix + "animation-name:" + animName + ";";
  64. css2 += "@" + prefix + "keyframes " + animName + "{from{opacity:.9;}to{opacity:1;}}";
  65. });
  66. css += "}" + css2;
  67. addStyle(css);
  68. }
  69. if (handler) {
  70. var bindedFunc = function (e) {
  71. var els = document.querySelectorAll(elCssPath),
  72. tar = e.target,
  73. match = false;
  74. if (els.length !== 0) {
  75. forEach(els, function (el) {
  76. if (tar === el) {
  77. if (executeOnce) {
  78. removeNodeInsertedListener(bindedFunc);
  79. }
  80. handler.call(tar, e);
  81. return;
  82. }
  83. });
  84. }
  85. };
  86. forEach(eventTypeList, function (eventType) {
  87. document.addEventListener(eventType, bindedFunc, false);
  88. });
  89. return bindedFunc;
  90. }
  91. }
  92. //移除精确监听
  93. function removeNodeInsertedListener(bindedFunc) {
  94. var eventTypeList = ["animationstart", "webkitAnimationStart", "MSAnimationStart", "oAnimationStart"],
  95. forEach = function (array, func) {
  96. for (var i = 0, l = array.length; i < l; i++) {
  97. func(array[i]);
  98. }
  99. };
  100. forEach(eventTypeList, function (eventType) {
  101. document.removeEventListener(eventType, bindedFunc, false);
  102. });
  103. }
  104. //逐一屏蔽函数
  105. function goHideOneByOne(nn, lp) {
  106. if (whiteUsIds.indexOf(userName) == -1) {
  107. if (HideToday.date != fulltime) {
  108. HideToday = {};
  109. HideToday.date = fulltime;
  110. HideToday[userName] = [];
  111. }
  112. getHiddenList = (HideToday[userName]) ? HideToday[userName] : [];
  113. if (getHiddenList.indexOf(getBlackList[nn]) == -1) {
  114. var postData = encodeURI("type=1&hide_un=" + getBlackList[nn] + "&ie=utf-8");
  115. var urll = "http://tieba.baidu.com/tphide/add";
  116. onebyone = new XMLHttpRequest();
  117. onebyone.open("POST", urll, true);
  118. onebyone.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  119. onebyone.setRequestHeader("Content-length", postData.length);
  120. onebyone.setRequestHeader("Connection", "close");
  121. onebyone.send(postData);
  122. onebyone.onreadystatechange = function () {
  123. if (onebyone.readyState == 4) {
  124. if (onebyone.status == 200) {
  125. var reTextTxt = JSON.parse(onebyone.responseText);
  126. console.log(fulltime + " 屏蔽 " + getBlackList[nn] + " " + reTextTxt.msg);
  127. ssw = HideToday[userName].push(getBlackList[nn]);
  128. if (nn == lp) {
  129. HideToday[userName] = getBlackList;
  130. localStorage["HideToday"] = JSON.stringify(HideToday);
  131. console.log(fulltime + " 眼中钉全部屏蔽完毕!");
  132. } else {
  133. localStorage["HideToday"] = JSON.stringify(HideToday);
  134. ns = nn + 1;
  135. setTimeout(function () {
  136. goHideOneByOne(ns, lp); //自调用,顺序循环
  137. }, 1000);
  138. }
  139. }
  140. }
  141. }
  142. } else {
  143. console.log(getBlackList[nn] + " 今天已经屏蔽过了。");
  144. if (nn == lp) {
  145. HideToday[userName] = getBlackList;
  146. localStorage["HideToday"] = JSON.stringify(HideToday);
  147. console.log(fulltime + " 眼中钉全部屏蔽完毕!");
  148. } else {
  149. ns = nn + 1;
  150. goHideOneByOne(ns, lp); //自调用,顺序循环
  151. }
  152. }
  153. }
  154. }
  155. //以用户脚本命令输入黑名单
  156. function hideSomeOneBlackSet() {
  157. if (getBlackList.toString() == "") {
  158. mess = "请输入屏蔽黑名单,以小写的逗号相互隔开,可带小写空格,或者是小写的引号:";
  159. caseShow = "坏人甲,坏人乙";
  160. } else {
  161. mess = "请修改屏蔽黑名单,以小写的逗号相互隔开,可带小写空格,或者是小写的引号:";
  162. caseShow = getBlackList.toString();
  163. }
  164. var getSetData = prompt(mess, caseShow);
  165. getBlackList = (getSetData == "坏人甲,坏人乙" || getSetData == "") ? [] : getSetData.replace(/,/g, ",").replace(/\s/g, "").replace(/["']/g, "").split(",");
  166. localStorage.tiebaHideBlackList = getBlackList.toString();
  167. GM_setValue("tiebaHideBlackList", getBlackList.toString());
  168. goHideOneByOne(0, getBlackList.length - 1);
  169. }
  170. //以用户脚本命令输入马甲白名单
  171. function hideSomeOneWhiteSet() {
  172. if (whiteUsIds.toString() == "") {
  173. mess = "请输入不执行本脚本的小号名单,以小写逗号相分隔,可带小写空格或小写引号,比如\r\n文科980195412是我的一个小号,当我用她登录时,不执行屏蔽:";
  174. caseShow = "文科980195412,xyz";
  175. } else {
  176. mess = "请修改不执行脚本的小号名单,以小写逗号相分隔,可带小写空格或小写引号:";
  177. caseShow = whiteUsIds.toString();
  178. }
  179. var getSetData = prompt(mess, caseShow);
  180. whiteUsIds = (getSetData == "文科980195412,xyz" || getSetData == "") ? [] : getSetData.replace(/,/g, ",").replace(/\s/g, "").replace(/["']/g, "").split(",");
  181. localStorage.whiteUserIds = whiteUsIds.toString();
  182. GM_setValue("whiteUserIds", whiteUsIds.toString());
  183. }
  184. GM_registerMenuCommand("tieba_Hide_SomeOne黑名单设置", hideSomeOneBlackSet);
  185. GM_registerMenuCommand("tieba_Hide_SomeOne白名单设置", hideSomeOneWhiteSet);
  186.  
  187. //判断条件 执行屏蔽动作
  188.  
  189. if (whiteUsIds.indexOf(userName) == -1 && (HideToday.date != fulltime || HideToday[userName].toString() != getBlackList.toString())) {
  190. goHideOneByOne(0, getBlackList.length - 1);
  191. }
  192.  
  193. if (whiteUsIds.indexOf(userName) == -1) {
  194. addNodeInsertedListener(".j_thread_list", function () { //帖子列表
  195. var Lhtml = $(this).find(".tb_icon_author").attr("title").match(/.*[::]\s?(.*)/)[1];
  196. if (getBlackList.indexOf(Lhtml) != -1) {
  197. $(this).remove();
  198. }
  199. });
  200. addNodeInsertedListener(".lzl_single_post", function () { //楼中楼
  201. var iUserIdhtml = $(this).find(".j_user_card").attr("username");
  202. if (getBlackList.indexOf(iUserIdhtml) != -1) {
  203. $(this).remove();
  204. }
  205. });
  206. addNodeInsertedListener(".j_feed_replyme", function () { //回复我的
  207. var iUserIdhtml = $(this).find(".replyme_user").text().replace(":", "");
  208. if (getBlackList.indexOf(iUserIdhtml) != -1) {
  209. $(this).remove();
  210. }
  211. });
  212. }