// ==UserScript==
// @name 自动删除贴吧传销广告
// @description 打开担任大吧或者小吧的贴吧的看帖页面后自动扫描帖子,删除贴吧广告帖子和楼层
// @include http://tieba.baidu.com/f?*
// @include http://tieba.baidu.com/p/*
// @version 0.1
// @author yechenyin
// @namespace https://gf.qytechs.cn/users/3586-yechenyin
// @require https://code.jquery.com/jquery-1.11.2.min.js
// @grant GM_setClipboard
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
settings = {
'scan_threads': 20,
'scan_interval_seconds':60,
'scan_floors':20,
'below_level':4,
'block_rules':'@高清网盘看头像',
'block_users': '我装疯岁',
'block_signs': 'http://imgsrc.baidu.com/forum/w=580/sign=04d3cea28ab1cb133e693c1bed5556da/e3a430d12f2eb938ab2bdfbfd2628535e4dd6ff8.jpg?v=tbs'+'\n'+
'http://imgsrc.baidu.com/forum/w=580/sign=c657712f044f78f0800b9afb49300a83/2c7a4b36acaf2edd9e76a6928a1001e9380193bd.jpg?v=tbs'+'\n'+
'http://imgsrc.baidu.com/forum/pic/item/889866c379310a550e1704aeb04543a9832610ec.jpg?v=tbs'
};
for (var prop in settings) {
if (!get(prop))
set(prop, settings[prop]);
}
jQuery.fn.loaded = function(action) {
var target = this.selector;
var check = setInterval(function () {
if ($(target).length > 0) {
console.log($(target).length + ' ' + target + " is loaded");
clearInterval(check);
action();
}
}, 200);
};
$.fn.update = function () {
$(this).load(location.href + ' ' + this.selector + '>*');
};
// String.prototype.match = function (regexp) {
// if (typeof regexp == 'string')
// regexp = [regexp];
// };
String.prototype.check = function (regexp) {
if (typeof regexp == 'string')
regexp = [regexp];
if (regexp && regexp instanceof Array && regexp.length>0){
var re;
for (var i = 0; i < regexp.length; i++) {
if (/^\/.*\/$/.test(regexp)) {
re = regexp[i].substring(1, regexp[i].length-1);
} else {
re = regexp[i].replace(/[\.|\\|\[|\{|\(|\)|\^|\$|\||\?|\+]/g, '\\$&').replace(/\*/g, '.*');
}
//console.log(new RegExp(re));
if ((new RegExp(re)).test(this))
return true;
}
return false;
} else if (regexp instanceof RegExp) {
return regexp.test(this);
}
};
function count() {
var i = 0;
(function () {
console.log(i);
i++;
})();
}
function loadData(href, selector, attr, callback) {
if (arguments.length == 2 && typeof arguments[1] === 'function') {
arguments[1](response);
return;
}
if (arguments.length == 3) {
attr = '';
callback = arguments[2];
}
$.get(href, function (response) {
var data = [];
if (typeof selector == 'string') {
for (var k = 0; k < $(response).find(selector).length; k++) {
var element = $(response).find(selector).eq(k);
if (attr === 'text')
data.push(element.text());
else if (attr && element.attr(attr))
data.push(element.attr(attr));
else
data.push(element[0].outerHtml);
}
} else if (selector instanceof RegExp) {
if (selector.exec(response)[1])
data.push(selector.exec(response)[1]);
else
data.push(selector.exec(response)[0]);
}
if (selector instanceof Array){
for (var i = 0; i < selector.length; i++) {
if (selector[i] instanceof RegExp) {
var matched;
if (elector[i].exec(response)[1])
matched = selector[i].exec(response)[1];
else
matched = selector[i].exec(response)[0];
if (data instanceof Array && attr instanceof Array)
data = {};
if (data instanceof Object)
data.attr = matched;
else
data.push(matched);
} else
for (var j = 0; j < $(response).find(selector).length; j++) {
var element = $(response).find(selector[i]).eq(j);
if (attr === 'text')
data.push(element.text());
else if (attr && element.attr(attr))
data.push(element.attr(attr));
else
data.push(element[0].outerHtml);
}
}
}
if (data.length === 1)
callback(data[0]);
else
callback(data);
});
}
if (localStorage.running_state === undefined || !localStorage.running_state)
localStorage.running_state = 'running';
$(window).on('beforeunload', function () {
localStorage.running_state = '';
});
function switch_running() {
if (typeof stop_scan !== 'undefined' && stop_scan)
stop_scan = false;
else
stop_scan = true;
}
if (!location.href.indexOf("http://tieba.baidu.com/f?") && localStorage.running_state) {
$('.u_setting ul').loaded(function () {
$('.u_setting ul').prepend($('<li>').append($('<a>', {text:'删除设置', css:{cursor:'pointer'}, click:function() {
var setting = $('<div>', {css:{padding:'5px 16px', background:'#fff'}});
setting.append($('<div>', {text:'×', css:{width:'20px', height:'20px', color:'#999', 'font-size':'20px', float:'right', cursor:'pointer', margin:'-4px -15px 0 0'}, click:function() {
$('.delete_setting').remove();
}}));
setting.append($('<div>', {text:'扫描设置', css:{color:'#bbb', 'margin':'4px 0 0px'}}));
setting.append($('<span>', {text:'每次扫描'}));
setting.append($('<input>', {name:'scan_threads', type:'text', css:{width:'30px', height:'12px', margin:'0 3px'}}));
setting.append($('<span>', {text:'个帖子'}));
setting.append($('<div>', {css:{'margin':'2px'}}));
setting.append($('<span>', {text:'每次扫描间隔'}));
setting.append($('<input>', {name:'scan_interval_seconds', type:'text', css:{width:'30px', height:'12px', margin:'0 3px'}}));
setting.append($('<span>', {text:'秒'}));
setting.append($('<div>', {css:{'margin':'2px'}}));
setting.append($('<span>', {text:'每个帖子扫描'}));
setting.append($('<input>', {name:'scan_floors', type:'text', css:{width:'30px', height:'12px', margin:'0 3px'}}));
setting.append($('<span>', {text:'楼'}));
setting.append($('<div>', {text:'广告自动删除设置', css:{color:'#bbb', 'margin':'12px 0 0px'}}));
setting.append($('<span>', {text:'仅扫描小于等于等级'}));
setting.append($('<input>', {name:'below_level', type:'text', css:{width:'20px', height:'12px', margin:'0 3px'}}));
setting.append($('<span>', {text:'以下的用户'}));
setting.append($('<div>', {css:{'margin':'4px'}}));
setting.append($('<span>', {text:'广告包含的关键字'}));
setting.append($('<span>', {text:'(每个关键字使用空格隔开,中间可以用*表示任意长度文字。另外可以使用以/开头和结尾的正则表达式)', css:{color:'#999'}}));
setting.append($('<textarea>', {name:'block_rules', css:{height:'40', width:'360px', color:'#333', padding:'0px 4px', display:'block', margin:'1px auto 10px', 'border':'1px solid #bebebe'}}));
setting.append($('<span>', {text:'广告包含的签名图片地址'}));
setting.append($('<span>', {text:'(每个地址使用空格或者换行隔开)', css:{color:'#999'}}));
setting.append($('<textarea>', {name:'block_signs', css:{height:'40', width:'360px', color:'#333', padding:'0px 4px', display:'block', margin:'1px auto 10px', 'border':'1px solid #bebebe'}}));
setting.find('input, textarea').each(function() {
$(this).val(get($(this).attr('name')));
});
setting.find('input, textarea').change(function() {
set($(this).attr('name'), $(this).val());
});
//setting.append($('<button>', {text:'导入设置', css:{margin:'14px 0 8px 0px', 'line-height':'20px', padding:'0px 6px', background:'#fff', border:'1px solid #aaa', color:'#777', 'border-radius':2, 'font-size':'12px', }}));
//setting.append($('<button>', {text:'导出设置', css:{margin:'14px 0 8px 20px', 'line-height':'20px', padding:'0px 6px', background:'#fff', border:'1px solid #aaa', color:'#777', 'border-radius':2, 'font-size':'12px', }}));
$('.u_ddl').hide();
$('<div>', {class:'delete_setting', css:{width:'400px', top:'30px ', left:'50%', 'margin-left':'-300px', background:'rgba(51, 51, 51, 0.3)', padding:'4px', position:'fixed', 'z-index':10001, 'font-size':'12px', 'vertical-align':'center', 'box-shadow':'0 0 5px #C6C6C6', 'border':'#aaa solid 1px', 'border-radius':3}}).append(setting).appendTo($('body'));
}})));
$('.u_setting ul').prepend($('<li>').append($('<a>', {text:'停止删除', css:{cursor:'pointer'}, click:function() {
if ($(this).text() == '停止删除') {
stop_scan = true;
$(this).text('自动删除');
} else {
stop_scan = false;
$(this).text('停止删除');
if ($('.tbForumRadioManage').length > 0)
check_and_delete();
}
}})));
});
var i = 0;
if ($('.tbForumRadioManage').length > 0)
check_and_delete();
function check_and_delete(hrefs) {
var href;
if (hrefs)
href = 'http://tieba.baidu.com/' + hrefs[i];
else
href = $("a.j_th_tit")[i].href;
//console.log(href);
var tid = href.match(/\d+/)[0];
if (typeof stop_scan !== 'undefined' && stop_scan)
return;
whetherRecorded(tid, function (scaned) {
if (!scaned) {
$.get(href, function (data) {
html = data;
console.log(href + ' ' + $(data).find('.l_post').length);
//console.log(/<title>(.*?)<\//.exec(data)[1]);
if (/"tbs"\s*:\s*"(\w+)"/.exec(data)) {
var tbs = /"tbs"\s*:\s*"(\w+)"/.exec(data)[1];
//var tid = /thread_id\s*:\s*(\d+)/.exec(data)[1];
var kw = /forum\.name_url\s*=\s*"(\S+?)"/.exec(data)[1];
var fid = /\{"forum_id"\s*:\s*(\d+)/.exec(data)[1];
$(data).find('.l_post').each(function(j) {
var matched = false;
if ($(this).find('.j_user_sign').length > 0 && get('block_sign').indexOf($(this).find('.j_user_sign').attr('src')) >= 0)
matched = true;
else if ($(this).find('.d_post_content').text().check(get('block_rules').split(' ')))
matched = true;
if ($(this).find('.j_user_sign').length > 0 && get('block_sign').indexOf($(this).find('.j_user_sign').attr('src')) >= 0)
console.log($(this).find('.j_user_sign').attr('src'));
if($(this).find('.d_badge_lv').text() <= 4 && matched) {
var data = {commit_fr:'pb',
ie:'utf-8',
tbs:tbs,
kw:kw,
fid:fid,
tid:tid,
is_vipdel:0,
pid:$(this).find('.d_post_content').attr('id').replace('post_content_', ''),
is_finf:false
};
console.log(data);
console.log(j + ' ' + $(this).find('.d_post_content').text());
$.post('http://tieba.baidu.com/f/commit/post/delete', data, function(response) {
console.log(response);
});
if (get('also_blockid')) {
data = {day:10,
fid:fid,
tbs:tbs,
ie:'utf-8',
'user_name[]':0,
'pid[]':$(this).find('.d_post_content').attr('id').replace('post_content_', ''),
'reason':'大量散布广告'
};
console.log(data);
$.post('http://tieba.baidu.com/pmc/blockid', data, function(response) {
console.log(response);
});
}
}
});
if ($(data).find('.l_post').length >= 20)
recorded(tid);
}
});
}
i++;
//console.log(i);
if (i== get('scan_threads') || i == $("a.j_th_tit").length) {
setTimeout(function () {
if (typeof load_href === 'undefined') {
if (!getUrlParameter('pn'))
load_href = location.href + '&pn=50';
else
load_href = location.href.replace(/pn=\d+/, 'pn='+(parseInt(getUrlParameter('pn'))+50));
}
else
load_href = load_href.replace(/pn=\d+/, 'pn='+(parseInt(/pn=(\d+)/.exec(load_href)[1])+50));
if (parseInt(/pn=(\d+)/.exec(load_href)[1]) >= parseInt(get('scan_threads')))
load_href = load_href.replace(/pn=\d+/, 'pn=0');
console.log(load_href);
loadData(load_href, 'a.j_th_tit', 'href', function (posts) {
i = 0;
check_and_delete(posts);
});
}, get('scan_interval_seconds') * 1000);
} else {
setTimeout(function() {
check_and_delete();
}, 50);
}
});
}
}
if (!location.href.indexOf("http://tieba.baidu.com/p") && localStorage.running_state) {
// if (typeof unsafeWindow === 'undefined')
// unsafeWindow = window;
// $(".l_post").each(function() {
// if (parseInt($(this).find('.d_badge_lv')) <= 4) {
// var text = $(this).find('.d_post_content').text();
// var data = {commit_fr:'pb',
// ie:'utf-8',
// tbs:unsafeWindow.PageData.tbs,
// kw:unsafeWindow.PageData.forum.name_url.replace(/&\S+/, ''),
// fid:unsafeWindow.PageData.sign_forum_info.forum_info.forum_id,
// tid:unsafeWindow.PageData.thread.thread_id.toString(),
// is_vipdel:0,
// pid:$(this).find('.d_post_content').attr('id').replace('post_content_', ''),
// is_finf:false
// };
// console.log(data);
// if ($(this).find('.j_user_sign').length > 0 && get('block_sign').indexOf($(this).find('.j_user_sign').attr('src')) >= 0)
// $.post('http://tieba.baidu.com/f/commit/post/delete', {data: data}, function(data, textStatus, xhr) {
// console.log(data);
// });
// }
// });
// openDB().onsuccess = function(event){
// var db = event.target.result;
// var transaction = db.transaction(["read"],"readwrite");
// var store = transaction.objectStore("read");
//
//
// for (var i=0; i<$("a.j_th_tit").length; i++) {
// (function(i) {
// var request = store.get($("a.j_th_tit")[i].href.match(/\d+/)[0]);
// request.onsuccess = function(event){
// if (event.target.result) {
// $("a.j_th_tit")[i].href = $("a.j_th_tit")[i].href + event.target.result.last;
// console.log(i+$("a.j_th_tit")[i].href);
// }
// };
// })(i);
// }
// };
}
function onStoreOpened(func, funcByLocalStorage, use_local_storage) {
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
if(!indexedDB || use_local_storage) {
if(!indexedDB)
console.log("你的浏览器不支持IndexedDB");
if (funcByLocalStorage)
funcByLocalStorage();
} else {
var request = indexedDB.open("tieba_recorded", 1);
var db;
request.onerror = function(event){
console.log("打开数据库失败", event);
};
request.onupgradeneeded = function(event){
console.log("Upgrading database");
db = event.target.result;
var objectStore = db.createObjectStore("recorded", { keyPath : "tid" });
};
request.onsuccess = function(event){
var db = event.target.result;
var transaction = db.transaction(["recorded"],"readwrite");
var store = transaction.objectStore("recorded");
func(store);
};
}
}
function recorded(tid) {
onStoreOpened(function(store){
store.add({tid: tid});
console.log(tid + " is recorded");
},
function () {
if (typeof localStorage.recorded === 'undefined')
localStorage.recorded = '';
localStorage.recorded = tid + ' ' + localStorage.recorded;
});
}
function whetherRecorded(tid, callback) {
var result;
onStoreOpened(function(store){
var request = store.get(tid);
request.onsuccess = function(event){
if (request.result) {
//console.log(tid + " is recorded already");
result = true;
} else {
//console.log(tid + " is not recorded");
result = false;
}
if (callback)
callback(result);
};
}, function () {
console.log("你的浏览器不支持IndexedDB");
if (localStorage.recorded && localStorage.recorded.indexOf(tid) >= 0) {
result = true;
} else {
result = false;
}
if (callback)
callback(result);
});
}
function get(name) {
if (typeof GM_getValue !== 'undefined') {
//console.log(name + ': ' + GM_getValue(name));
return GM_getValue(name);
} else {
var value;
name = 'gm_'+name;
//console.log(name + ': ' + localStorage[name]);
if (typeof localStorage[name] !== 'undefined' && localStorage[name])
value = JSON.parse(localStorage[name]);
return value;
}
}
function set(name, value) {
if (typeof GM_setValue !== 'undefined') {
GM_setValue(name, value);
console.log(name + ' = ' + GM_getValue(name));
} else {
name = 'gm_'+name;
localStorage[name] = JSON.stringify(value);
console.log(name + ' = ' + localStorage[name]);
}
}
function getUrlParameter(name) {
if (location.href.indexOf('?') > 0) {
var href = location.href.substring(location.href.indexOf('?')+1);
if (href.indexOf('#')>=0)
href = href.substring(0, href.indexOf('#'));
var arrays = href.split('&');
for (var i = 0; i < arrays.length; i++) {
var parameter = arrays[i].split('=');
if (decodeURIComponent(parameter[0]) === name)
return decodeURIComponent(parameter[1]);
}
return '';
} else
return '';
}
//@ sourceURL=delete_ad.js