- // ==UserScript==
- // @name remove the posts which make you sick
- // @author burningall
- // @description 贴吧移除讨厌鬼的帖子
- // @version 2015.7.5.2.0
- // @include *tieba.baidu.com/p/*
- // @include *tieba.baidu.com/*
- // @include *tieba.baidu.com/f?*
- // @grant GM_addStyle
- // @grant GM_getValue
- // @grant GM_setValue
- // @grant GM_listValues
- // @grant GM_deleteValue
- // @compatible chrome 推荐
- // @compatible firefox 性能稍微不足
- // @license The MIT License (MIT); http://opensource.org/licenses/MIT
- // @supportURL http://www.burningall.com
- // @contributionURL troy450409405@gmail.com|alipay.com
- // @namespace https://gf.qytechs.cn/zh-CN/users/3400-axetroy
- // ==/UserScript==
-
- //============快捷键==========
- //【Ctrl】+【F3】-----调出控制面板
- //============样式区==========
- var style='\
- body{\
- -webkit-backface-visibility: hidden;\
- }\
- /*给body添加滤镜*/\
- .blur{\
- -webkit-filter: blur(5px) grayscale();\
- -moz-filter: blur(5px) grayscale();\
- -o-filter: blur(5px) grayscale();\
- -ms-filter: blur(5px) grayscale();\
- filter: blur(5px) grayscale();\
- }\
- #pannal-troy{\
- width:200px;\
- height:auto;\
- background:#303030;\
- color:#fff;\
- position:fixed;\
- z-index:1000000000;\
- text-align:center;\
- }\
- #pannal-troy>div{\
- margin:10px 0;\
- }\
- #pannal-troy input{\
- color:#3e3e3e;\
- }\
- #pannal-troy h3{\
- color:rgb(0, 255, 226);\
- }\
- #pannal-setting input[type=range]{\
- width:80%;\
- }\
- #fn input{\
- padding:5px;\
- margin:0 5px;\
- border:none;\
- cursor:pointer;\
- }\
- #fn input:hover{\
- background:#2A959D;\
- color:#fff;\
- }\
- #pannal-troy>span{\
- position:absolute;\
- padding:0 10px;\
- top:0;\
- right:0;\
- cursor:pointer;\
- opacity:0.8;\
- background:#fff;\
- color:#303030;\
- }\
- #blockWay{\
- color:#3e3e3e;\
- border:none;\
- }\
- /*控制面板遮罩层*/\
- #mars{\
- position:fixed;\
- width:100%;\
- height:100%;\
- background:rgba(155, 155, 155,0.5);\
- top:0;\
- left:0;\
- z-index:999999999;\
- }\
- /*屏蔽模板遮罩层*/\
- .mar{\
- position:absolute;\
- top:0;\
- left:0;\
- z-index:999999998;\
- -webkit-backface-visibility:hidden;\
- }\
- #showList{\
- position:relative;\
- overflow-y:auto;\
- overflow-x:hidden;\
- width:100%;\
- height:93%;\
- }\
- #list{\
- position:absolute;\
- width:0;\
- height:20px;\
- top:-10px;\
- left:200px;\
- background:#303030;\
- overflow:hidden;\
- }\
- #showList>li{\
- width:100%;\
- height:18px;\
- clear:both;\
- margin:10px 0;\
- cursor:pointer;\
- }\
- #showList>li:hover{\
- background-color:#54A19E;\
- color:#303030;\
- }\
- .key{\
- float:left;\
- clear:both;\
- margin-left:10px;\
- width:120px;\
- text-align:left;\
- overflow:hidden;\
- text-overflow:ellipsis;\
- white-space:nowrap;\
- }\
- .col{\
- border:none;\
- }\
- .deletThis{\
- float:right;\
- cursor:pointer;\
- margin-right:10px;\
- padding:0 5px;\
- border:0;\
- height:18px;\
- }\
- .disable-btn{\
- background:#6B6B6B;\
- cursor:not-allowed;\
- }\
- #addBlackList input{\
- width:80%;\
- }\
- #thread_list>li[data-field] .threadlist_lz>.threadlist_author,.l_post .d_name{\
- background:#303030;\
- }\
- #sear{\
- position:relative;\
- margin:0 auto;\
- text-align:center;\
- height:21px;\
- width:100%;\
- border:none;\
- }\
- ';
- GM_addStyle(style);
- //============公共函数区==========
- function hasClass(obj,cName) {
- // ( \\s|^ ) 判断前面是否有空格 (\\s | $ )判断后面是否有空格 两个感叹号为转换为布尔值 以方便做判断
- return !! obj.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
- };
- function addClass(obj, cName) {
- if (!hasClass(obj, cName)) {
- obj.className += " " + cName;
- };
- };
- function removeClass(obj, cName) {
- if (hasClass(obj, cName)) {
- obj.className = obj.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), " "); // replace方法是替换
- };
- };
- function addEvent(obj, event, fn) {
- return obj.addEventListener ? obj.addEventListener(event, fn, false) : obj.attachEventListener('on' + event, fn);
- };
- function getStyle(obj, attr) {
- return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
- };
- function $(id) {
- return document.getElementById(id)
- };
- function getSize(attr){
- return document.documentElement[attr] ? document.documentElement[attr] : document.body[attr]
- }
- function doMove(obj, attr, dir, target, endFn) {
- dir = parseInt(getStyle(obj, attr)) < target ? dir: -dir; //对于方向矫正
- clearInterval(obj.timerMove) //清楚定时器
- obj.timerMove = setInterval(function() {
- var speed = parseInt(getStyle(obj, attr)) + dir //步长
- if (speed > target && dir > 0 || speed < target && dir < 0) { //判断往前或往后
- speed = target;
- };
- obj.style[attr] = speed + "px" //赋值
- if (speed == target) {
- clearInterval(obj.timerMove);
- endFn && endFn(); //回掉函数
- };
- },
- 30)
- };
- function uniqueArray(data){
- data = data || [];
- var a = {};
- for (var i=0; i<data.length; i++) {
- var v = data[i];
- if (typeof(a[v]) == 'undefined'){
- a[v] = 1;
- }
- };
- data.length=0;
- for (var i in a){
- data[data.length] = i;
- }
- return data;
- }
- //============主要代码区==========
- function juggUrl(){
- var url=location.href;
- var postIn=/.*tieba.baidu.com\/p\/.*/ig;
- var postList=/.*tieba.baidu.com\/(f\?.*|[^p])/ig;
- if( postIn.test(url) ){//如果是帖子内
- return 1;
- }else if( postList.test(url) ){//如果在帖子列表
- return 2;
- }
- }
- function createList(){
- $('showList').innerHTML='';
- var li='';
- for(var i=0;i<GM_listValues().length;i++){
- //控制台输出 键名:键值
- var key = GM_listValues()[i];
- var value = GM_getValue( GM_listValues()[i],'');
- if( (typeof value =='number' && value>1000) || value==''){
- //console.log( GM_listValues()[i] + ":" + GM_getValue(GM_listValues()[i],'') );
- li += '<li class="show_list" data="'+ key +'">'+'<span class="key">'+key+'</span>'+'<input class="deletThis" type="button" value="删除"/>'+'</li>';
- }//if
- }//for
- $('showList').innerHTML=li;
- var aDeleBtn=document.querySelectorAll('.deletThis');
- for(var i=0;i<aDeleBtn.length;i++){
- aDeleBtn[i].onclick=function(){
- var thisKey = this.parentNode.getAttribute('data');//获取当前结点的key
- GM_deleteValue( thisKey );//删除变量
- this.parentNode.remove( this.parentNode );//删除节点
- }//clickb
- }//for
- };
- function marks(obj){
- if(obj.querySelectorAll('.mar').length>=1){
- return;
- }
- var oMar = document.createElement('div');
- oMar.className = 'mar';
- //要屏蔽的模块
- obj.style.position = 'relative';
- obj.style.webkitFilter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//chrome
- obj.style.filter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//firefox
- obj.style.oFilter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//opera
- obj.style.msFilter = 'blur('+ GM_getValue('setting-gus','3') +'px)';//IE
- obj.style.webkitBackfaceVisibility = 'hidden';
- obj.appendChild( oMar );
- //遮罩层
- oMar.style.width = parseInt( getStyle(obj,'width') ) + 'px';
- oMar.style.height = parseInt( getStyle(obj,'height') ) + 'px';
- oMar.style.background = GM_getValue('setting-col','#ffffff');
- oMar.style.opacity = GM_getValue('setting-opa','0.8');
- oMar.style.filter = 'alpha(opacity=' + GM_getValue('setting-opa','0.8')*100 +')';
- oMar.style.webkitBackfaceVisibility = 'hidden';
- };
- function blockMod(){
- if( GM_getValue('setting-blockWay','遮罩屏蔽')=='遮罩屏蔽' ){
- return 1;//遮罩模式
- }else{
- return 2;//删除节点
- }
- }
-
- function btclick(This,list,n){
- var _thisTop,_thisUserId,_thisUserName,nowUserId,nowUserName;
- if(n=='postList'){
- _thisTop = This.parentNode.parentNode.parentNode.parentNode;
- _thisUserId = JSON.parse(_thisTop.getAttribute('data-field')).id;//当前id
- _thisUserName = JSON.parse(_thisTop.getAttribute('data-field')).author_name;//当前userName
- }else if(n=='post'){
- _thisTop = This.parentNode.parentNode.parentNode;
- _thisUserId = JSON.parse(_thisTop.getAttribute('data-field')).author.user_id;//当前id
- _thisUserName = JSON.parse(_thisTop.getAttribute('data-field')).author.user_name;//当前userName
- }
- if ( confirm('是否屏蔽') == true) { //如果按下确认
- GM_setValue( _thisUserName,_thisUserId );
- for (var j = 0; j < list.length; j++) {//循环匹配是否符合
- var userInfo = JSON.parse(list[j].getAttribute('data-field'))
- if(n=='postList'){
- nowUserId = userInfo.id;
- nowUserName = userInfo.author_name;
- }else if(n=='post'){
- nowUserId = userInfo.author.user_id;
- nowUserName = userInfo.author.user_name;
- }
- if (nowUserId == _thisUserId || nowUserName==_thisUserName) {//匹配成功
- if( blockMod()==1 ){//遮罩模式
- marks(list[j]);
- }else{//删除节点模式
- list[j].style.display = 'none';
- }
- }//匹配是否符
- } //for
- }//if如果按下确认
- }
- function block(name,id,list,I){
- for(var j=0;j<GM_listValues().length;j++){
- if( GM_listValues()[j] == name || GM_getValue(GM_listValues()[j],'') == id ){//如果匹配到
- if( blockMod()==1 ){//遮罩模式
- marks(list[I]);
- }else{//删除节点模式
- list[I].style.display = 'none';
- }
- }//匹配是否符合
- }//for,遍历屏蔽
- }
-
- function autoTips(){
- var aUsers,userName,userId;
- if( juggUrl()==1 ){//帖子内
- var aTipsArr = new Array();
- var newString = '';
- aUsers = document.querySelectorAll('#j_p_postlist>div[data-field]');//获取列表
- for(var i=0;i<aUsers.length;i++){
- userName = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_name;//第i个userName
- userId = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_id;//第i个userId
- aTipsArr.push( userName );
- }
- uniqueArray( aTipsArr );
- for(var i=0;i<aTipsArr.length;i++){
- newString += '<option value="' + aTipsArr[i] + '" />';
- }
- lst.innerHTML = newString;
- }else{//帖子列表
- var aTipsArr = new Array();
- var newString = '';
- aUsers=document.querySelectorAll('#thread_list>li[data-field]');//获取列表
- for(var i=0;i<aUsers.length;i++){
- userName = JSON.parse(aUsers[i].getAttribute('data-field')).author_name;//第i个userName
- userId = JSON.parse(aUsers[i].getAttribute('data-field')).id;//第i个userId
- aTipsArr.push( userName );
- }
- uniqueArray( aTipsArr );
- for(var i=0;i<aTipsArr.length;i++){
- newString += '<option value="' + aTipsArr[i] + '" />';
- }
- lst.innerHTML = newString;
- }//if....else
- }
- function closePannal(){
- doMove($('list'),'height',40,20,function(){
- doMove($('list'),'width',25,0,function(){//收起
- $('view').value = '>>';
- turn = false;
- $('pannal-troy').style.overflow = 'hidden';
- doMove($('pannal-troy'),'height',40,15,function(){
- doMove($('pannal-troy'),'width',25,0,function(){
- $('pannal-troy').remove(this);
- $('mars').remove(this);
- document.body.className = '';
- })
- })
- });
- })
- }
- function init() {//初始化程序
- //=======帖子内==========
- if( juggUrl()==1 ){
- var aShut = document.querySelector('.l_post .d_name');
- if( aShut.onclick ){
- return false;
- }
- var aUsers = document.querySelectorAll('#j_p_postlist>div[data-field]');//获取列表
- var aName = document.querySelectorAll('.l_post .d_name');//屏蔽按钮将要插入的位置
- for (var i = 0; i < aUsers.length; i++) {
- //获取id和name
- var userName = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_name;//第i个userName
- var userId = JSON.parse(aUsers[i].getAttribute('data-field')).author.user_id;//第i个userId
- //添加点击事件
- aName[i].onclick=function(){
- var this_=this;
- btclick(this_,aUsers,'post','author[user_id]','author[user_name]');
- }
- //页面加载开始屏蔽
- block(userName,userId,aUsers,i);
- } //for,遍历所有节点
- //=======帖子列表==========
- }else if( juggUrl()==2 ){
- var aShut = document.querySelector('#thread_list>li[data-field] .threadlist_lz>.threadlist_author, .l_post .d_name');
- if( aShut.onclick ){
- return false;
- }
- var aUsers=document.querySelectorAll('#thread_list>li[data-field]');//获取列表
- var aName = document.querySelectorAll('#thread_list>li[data-field] .threadlist_lz>.threadlist_author');//屏蔽按钮将要插入的位置
- for(var i=0;i<aUsers.length;i++){
- //获取id和name
- var userName = JSON.parse(aUsers[i].getAttribute('data-field')).author_name;//第i个userName
- var userId = JSON.parse(aUsers[i].getAttribute('data-field')).id;//第i个userId
- //添加点击事件
- aName[i].onclick=function(){
- var this_=this;
- btclick(this_,aUsers,'postList','id','author_name');
- }
- //页面加载开始屏蔽
- block(userName,userId,aUsers,i)
- }//for,遍历所有节点
- }//在帖子列表
- }//初始化
- //============执行区==========
- addEvent(window,'load',function(){
- init();
- })
- //跟随翻页加载
- var aUsers,aShut;
- addEvent(window,'scroll',function(){
- if( getSize('scrollTop') == 0){
- clearTimeout(document.TPage);
- document.TPage = setTimeout(init,3000);
- }//如果滚动到顶部
- })
- //============控制面板以及相关==========
- addEvent(window,'resize',function(){
- if( $('pannal-troy') ){
- $('pannal-troy').style.top = ( getSize('clientHeight') - $('pannal-troy').offsetHeight )/2 + 'px';
- $('pannal-troy').style.left = ( getSize('clientWidth') - $('pannal-troy').offsetWidth )/2 + 'px';
- }
- })
- addEvent(window,'keyup',function(e){
- var e = e || window.event;
- if( e.ctrlKey && e.keyCode==114 ){//快捷键ctrl+F3
- if( $('pannal-troy') ) return;
- var pannal = document.createElement('div');
- var pannal_mars = document.createElement('div');
- pannal_mars.id = 'mars';
- pannal.id = 'pannal-troy';
- pannal.innerHTML='\
- <h3>配置参数</h3>\
- <div id="pannal-setting">\
- 屏蔽方式:<select id="blockWay">\
- <option>遮罩屏蔽</option>\
- <option>删除节点</option>\
- </select><br/>\
- 遮罩层颜色:<input id="col" type="color" /><br/>\
- 遮罩透明度(0~1):<span id="opa-text"></span><input id="opa" type="range" min="0" max="1" step="0.1" /><br/>\
- 高斯模糊像素(0~10):<span id="gus-text"></span><input id="gus" type="range" min="0" max="10" step="1" /><br/>\
- </div>\
- <hr/>\
- <h3>添加讨厌鬼</h3>\
- <div id="addBlackList">\
- 数字ID(选填):<input id="userId" type="text" placeholder="user_id"/><br/>\
- 贴吧ID(必填):<input id="userName" type="text" placeholder="user_name" list="lst" autocomplete="off"/>\
- <datalist id="lst" autocomplete="on"></datalist>\
- </div>\
- <hr/>\
- <h3>功能</h3>\
- <div id="fn">\
- <input id="save" type="button" value="保存" />\
- <input id="clear" type="button" value="清空" />\
- <input id="view" type="button" value=">>" />\
- </div>\
- <div id="list">\
- <input id="sear" type="text" list="BlackList" placeholder="搜索" autocomplete="off" />\
- <ul id="showList"></ul>\
- </div>\
- <span id="queit">X</span>\
- '
- document.documentElement.appendChild( pannal );
- document.documentElement.appendChild( pannal_mars );
- document.body.className = 'blur';
- pannal.style.top = ( getSize('clientHeight') - pannal.offsetHeight )/2 + 'px';
- pannal.style.left = ( getSize('clientWidth') - pannal.offsetWidth )/2 + 'px';
- //初始化
- $('blockWay').value = GM_getValue('setting-blockWay','遮罩屏蔽' );//屏蔽方式
- $('col').value = GM_getValue('setting-col','#ffffff'); //默认遮罩的颜色
- $('opa').value = GM_getValue('setting-opa','0.8'); //默认遮罩透明度
- $('opa-text').innerHTML = $('opa').value;
- $('gus').value = GM_getValue('setting-gus','3'); //默认遮罩下的高斯模糊半径
- $('gus-text').innerHTML = $('gus').value;
- createList(); //加载黑名单列表
- addEvent($('opa'),'input',function(){
- $('opa-text').innerHTML = $('opa').value;
- })
- addEvent($('gus'),'input',function(){
- $('gus-text').innerHTML = $('gus').value;
- })
- //智能提示userName和userId
- addEvent($('userName'),'input',function(){
- autoTips();
- })
- //删除节点模式则禁用选项
- addEvent($('blockWay'),'input',function(){
- var aInput=[$('col'),$('opa'),$('gus')]
- if( $('blockWay').value == '删除节点' ){//删除节点模式
- for(var i=0;i<aInput.length;i++){
- aInput[i].readOnly = 'true';
- aInput[i].className = 'disable-btn';
- }
- }else{//遮罩屏蔽模式模式
- for(var i=0;i<aInput.length;i++){
- aInput[i].readOnly = 'false';
- aInput[i].className = '';
- }
- }
- })
- //====保存按钮====
- addEvent($('save'),'click',function(){
- //判断屏蔽方式
- if($('blockWay').value=='遮罩屏蔽'){
- GM_setValue('setting-blockWay','遮罩屏蔽');
- }else if($('blockWay').value=='删除节点'){
- GM_setValue('setting-blockWay','删除节点');
- }
- //遮罩颜色
- GM_setValue('setting-col',$('col').value);
- //遮罩透明度1
- GM_setValue('setting-opa',$('opa').value);
- //高斯模糊半径
- GM_setValue('setting-gus',$('gus').value);
- //匹配ID是否正确
- var regId=/^[1-9][0-9]{4,}$/ig;
- if( $('userName').value!='' && (regId.test($('userId').value)==true || $('userId').value=='') ){
- //保存形式:user_name:user_id
- GM_setValue( $('userName').value,$('userId').value );
- 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') );
- }else if( $('userName').value=='' && $('userId').value==''){
- 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') )
- }else{
- alert('输入不正确:\n数字ID:选填([5~n位数])\n贴吧ID:必填')
- }
- if( parseInt( getStyle($('list'),'width') )>0 ){//如果列表已经展开
- createList()//重新生成列表
- }
- })
- //====清空按钮====
- addEvent($('clear'),'click',function(){
- for(var i=0;i<GM_listValues().length;i++){
- //清空所有变量
- //GM_deleteValue( GM_listValues()[i] ) + ":" + GM_getValue( GM_listValues()[i],'' )
- var value = GM_getValue( GM_listValues()[i],'');
- if( (typeof value=='number' && value>1000) || value==''){
- GM_deleteValue( GM_listValues()[i] );//删除键名
- }
- }//for
- if( parseInt(getStyle($('list'),'width'))>0 ){//如果列表已经展开
- createList();//重新生成列表
- }
- })
- //====展开/收起按钮====
- var turn = false;
- addEvent($('view'),'click',function(){
- if(turn == false){
- createList();
- //$('showList').style.overflowY = 'scroll';
- doMove($('list'),'width',20,200,function(){//展开
- doMove($('list'),'height',40,321,function(){
- $('sear').focus();
- $('view').value = '<<';
- turn = true;
- });
- })
- }else if( turn == true ){
- //$('showList').style.overflowY = 'hidden';
- doMove($('list'),'height',40,20,function(){
- doMove($('list'),'width',20,0,function(){//收起
- $('view').value = '>>';
- turn = false;
- });
- })
- }
- })
- //黑名单搜索
- addEvent($('sear'),'input',function(){
- createList();
- var list = document.querySelectorAll('#showList .show_list');
- $('showList').innerHTML = '';//清空列表
- var str = '';
- for(var i=0;i<list.length;i++){
- var userName = list[i].getAttribute('data');
- if( userName.indexOf( $('sear').value ) >=0 ){//如果搜索到
- str += '<li class="show_list" data="'+ userName +'">'+'<span class="key">'+userName+'</span>'+'<input class="deletThis" type="button" value="删除"/>'+'</li>';
- }
- }//for
- $('showList').innerHTML = str;//生成列表
- //添加点击事件
- var aDeleBtn=document.querySelectorAll('.deletThis');
- for(var i=0;i<aDeleBtn.length;i++){
- aDeleBtn[i].onclick=function(){
- var thisKey = this.parentNode.getAttribute('data');//获取当前结点的key
- GM_deleteValue( thisKey );//删除变量
- this.parentNode.remove( this.parentNode );//删除节点
- }//click
- }//for
- })
- //黑名单搜索,backspace键
- addEvent(window,'keyup',function(e){
- var e= e || window.event;
- if(e.keyCode==8){//backspace
- if( $('sear').value == '' ){
- createList();
- }
- }
- })
- //===关闭按钮====
- addEvent($('queit'),'click',function(){
- closePannal()
- })
- addEvent($('mars'),'click',function(){
- closePannal()
- })
- }//if
- })//addEvent