// --------------------------------------------------------------------
//
// ==UserScript==
// @name 贴子脱水工具
// @namespace https://github.com/kingems/PaperGather
// @version 0.1.8
// @author kingem(kingem@126.com)
// @description 百度贴吧,天涯论坛,豆瓣小组等的贴子脱水工具(浏览的文章版权归原作者所有)
// @grant GM_addStyle
// @require https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js
// @include *://tieba.baidu.com/p/*
// @include *://*/thread-*-*-*.html
// @include *://bbs.tianya.cn/post-*-*-*.shtml
// @include *://www.douban.com/group/*
// @run-at document-end
// @homepageURL https://greasyfork.org/scripts/32466/
// @downloadURL https://update.greasyfork.icu/scripts/32466/%E8%B4%B4%E5%AD%90%E8%84%B1%E6%B0%B4%E5%B7%A5%E5%85%B7.user.js
// @updateURL https://update.greasyfork.icu/scripts/32466/%E8%B4%B4%E5%AD%90%E8%84%B1%E6%B0%B4%E5%B7%A5%E5%85%B7.meta.js
// ==/UserScript==
//
// --------------------------------------------------------------------
(function(){
var Rule = {
};
Rule.specialSite = [
{siteName : 'tieba',
url : '.*?://tieba\\.baidu\\.com/p/\\d+',
//获取banner位置
banner_path: 'div.p_thread',
//获取楼层位置
floor_path:'div.l_post',
//获取页面编码格式
page_charset:function(){
var h = $('head').html();
var h_m = h.match(/charset=\"(.*?)\">/);
if(!h_m) return 'gb2312';
return h_m[1];
},
//获取总页数
get_page_num:function(data){
var pg = $('li.l_reply_num');
if (data) pg=$(data).find('li.l_reply_num');
if(!pg) return;
var num = pg.eq(0).html().replace(/<[^>]*>/g,'');
var num_m = num.match(/共(\d+)页/);
if(!num_m) return 1;
return num_m[1];
},
//获取标题
get_topic_name:function(){
return $('title').text();
},
//获得第1页网址
format_thread_url_1st:function(url,islz){
url = url.replace(/[\?\#].*$/, '');
if (islz) url += '?see_lz=1';
return url;
},
// 格式化第i页网址
format_thread_url_ith:function(url,i){
var j = i.toString();
if(url.match(/\?see_lz=1/)){
n_url = url.concat('&pn='+j);
}else {
n_url = url.replace(/$/, '?pn='+j);
}
return n_url;
},
//提取楼层主要信息
extract_floor_info:function(bot,index) {
var info = bot;
var re = new Object;
var ptail = info.attr('data-field');
if (!ptail) {
return null;
};
re["poster"] = info.find('li.d_name').text(); //作者
re["id"] = ptail.match(/"post_no":(.*?),/)[1]; //楼层
if (ptail.match(/"date":"(.*?)"/)){
re["time"] = ptail.match(/"date":"(.*?)"/)[1];//时间
}else{
re["time"] =info.find('span.tail-info').text().split('楼')[1];//时间
}
re["content"] = info.find('div.d_post_content').html().
replace(/<\/?font[^>]*>/g, ''); //内容
re["word_num"] = re["content"].replace('<[^>]+>','').length; // 字数
return re;
},
},
{siteName : 'tianya',
url : '.*?://bbs\\.tianya\\.cn/post-.*?-.*?-\\d+\\.shtml',
//获取banner位置
banner_path: 'div.atl-menu',
//获取楼层位置
floor_path:'div.atl-content',
//获取页面编码格式
page_charset:function(){
var h = $('head').html();
var h_m = h.match(/charset=\"(.*?)\"/);
if(!h_m) return 'utf-8';
return h_m[1];
},
//获取总页数
get_page_num:function(data){
var pg = $('div.atl-pages');
if (data) pg = $(data).find('div.atl-pages');
if(!pg) return;
var num = pg.eq(0).html().replace(/<[^>]*>/g,' ').replace(/\s+/g,' ');
var num_m = num.match(/(\d+) 下页/);
if(!num_m) return 1;
return num_m[1];
},
//获取标题
get_topic_name:function(){
return $('h1').text();
},
//获得第1页网址
format_thread_url_1st:function(url,islz){
return url.replace(/\d+.shtml/, '1.shtml');
},
// 格式化第i页网址
format_thread_url_ith:function(url,i){
var j = i.toString();
var n_url = url.replace(/\d+.shtml/, j+'.shtml');
return n_url;
},
//提取楼层主要信息
extract_floor_info:function(bot,index) {
var info = bot.parent();
var re = new Object;
re["content"] = info.find('div.atl-content').html().
replace(/<\/?font[^>]*>/g, '').replace(/
(\s*.*)*<\/div>/,'').replace(/
]+class="atl-reply(\s*.*)*<\/div>/,'');
re["word_num"] = re["content"].replace('<[^>]+>','').length;
var atinfo = info.find('.atl-info').text();
if (!atinfo) {
atinfo = info.parent().prev().find('.atl-info').text();
re["id"] = 1;
}else{
re["id"] = parseInt(info.attr('id'))+1;
}
re["poster"] = atinfo.match(/[楼主作者]{2}:(.*?)\s*时间/)[1];
re["time"] = atinfo.match(/时间:([\d\s-:]+)/)[1];
return re;
},
},
{siteName : 'douban',
url : '.*?://www\\.douban\\.com/group/.*?',
//获取banner位置
banner_path: 'div.article',
//获取楼层位置
floor_path:'div.user-face',
//获取页面编码格式
page_charset:function(){
var h = $('head').html();
var h_m = h.match(/charset=\"(.*?)\"/);
if(!h_m) return 'utf-8';
return h_m[1];
},
//获取总页数
get_page_num:function(data){
var pg = $('.thispage');
if (data) pg=$(data).find('.thispage');
if(!pg) return 1;
return pg.eq(0).attr('data-total-page');
},
//获取标题
get_topic_name:function(){
return $('h1').text();
},
//获得第1页网址
format_thread_url_1st:function(url,islz){
url = url.replace(/\?start.*$/,'').replace(/[\#\&].*$/, '');
if(islz) url += '?author=1';
return url;
},
// 格式化第i页网址
format_thread_url_ith:function(url,i){
var j = (i-1).toString();
var n_url = url;
if(url.match(/\?author=1/)){
n_url = url.concat('&start='+j+'00');
}else {
n_url = url.replace(/$/, '?start='+j+'00');
}
return n_url;
},
//提取楼层主要信息
extract_floor_info:function(bot,index) {
var info = bot;
var re = new Object;
re["id"] = index;
re["poster"] = info.find('img.pil').attr('alt');
re["time"] = info.next().text().match(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/)[1];
re["content"] = info.next().html().replace(/
\s*.*\s*.*\s*<\/h[34]>/,'').replace(/<\/?font[^>]*>/g, '').replace(/]+class=".*lnk.*<\/a>/g,'');
re["word_num"] = re["content"].replace('<[^>]+>','').length;
return re;
},
},
{siteName : 'discuz',
url : '.*?://.*?/thread-.*?-.*?-1\.html',
//获取banner位置
banner_path: 'div#pt',
//获取楼层位置
floor_path:'div#postlist table.plhin',
//获取页面编码格式
page_charset:function(){
var h = $('head').html();
var h_m = h.match(/charset=\"(.*?)\"/);
if(!h_m) return 'gb2312';
return h_m[1];
},
//获取总页数
get_page_num:function(data){
var pg = $('div#pgt');
if (data) pg=$(data).find('div#pgt');
if(!pg) return;
var num = pg.eq(0).html().replace(/<[^>]*>/g,' ');
var num_m = num.match(/[\D\s]*(\d+)\s*(页|下一页)/);
if(!num_m) return;
return num_m[1];
},
//获取标题
get_topic_name:function(){
return $('#thread_subject').text();
},
//获得第1页网址
format_thread_url_1st:function(url,islz){
url = url.replace(/\/thread-(\d+)-\d+-1.html$/, '/forum.php?mod=viewthread&tid=$1&page=1');
url = url.replace(/#.*$/, '').replace(/&extra=[^&]*/,'').replace(/&page=\d+/, '&page=1');
if(! url.match(/&page=\d+/)){
url = url.concat('&page=1');
}
return url;
},
// 格式化第i页网址
format_thread_url_ith:function(url,i){
var j = i.toString();
var n_url = url.replace(/&page=\d+/, '&page='+ j);
return n_url;
},
//提取楼层主要信息
extract_floor_info:function(bot,index) {
var info = bot.parent();
var re = new Object;
re["poster"] = info.find('div.authi').eq(0).text();
re["time"] = info.find('div.authi em').text().replace('发表于','');
re["id"] = index;
re["content"] = info.find('td.t_f').html().
replace(/<[^>]+style="display:none"[^>]*>[^<]+<[^>]+>/g, '').
replace(/<[^>]+class="jammer"[^>]*>[^<]+<[^>]+>/g, '').
replace(/<\/?font[^>]*>/g, '');
re["word_num"] = re["content"].replace('<[^>]+>','').length;
return re;
},
},
];
// 通用函数
// 获取特殊规则
function getCurSiteInfo() {
var rules = Rule.specialSite;
var locationHref = location.href;
var info = {};
for (var i in rules) {
var x = rules[i];
var url = new RegExp(x.url, 'ig');
if (url.test(locationHref)){
info = x;
break;
}
}
return info;
};
//添加选项框
function add_dewater_banner(xp) {
$dewater_div = $('\
\
\
显示页至\
页的内容,\
且只显示楼至\
楼的内容,\
每楼最少字,\
只看楼主,\
\
');
$(xp).eq(0).before($dewater_div);
$('#dewater_btn').click(function(){
var option = get_dewater_option();
if (option.set_min_floor && option.set_max_floor && option.set_min_floor> option.set_max_floor) {
alert("显示楼层数填写错误");
return;
}
var main_floors = get_thread_floors(option);
option.poster = get_topic_poster(main_floors);
var topic=set_topic('#dewater_title');
set_dewater_head(topic);
$('#dewater_floors').html('');
add_floor_onpage(main_floors,option);
$('body').html($('#dewater_div').html());
set_btn_click();
set_top_btn();
});
$main_floors = $('\
');
$(xp).before($main_floors);
};
//设置标题
function set_topic(dst) {
var tp = site.get_topic_name() ;
var c = '' +tp+ ''+
'\
前往楼, \
\
\
\
\
'+
'
';
$(dst).html(c);
return tp;
};
//添加按钮点击事件
function set_btn_click() {
$('#go_floor_btn').click(function(){
var gofloornum = $("#go_floor_num")[0].value;
if (gofloornum) {
var url=site.format_thread_url_1st(location.href,false);
location.href = url+"#floor"+gofloornum;
}
});
$('#goback_btn').click(function(){
location.href = $('#backurl').attr('href');
});
$('#flip_btn').click(function(){
$('.floorhide').each(function(index){
if (index<30) {
$(this).show();
$(this).attr('class','floor');
}
});
if ($('.floorhide').length < 1) $('#flip_btn').remove();
});
$('#save_txt_btn').click(function(){
var chapters = [];
var fileobj = $('#backurl');
var fileName = fileobj.text();
var fileUrl = fileobj.attr('href');
chapters.push(fileName);
chapters.push(fileUrl);
$('.floor').each(function(){
var s=$(this).html();
var values = s.replace(/ /g,'').replace(/\s+/g,' ').replace(/
/g,'\r\n').replace(//g,'[我是一张图片]<').replace(/<[^>]*>/g, '');
chapters.push(values);
});
$('.floorhide').each(function(){
var s=$(this).html();
var values = s.replace(/ /g,'').replace(/\s+/g,' ').replace(/
/g,'\r\n').replace(//g,'[我是一张图片]<').replace(/<[^>]*>/g, '');
chapters.push(values);
});
saveAs(chapters.join('\r\n-----------------\r\n'),fileName+'.txt');
});
$('#save_html_btn').click(function(){
var fileName = $('#backurl').text();
var chapters = [''];
var htmltext = $('*').html().replace(/class="floorhide"/g,'class=\"floor\"').replace(/]class="flip".*<\/div>/,'').replace(/
]class="topbtn".*<\/div>/,'').replace(/