Skip to content

Instantly share code, notes, and snippets.

@wenketel
Created April 14, 2014 10:50
Show Gist options
  • Save wenketel/10637247 to your computer and use it in GitHub Desktop.
Save wenketel/10637247 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Tieba Preload
// @version 1.1.5
// @description 贴吧预加载,自动加载下一页内容,同时附带消灭贴吧自带 Lazy Load 功能
// @match http://tieba.baidu.com/*
// @include http://tieba.baidu.com/*
// @author 864907600cc
// @icon http://1.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
// @grant none
// @run-at document-end
// @updateURL https://userscripts.org/scripts/source/423917.meta.js
// @downloadURL https://userscripts.org/scripts/source/423917.user.js
// ==/UserScript==
// 贴子判断方式由 kookxiang 提供
// 本脚本基于 GPLv3 协议开源 http://www.gnu.org/licenses/gpl.html‎
// (c) 86497600cc. Some Rights Reserved.
// 以下语句中部分判断代码来自 Chrome 扩展程序 KK 贴吧工具箱 (by kookxiang | https://chrome.google.com/webstore/detail/bcmemjlkdbeephmnklgimfnjlmfhfdgi)
if(typeof PageData!='undefined'&&PageData&&(PageData.product=='pb'||PageData.product=='frs')){ // 判断是否载入
var setting=window.localStorage.getItem('tb_preload_setting')?(window.localStorage.getItem('tb_preload_setting').indexOf('{')>=0?JSON.parse(window.localStorage.getItem('tb_preload_setting')):{thread_preload:1,thread_list_lazyload:1}):{thread_preload:1,thread_list_lazyload:1};
var stylesheet='.tb_preload_notification{position:fixed;right:10px;padding:5px;bottom:-50px;box-shadow:0 0 1px 5px rgba(0,0,0,0.5);background:rgba(0,0,0,0.5);font-size:12px;color:#fff;opacity:0;-webkit-transition:0.25s all linear;-o-transition:0.25s all linear;transition:0.25s all linear;user-select:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;cursor:pointer;z-index:999999}.tb_preload_notification[show]{bottom:10px;opacity:1}.tb_preload_page{width:100%;font-size:14px;text-align:center}.tb_preload_page a{text-decoration:none;color:#333;margin:20px 0;display:block}.tb_preload_setting{color:#2d64b3;cursor:pointer}.list_loading,.right_section [data-type="tb-datalazyload"]{display:none}.vpic_wrap img{max-height:90px}';
var ss=document.createElement('style');
ss.textContent=stylesheet;
document.head.appendChild(ss);
var loading=false,
loader=null;
// 以下语句中代码来自 Kill Tieba Lazy Load (by 864907600cc, Licence: WTFPL)
function kill_tieba_lazyload(){
var codearea=document.getElementById('pblistCodeArea')||document.getElementById('frslistCodeArea')||null;
if(codearea!=null){
//document.getElementById('list_loading').outerHTML=document.getElementById('pblistCodeArea').value;
//document.getElementById('pblistCodeArea').parentElement.removeChild(document.getElementById('pblistCodeArea'));
var lazyload_t_nodes=codearea.childNodes;
for(var i=0;i<lazyload_t_nodes.length;i++){
if(lazyload_t_nodes[i].nodeType==8){
codearea.outerHTML=lazyload_t_nodes[i].data;
//lazyload_nodes[i].parentElement.removeChild(lazyload_nodes[i])
break;
}
}
_.Module.use("pb/widget/ForumListV3", {});
}
if(document.querySelectorAll('img[data-tb-lazyload]')){
var lazyload_i_nodes=document.querySelectorAll('img[data-tb-lazyload]');
for(var i=0;i<lazyload_i_nodes.length;i++){
lazyload_i_nodes[i].setAttribute('src',lazyload_i_nodes[i].getAttribute('data-tb-lazyload'));
lazyload_i_nodes[i].removeAttribute('data-tb-lazyload');
}
}
if(document.querySelectorAll('img[onloadfun="listimg"][original]')){
var lazyload_i_nodes=document.querySelectorAll('img[onloadfun="listimg"][original]');
for(var i=0;i<lazyload_i_nodes.length;i++){
lazyload_i_nodes[i].setAttribute('src',lazyload_i_nodes[i].getAttribute('original'));
}
}
if(PageData.product=='frs'&&document.cookie.match(/batch_delete_mode=(\w{4,5})/)&&document.cookie.match(/batch_delete_mode=(\w{4,5})/)[1]=='true'){
var tls=document.getElementsByClassName('j_thread_list');
for(var i=0;i<tls.length;i++){
if(!tls[i].getElementsByClassName('batch_delete')[0]){
var s='<div class="batch_delete j_batch_delete"><div class="batch_delete_text">勾选</div><input type="checkbox" class="batch_delete_select j_batch_delete_select"></div>';
var t=document.createElement('div');
tls[i].appendChild(t);
t.outerHTML=s;
}
}
}
}
// 以下语句中代码基于 Tieba Preload 修改 (by 864907600cc, Licence: GPLv3 | http://ccloli.com/201212/tieba-preload-source/)
function whether_fetching(){
//console.log('whether_fetching');
var getscroll=document.documentElement.scrollTop||document.body.scrollTop,
scroll_psi=document.body.scrollHeight-getscroll;
if(scroll_psi<=5000&&loading==false){
if(cur_page<total_page){
loading=true;
fetch_next_page();
}
//else window.onscroll=null;
else clearInterval(loader);
}
}
function fetch_next_page(){
//console.log('fetch_next_page');
var url=page_url+'?see_lz='+see_lz+'&pn='+next_page;
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var str=xhr.responseText;
if(PageData.page=='v1')var data=str.split('"p_postlist"')[1].split('<div class="p_thread')[0].match(/>([\s\S]+)/)[1]; // 旧版贴吧
else var data=str.split('"p_postlist"')[1].split('"right_section')[0].match(/>([\s\S]+)<\/div>[\s\S]*<\/div>[\s\S]*<div/)[1]; // 先 split 再 match 减少资源消耗(>20s → <1s)
var node=document.getElementsByClassName('p_postlist')[0],
t_node=document.createElement('div'),
p_node=document.createElement('div');
p_node.className='tb_preload_page';
p_node.innerHTML='<a href="'+url+'">第 '+next_page+' 页,共 '+total_page+' 页</a>';
node.appendChild(p_node);
node.appendChild(t_node);
t_node.outerHTML=data;
kill_tieba_lazyload();
++cur_page;
++next_page;
loading=false;
if(cur_page==total_page)clearInterval(loader);
notification.removeAttribute('show');
}
}
xhr.open('GET',url);
xhr.send();
notification.setAttribute('show','true');
notification.textContent='正在加载第 '+next_page+' 页......';
}
function set_function(v){
if(v==1){
if(setting.thread_preload==1){
setting.thread_preload=0;
setting_node.textContent='开启自动加载';
setting_node_2.textContent='开启自动加载';
if(loader!=null){
clearInterval(loader);
loader=null;
}
notification.textContent='已关闭自动加载......';
notification.setAttribute('show','true');
setTimeout(function(){notification.removeAttribute('show')},5000);
}
else{
setting.thread_preload=1;
setting_node.textContent='关闭自动加载';
setting_node_2.textContent='关闭自动加载';
if(loader==null)preload_listener();
notification.textContent='已开启自动加载......';
notification.setAttribute('show','true');
setTimeout(function(){notification.removeAttribute('show')},5000);
}
}
else if(v==2){
if(setting.thread_list_lazyload==1){
setting.thread_list_lazyload=0;
setting_node.textContent='开启自动去除首页 lazyload';
if(loader!=null){
clearInterval(loader);
loader=null;
}
notification.textContent='已禁用自动去除首页 lazyload......';
notification.setAttribute('show','true');
setTimeout(function(){notification.removeAttribute('show')},5000);
document.getElementById('frs_list_pager').onclick=null;
}
else{
setting.thread_list_lazyload=1;
setting_node.textContent='禁用自动去除首页 lazyload';
if(loader==null)kill_tieba_lazyload_listener();
notification.textContent='已开启自动去除首页 lazyload......';
notification.setAttribute('show','true');
setTimeout(function(){notification.removeAttribute('show')},5000);
}
}
window.localStorage.setItem('tb_preload_setting',JSON.stringify(setting));
}
function preload_listener(){
if(setting.thread_preload==1&&cur_page!=total_page){
//console.log('aaa');
//window.onscroll=function(){ // 改用定时器以节省资源
loader=setInterval(function(){whether_fetching()},1000);
//}
}
}
function kill_tieba_lazyload_listener(){
if(setting.thread_list_lazyload==1){
document.getElementById('frs_list_pager').onclick=function(){
if(loader==null)loader=setInterval(function(){
if(document.getElementById('frslistCodeArea')){
kill_tieba_lazyload();
clearInterval(loader);
loader=null;
kill_tieba_lazyload_listener();
}
},1000);
}
}
}
kill_tieba_lazyload();
var notification=document.createElement('div');
notification.className='tb_preload_notification';
document.body.appendChild(notification);
notification.onclick=function(){set_function(1)};
if(PageData.product=='frs'){
if(document.getElementsByClassName('th_footer_l')[0]){
var setting_node=document.createElement('a'),
page_node=document.getElementsByClassName('th_footer_l')[0];
setting_node.textContent=setting.thread_list_lazyload==1?'禁用自动去除首页 lazyload':'开启自动去除首页 lazyload'; // 好长 _(:з」∠)_
setting_node.className='tb_preload_setting';
setting_node.onclick=function(){set_function(2)};
page_node.appendChild(setting_node);
}
notification.style.pointerEvents='none';
kill_tieba_lazyload_listener();
}
else if(PageData.product=='pb'&&PageData.pager){
/*cur_page=0,
total_page=0,
page_url='',
see_lz=0,*/
var xhr=new XMLHttpRequest(),
cur_page=PageData.pager.cur_page,
total_page=PageData.pager.total_page,
page_url=PageData.thread_url?'//tieba.baidu.com'+PageData.thread_url:location.href.match(/\/\/tieba\.baidu\.com\/p\/\d+/)[0],
see_lz=PageData.special.lz_only,
next_page=cur_page+1;
var setting_node=document.createElement('li'),
page_node=document.getElementsByClassName('l_posts_num');
setting_node.textContent=setting.thread_preload==1?'关闭自动加载':'开启自动加载';
setting_node.className='tb_preload_setting';
var setting_node_2=setting_node.cloneNode(true);
setting_node.onclick=function(){set_function(1)};
setting_node_2.onclick=function(){set_function(1)};
if(page_node[0])page_node[0].insertBefore(setting_node,page_node[0].getElementsByClassName('l_pager')[0].nextSibling);
if(page_node[1])page_node[1].insertBefore(setting_node_2,page_node[1].getElementsByClassName('l_pager')[0].nextSibling);
preload_listener();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment