remove the posts which make you sick

移除讨厌鬼的帖子

当前为 2015-07-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name remove the posts which make you sick
  3. // @author burningall
  4. // @description 移除讨厌鬼的帖子
  5. // @version 2015.7.4.2.0
  6. // @include *tieba.baidu.com/p/*
  7. // @include *tieba.baidu.com/*
  8. // @include *tieba.baidu.com/f?*
  9. // @grant GM_addStyle
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_listValues
  13. // @grant GM_deleteValue
  14. // @supportURL http://www.burningall.com
  15. // @contributionURL troy450409405@gmail.com|alipay.com
  16. // @namespace https://greasyfork.org/zh-CN/users/3400-axetroy
  17. // ==/UserScript==
  18.  
  19. //============快捷键==========
  20. //【Ctrl】+【F3】-----调出控制面板
  21. //============样式区==========
  22. var style='\
  23. body{\
  24. -webkit-backface-visibility: hidden;\
  25. }\
  26. .blur{\
  27. -webkit-filter: blur(10px);\
  28. -moz-filter: blur(10px);\
  29. -o-filter: blur(10px);\
  30. -ms-filter: blur(10px);\
  31. filter: blur(10px);\
  32. }\
  33. #pannal-troy{\
  34. width:200px;\
  35. height:auto;\
  36. background:#303030;\
  37. color:#fff;\
  38. position:fixed;\
  39. z-index:1000000000;\
  40. text-align:center;\
  41. }\
  42. #pannal-troy>div{\
  43. margin:10px 0;\
  44. }\
  45. #pannal-troy input{\
  46. color:#3e3e3e;\
  47. }\
  48. #pannal-troy h3{\
  49. color:rgb(0, 255, 226);\
  50. }\
  51. #pannal-setting input[type=range]{\
  52. width:80%;\
  53. }\
  54. #fn input{\
  55. padding:5px;\
  56. margin:0 5px;\
  57. border:none;\
  58. cursor:pointer;\
  59. }\
  60. #fn input:hover{\
  61. background:#2A959D;\
  62. color:#fff;\
  63. }\
  64. #pannal-troy>span{\
  65. position:absolute;\
  66. padding:0 10px;\
  67. top:0;\
  68. right:0;\
  69. cursor:pointer;\
  70. opacity:0.8;\
  71. background:#fff;\
  72. color:#303030;\
  73. }\
  74. #blockWay{\
  75. color:#3e3e3e;\
  76. border:none;\
  77. }\
  78. #mars{\
  79. position:fixed;\
  80. width:100%;\
  81. height:100%;\
  82. background:rgba(155, 155, 155,0.5);\
  83. top:0;\
  84. left:0;\
  85. z-index:999999999;\
  86. }\
  87. #showList{\
  88. position:relative;\
  89. overflow-y:auto;\
  90. width:100%;\
  91. max-height:115px;\
  92. }\
  93. #showList>li{\
  94. width:100%;\
  95. height:18px;\
  96. clear:both;\
  97. }\
  98. .key{\
  99. float:left;\
  100. clear:both;\
  101. }\
  102. .deletThis{\
  103. float:right;\
  104. cursor:pointer;\
  105. }\
  106. .disable-btn{\
  107. background:#6B6B6B;\
  108. cursor:not-allowed;\
  109. }\
  110. #addBlackList input{\
  111. width:80%;\
  112. }\
  113. ';
  114. GM_addStyle(style);
  115. //============公共函数区==========
  116. function addEvent(obj, event, fn) {
  117. return obj.addEventListener ? obj.addEventListener(event, fn, false) : obj.attachEventListener('on' + event, fn);
  118. };
  119. function getStyle(obj, attr) {
  120. return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
  121. };
  122. function $(id) {
  123. return document.getElementById(id)
  124. };
  125. function getSize(attr){
  126. return document.documentElement[attr] ? document.documentElement[attr] : document.body[attr]
  127. }
  128. function uniqueArray(data){
  129. data = data || [];
  130. var a = {};
  131. for (var i=0; i<data.length; i++) {
  132. var v = data[i];
  133. if (typeof(a[v]) == 'undefined'){
  134. a[v] = 1;
  135. }
  136. };
  137. data.length=0;
  138. for (var i in a){
  139. data[data.length] = i;
  140. }
  141. return data;
  142. }
  143. //============主要代码区==========
  144. function juggUrl(){
  145. var url=location.href;
  146. var postIn=/.*tieba.baidu.com\/p\/.*/ig;
  147. var postList=/.*tieba.baidu.com\/(f\?.*|[^p])/ig;
  148. if( postIn.test(url) ){//如果是帖子内
  149. return 1;
  150. }else if( postList.test(url) ){//如果在帖子列表
  151. return 2;
  152. }
  153. }
  154. function createList(){
  155. $('showList').innerHTML='';
  156. var li='';
  157. for(var i=0;i<GM_listValues().length;i++){
  158. //控制台输出 键名:键值
  159. var key = GM_listValues()[i];
  160. var value = GM_getValue( GM_listValues()[i],'');
  161. if( (typeof value =='number' && value>1000) || value==''){
  162. console.log( GM_listValues()[i] + ":" + GM_getValue(GM_listValues()[i],'') );
  163. li += '<li class="show_list" data="'+ key +'">'+'<span class="key">'+key+'</span>'+'<input class="deletThis" type="button" value="删除"/>'+'</li>';
  164. }//if
  165. }//for
  166. $('showList').innerHTML=li;
  167. var aDeleBtn=document.querySelectorAll('.deletThis');
  168. for(var i=0;i<aDeleBtn.length;i++){
  169. aDeleBtn[i].onclick=function(){
  170. var thisKey = this.parentNode.getAttribute('data');//获取当前结点的key
  171. GM_deleteValue( thisKey );//删除变量
  172. this.parentNode.remove( this.parentNode );//删除节点
  173. }//clickb
  174. }//for
  175. };
  176. function marks(obj){
  177. if(obj.querySelectorAll('mar').length>=1){
  178. return;
  179. }
  180. var oMar = document.createElement('div');
  181. oMar.className = 'mar';
  182. oMar.innerHTML = '屏蔽';
  183. obj.style.position = 'relative';
  184. obj.style.webkitFilter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//chrome
  185. obj.style.filter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//firefox
  186. obj.style.oFilter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//opera
  187. obj.style.msFilter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//IE
  188. obj.style.webkitBackfaceVisibility = 'hidden';
  189. obj.appendChild( oMar );
  190. oMar.style.width = parseInt( getStyle(obj,'width') ) + 'px';
  191. oMar.style.height = parseInt( getStyle(obj,'height') ) + 'px';
  192. oMar.style.background = GM_getValue('setting-col','#fff');
  193. oMar.style.position = 'absolute';
  194. oMar.style.top = '0';
  195. oMar.style.left = '0';
  196. oMar.style.zIndex = '999999998';
  197. oMar.style.opacity = GM_getValue('setting-opa','0.8');
  198. oMar.style.filter = 'alpha(opacity=' + GM_getValue('setting-opa','0.8')*100 +')';
  199. };
  200. function blockMod(){
  201. if( GM_getValue('setting-blockWay','遮罩屏蔽')=='遮罩屏蔽' ){
  202. return 1;//遮罩模式
  203. }else{
  204. return 2;//删除节点
  205. }
  206. }
  207. function btclick(This,list,n){
  208. var _thisTop,_thisUserId,_thisUserName,nowUserId,nowUserName;
  209. if(n=='postList'){
  210. _thisTop = This.parentNode.parentNode.parentNode.parentNode;
  211. _thisUserId = JSON.parse(_thisTop.getAttribute('data-field')).id;//当前id
  212. _thisUserName = JSON.parse(_thisTop.getAttribute('data-field')).author_name;//当前userName
  213. }else if(n=='post'){
  214. _thisTop = This.parentNode.parentNode.parentNode;
  215. _thisUserId = JSON.parse(_thisTop.getAttribute('data-field')).author.user_id;//当前id
  216. _thisUserName = JSON.parse(_thisTop.getAttribute('data-field')).author.user_name;//当前userName
  217. }
  218. if ( confirm('是否屏蔽') == true) { //如果按下确认
  219. GM_setValue( _thisUserName,_thisUserId );
  220. for (var j = 0; j < list.length; j++) {//循环匹配是否符合
  221. var userInfo = JSON.parse(list[j].getAttribute('data-field'))
  222. if(n=='postList'){
  223. nowUserId = userInfo.id;
  224. nowUserName = userInfo.author_name;
  225. }else if(n=='post'){
  226. nowUserId = userInfo.author.user_id;
  227. nowUserName = userInfo.author.user_name;
  228. }
  229. if (nowUserId == _thisUserId || nowUserName==_thisUserName) {//匹配成功
  230. if( blockMod()==1 ){//遮罩模式
  231. marks(list[j]);
  232. }else{//删除节点模式
  233. list[j].style.display = 'none';
  234. }
  235. }//匹配是否符
  236. } //for
  237. }//if如果按下确认
  238. }
  239. function block(name,id,list,I){
  240. for(var j=0;j<GM_listValues().length;j++){
  241. if( GM_listValues()[j] == name || GM_getValue(GM_listValues()[j],'') == id ){//如果匹配到
  242. if( blockMod()==1 ){//遮罩模式
  243. marks(list[I]);
  244. }else{//删除节点模式
  245. list[I].style.display = 'none';
  246. }
  247. }//匹配是否符合
  248. }//for,遍历屏蔽
  249. }
  250. function init() {//初始化程序
  251. //先清空
  252. var aBlockBtn = document.querySelectorAll('.shutup-post,.shutup-list,.mar')
  253. for(var i=0;i<aBlockBtn.length;i++){
  254. aBlockBtn[i].remove(this);
  255. }
  256. //=======帖子内==========
  257. if( juggUrl()==1 ){
  258. var aUsers = document.querySelectorAll('#j_p_postlist>div[data-field]');//获取列表
  259. var aName = document.querySelectorAll('.l_post .d_name');//屏蔽按钮将要插入的位置
  260. for (var i = 0; i < aUsers.length; i++) {
  261. //获取id和name
  262. var userName = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_name;//第i个userName
  263. var userId = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_id;//第i个userId
  264. //添加样式
  265. aName[i].style.background = '#303030';
  266. //添加点击事件
  267. aName[i].onclick=function(){
  268. var this_=this;
  269. btclick(this_,aUsers,'post');
  270. }
  271. //页面加载开始屏蔽
  272. block(userName,userId,aUsers,i);
  273. } //for,遍历所有节点
  274. //=======帖子列表==========
  275. }else if( juggUrl()==2 ){
  276. var aUsers=document.querySelectorAll('#thread_list>li[data-field]');//获取列表
  277. var aName = document.querySelectorAll('#thread_list>li[data-field] .threadlist_lz>.threadlist_author');//屏蔽按钮将要插入的位置
  278. for(var i=0;i<aUsers.length;i++){
  279. //获取id和name
  280. var userName = JSON.parse(aUsers[i].getAttribute('data-field')).author_name;//第i个userName
  281. var userId = JSON.parse(aUsers[i].getAttribute('data-field')).id;//第i个userId
  282. //添加样式
  283. aName[i].style.background = '#303030';
  284. //添加点击事件
  285. aName[i].onclick=function(){
  286. var this_=this;
  287. btclick(this_,aUsers,'postList');
  288. }
  289. //页面加载开始屏蔽
  290. block(userName,userId,aUsers,i)
  291. }//for,遍历所有节点
  292. }//在帖子列表
  293. }//初始化
  294. function autoTips(){
  295. var aUsers,userName,userId;
  296. if( juggUrl()==1 ){//帖子内
  297. var aTipsArr = new Array();
  298. var newString = '';
  299. aUsers = document.querySelectorAll('#j_p_postlist>div[data-field]');//获取列表
  300. for(var i=0;i<aUsers.length;i++){
  301. userName = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_name;//第i个userName
  302. userId = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_id;//第i个userId
  303. aTipsArr.push( userName );
  304. }
  305. uniqueArray( aTipsArr );
  306. for(var i=0;i<aTipsArr.length;i++){
  307. newString += '<option value="' + aTipsArr[i] + '" />';
  308. }
  309. lst.innerHTML = newString;
  310. }else{//帖子列表
  311. var aTipsArr = new Array();
  312. var newString = '';
  313. aUsers=document.querySelectorAll('#thread_list>li[data-field]');//获取列表
  314. for(var i=0;i<aUsers.length;i++){
  315. userName = JSON.parse(aUsers[i].getAttribute('data-field')).author_name;//第i个userName
  316. userId = JSON.parse(aUsers[i].getAttribute('data-field')).id;//第i个userId
  317. aTipsArr.push( userName );
  318. }
  319. uniqueArray( aTipsArr );
  320. for(var i=0;i<aTipsArr.length;i++){
  321. newString += '<option value="' + aTipsArr[i] + '" />';
  322. }
  323. lst.innerHTML = newString;
  324. }//if....else
  325. }
  326. //============执行区==========
  327. addEvent(window,'load',function(){
  328. init();
  329. })
  330. //跟随翻页加载
  331. var aUsers,aShut;
  332. addEvent(window,'scroll',function(){
  333. if( getSize('scrollTop') == 0){
  334. if( juggUrl()==1 ){//帖子内
  335. aUsers = document.querySelectorAll('#j_p_postlist>div[data-field]');
  336. aShut = document.querySelectorAll('.shutup-post');
  337. }else if(juggUrl()==2){//帖子列表
  338. aUsers=document.querySelectorAll('#thread_list>li[data-field]');
  339. aShut = document.querySelectorAll('.shutup-list');
  340. }
  341. if(aShut.length <= aUsers.length){//如果不存在屏蔽按钮
  342. init();
  343. }
  344. }//如果滚动到顶部
  345. })
  346. //============控制面板以及相关==========
  347. addEvent(window,'resize',function(){
  348. if( $('pannal-troy') ){
  349. $('pannal-troy').style.top = ( getSize('clientHeight') - $('pannal-troy').offsetHeight )/2 + 'px';
  350. $('pannal-troy').style.left = ( getSize('clientWidth') - $('pannal-troy').offsetWidth )/2 + 'px';
  351. }
  352. })
  353. addEvent(window,'keyup',function(e){
  354. var e = e || window.event;
  355. if( e.ctrlKey && e.keyCode==114 ){//快捷键ctrl+F3
  356. if( $('pannal-troy') ) return;
  357. var pannal = document.createElement('div');
  358. var pannal_mars = document.createElement('div');
  359. pannal_mars.id = 'mars';
  360. pannal_mars.className = "blur";
  361. pannal.id = 'pannal-troy';
  362. pannal.innerHTML='\
  363. <h3>配置参数</h3>\
  364. <div id="pannal-setting">\
  365. 屏蔽方式:<select id="blockWay">\
  366. <option>遮罩屏蔽</option>\
  367. <option>删除节点</option>\
  368. </select><br/>\
  369. 遮罩层颜色:<input id="col" type="color" /><br/>\
  370. 遮罩透明度(0~1):<input id="opa" type="range" min="0" max="1" step="0.1" /><br/>\
  371. 高斯模糊像素(0~10):<input id="gus" type="range" min="0" max="10" step="1" /><br/>\
  372. </div>\
  373. <hr/>\
  374. <h3>添加讨厌鬼</h3>\
  375. <div id="addBlackList">\
  376. 数字ID(选填):<input id="userId" type="text" placeholder="user_id"/><br/>\
  377. 贴吧ID(必填):<input id="userName" type="text" placeholder="user_name" list="lst" autocomplete="off"/>\
  378. <datalist id="lst" autocomplete="on"></datalist>\
  379. </div>\
  380. <hr/>\
  381. <h3>功能</h3>\
  382. <div id="fn">\
  383. <input id="save" type="button" value="保存" />\
  384. <input id="clear" type="button" value="清空" />\
  385. <input id="view" type="button" value="查看" />\
  386. </div>\
  387. <hr/>\
  388. <h3>屏蔽列表</h3>\
  389. <div>\
  390. <ul id="showList"></ul>\
  391. </div>\
  392. <span id="queit">X</span>\
  393. '
  394. document.body.appendChild( pannal );
  395. document.body.appendChild( pannal_mars );
  396. pannal.style.top = ( getSize('clientHeight') - pannal.offsetHeight )/2 + 'px';
  397. pannal.style.left = ( getSize('clientWidth') - pannal.offsetWidth )/2 + 'px';
  398. //初始化
  399. //智能提示userName和userId
  400. addEvent($('userName'),'input',function(){
  401. autoTips();
  402. })
  403. $('blockWay').value = GM_getValue('setting-blockWay','遮罩屏蔽' );//屏蔽方式
  404. $('col').value = GM_getValue('setting-col','#fff'); //默认遮罩的颜色
  405. $('opa').value = GM_getValue('setting-opa','0.8'); //默认遮罩透明度
  406. $('gus').value = GM_getValue('setting-gus','3'); //默认遮罩下的高斯模糊半径
  407. createList(); //加载黑名单列表
  408. //删除节点模式则禁用选项
  409. addEvent($('blockWay'),'input',function(){
  410. var aInput=[$('col'),$('opa'),$('gus')]
  411. if( $('blockWay').value == '删除节点' ){//删除节点模式
  412. for(var i=0;i<aInput.length;i++){
  413. aInput[i].readOnly = 'true';
  414. aInput[i].className = 'disable-btn';
  415. }
  416. }else{//遮罩屏蔽模式模式
  417. for(var i=0;i<aInput.length;i++){
  418. aInput[i].readOnly = 'false';
  419. aInput[i].className = '';
  420. }
  421. }
  422. })
  423. //====保存按钮====
  424. addEvent($('save'),'click',function(){
  425. //判断屏蔽方式
  426. if($('blockWay').value=='遮罩屏蔽'){
  427. GM_setValue('setting-blockWay','遮罩屏蔽');
  428. }else if($('blockWay').value=='删除节点'){
  429. GM_setValue('setting-blockWay','删除节点');
  430. }
  431. //遮罩颜色
  432. GM_setValue('setting-col',$('col').value);
  433. //遮罩透明度
  434. if( $('opa').value!='' && $('opa').value>0 && $('opa').value<1 ){//透明度有添加,并且0~1
  435. GM_setValue('setting-opa',$('opa').value);
  436. }
  437. //高斯模糊半径
  438. if( $('gus').value!='' && $('gus').value>0){
  439. GM_setValue('setting-gus',$('gus').value);
  440. }
  441. //匹配ID是否正确
  442. var reg=/^[0-9]{9}$/ig;
  443. if( $('userName').value!='' && (reg.test($('userId').value)==true || $('userId').value=='') ){
  444. //保存形式:user_name:user_id
  445. GM_setValue( $('userName').value,$('userId').value );
  446. alert('保存成功' + "\n用户名:" + $('userName').value + "\nID:" + GM_getValue($('userName').value,'') + '\n屏蔽方式:'+ GM_getValue('setting-blockWay','遮罩屏蔽' )+'\n遮罩颜色:' + GM_getValue('setting-col','#fff') + '\n遮罩透明度:'+ GM_getValue('setting-opa','0.8') +'\n高斯模糊:'+GM_getValue('setting-gus','3') );
  447. }else if( $('userName').value=='' && $('userId').value==''){
  448. alert('保存成功\n屏蔽方式:'+ GM_getValue('setting-blockWay','遮罩屏蔽' )+'\n遮罩颜色:' + GM_getValue('setting-col','#fff') + '\n遮罩透明度:'+ GM_getValue('setting-opa','0.8') +'\n高斯模糊:'+GM_getValue('setting-gus','3') )
  449. }else{
  450. alert('输入不正确:\n数字ID:选填(如果填了必须是9位数字)\n贴吧ID:必填')
  451. }
  452. createList();//重新生成列表
  453. })
  454. //====清空按钮====
  455. addEvent($('clear'),'click',function(){
  456. for(var i=0;i<GM_listValues().length;i++){
  457. //清空所有变量
  458. //GM_deleteValue( GM_listValues()[i] ) + ":" + GM_getValue( GM_listValues()[i],'' )
  459. var value = GM_getValue( GM_listValues()[i],'');
  460. if( (typeof value=='number' && value>1000) || value==''){
  461. GM_deleteValue( GM_listValues()[i] );//删除键名
  462. }
  463. }//for
  464. createList();//重新生成列表
  465. })
  466. //====清空按钮====
  467. addEvent($('view'),'click',function(){
  468. createList();//生成列表
  469. })
  470. //===关闭按钮====
  471. addEvent($('queit'),'click',function(){
  472. $('pannal-troy').remove(this);
  473. $('mars').remove(this);
  474. })
  475. addEvent($('mars'),'click',function(){
  476. $('pannal-troy').remove(this);
  477. $('mars').remove(this);
  478. })
  479. }//if
  480. })//addEvent