Tieba Preload

贴吧预加载,自动加载下一页内容,同时附带消灭贴吧自带 Lazy Load 功能

As of 2015-05-06. See the latest version.

  1. // ==UserScript==
  2. // @name Tieba Preload
  3. // @version 1.3.10
  4. // @description 贴吧预加载,自动加载下一页内容,同时附带消灭贴吧自带 Lazy Load 功能
  5. // @match http://tieba.baidu.com/*
  6. // @include http://tieba.baidu.com/*
  7. // @exclude http://tieba.baidu.com/*tp=1*
  8. // @exclude http://tieba.baidu.com/*tab=*
  9. // @exclude http://tieba.baidu.com/photo/*
  10. // @author 864907600cc
  11. // @icon http://1.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
  12. // @grant GM_registerMenuCommand
  13. // @noframes
  14. // @namespace http://ext.ccloli.com
  15. // ==/UserScript==
  16.  
  17. // 贴子判断方式由 kookxiang 提供
  18. // 本脚本基于 GPLv3 协议开源 http://www.gnu.org/licenses/gpl.html‎
  19. // (c) 86497600cc. Some Rights Reserved.
  20.  
  21. // 修复使用 GM 函数后脚本失效的问题,thanks to shyangs
  22. var //window=unsafeWindow,
  23. _ = unsafeWindow._,
  24. PageData = unsafeWindow.PageData,
  25. Page = unsafeWindow.Page,
  26. $ = unsafeWindow.$,
  27. //forumlist=unsafeWindow.forumlist,
  28. no_refresh_url_prefix = (typeof unsafeWindow.no_refresh_url_prefix != 'undefined') ? unsafeWindow.no_refresh_url_prefix : null;
  29. //is_firefox=navigator.userAgent.indexOf('Firefox')>=0?true:false;
  30.  
  31. var notification = document.createElement('div'),
  32. setting = (window.localStorage.getItem('tb_preload_setting') && window.localStorage.getItem('tb_preload_setting').indexOf('{') >= 0) ? JSON.parse(window.localStorage.getItem('tb_preload_setting')) : {},
  33. stylesheet = '@keyframes setting_panel_show{0%{top:100px;opacity:0}80%{top:-20px;opacity:1}100%{top:0px;opacity:1}}@-webkit-keyframes setting_panel_show{0%{top:100px;opacity:0}80%{top:-20px;opacity:1}100%{top:0px;opacity:1}}@keyframes setting_panel_hide{0%{top:0px;opacity:1}20%{top:-20px;opacity:1}100%{top:100px;opacity:0}}@-webkit-keyframes setting_panel_hide{0%{top:0px;opacity:1}20%{top:-20px;opacity:1}100%{top:100px;opacity:0}}.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;pointer-events:none}.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;font-size:14px}.tb_preload_setting{color:#2d64b3;cursor:pointer}.list_loading,.right_section [data-type="tb-datalazyload"]{display:none}.vpic_wrap img{max-height:90px}.tb_preload_setting_panel{width:600px;height:200px;margin:auto;position:fixed;top:0;bottom:0;left:0;right:0;z-index:999998;box-shadow:rgba(0,0,0,.5) 0 0 0 2500px,rgba(255,255,255,.75) 0 0 200px;background:rgba(255,255,255,.75);font-size:14px;-webkit-user-select:none;-moz-user-select:none;-webkit-animation:setting_panel_show 0.75s ease-in;animation:setting_panel_show 0.75s ease-in;font-family:"Hiragino Sans GB","Microsoft Yahei","WenQuanYi Micro Hei",Arial,Tahoma,sans-serif}.tb_preload_setting_panel[close]{-webkit-animation:setting_panel_hide 0.75s ease-out;animation:setting_panel_hide 0.75s ease-out}.tb_preload_setting_panel_bg{width:100%;height:100%;position:fixed;left:0;top:0;z-index:999997}.tb_preload_setting_panel span,.tb_preload_setting_panel a{display:block;cursor:pointer;height:20px;line-height:20px;margin:10px;-webkit-transition:all 0.25s linear;-moz-transition:all 0.25s linear;transition:all 0.25s linear}.tb_preload_setting_panel span{color:#F00;padding-left:50px}.tb_preload_setting_panel span:hover{text-shadow:#F00 0 0 1px}.tb_preload_setting_panel span[active]:hover,.tb_preload_setting_panel a:hover{text-shadow:#7fb900 0 0 1px}.tb_preload_setting_panel span::before{content:"×";display:inline-block;width:20px}.tb_preload_setting_panel span[active]{color:#7FB900}.tb_preload_setting_panel span[active]::before{content:"√"}.tb_preload_setting_panel p{margin:20px;font-size:20px;height:24px;line-height:24px;cursor:default;font-family:"Segoe Script"!important;text-shadow: #000 0 0 2px}.tb_preload_setting_panel a{color:#7FB900;font-family:"Segoe Script"!important;text-align:center}.tbui_follow_fixed{display:block!important}img[origin-src]{display:none!important}',
  34. setting_panel = false,
  35. ss = document.createElement('style'),
  36. notification_timer = null;
  37. ss.textContent = stylesheet;
  38. document.head.appendChild(ss);
  39. notification.className = 'tb_preload_notification';
  40. document.body.appendChild(notification);
  41. if (setting.thread_preload == null) setting.thread_preload = 1;
  42. if (setting.thread_list_lazyload == null) setting.thread_list_lazyload = 1;
  43. if (setting.notification_1_3_4 = null) {
  44. setting.notification_1_3_4 = 1;
  45. alert('Tieba Preload Notification\n\n1. 自该版本起添加 @namespace,若出现重复安装的情况请删除旧脚本;\n2. Firefox 下不翻页 bug 已临时使用定时器解决,为保证楼中楼完全加载,请注意在载入页面完成后不要在 1s 内滚动到页面底部 = = 关于该 bug 引起的具体原因仍旧不明,不知道是不是 Firefox 特有的 bug……\n3. 现可以通过在贴子页的“开启/关闭自动加载”处单击鼠标右键打开设置界面,原开启设置界面的方式保留;\n4. 1.3.5 版本起按住 Ctrl 键鼠标左键点击页面内的“开启/关闭自动加载”可设置仅当前页生效。');
  46. window.localStorage.setItem('tb_preload_setting', JSON.stringify(setting));
  47. }
  48.  
  49. function set_function(v, k) {
  50. if (k == null) k = true;
  51. switch (v) {
  52. case 1:
  53. if (setting.thread_preload == 1) {
  54. setting.thread_preload = 0;
  55. if (setting_node) {
  56. setting_node.textContent = '开启自动加载';
  57. setting_node_2.textContent = '开启自动加载';
  58. }
  59. if (PageData.product == 'pb' && typeof loader != 'undefined' && loader != null) {
  60. clearInterval(loader);
  61. loader = null;
  62. }
  63. show_notification(1, '已关闭自动加载......');
  64. notification.setAttribute('show', 'true');
  65. notification_timer = setTimeout(function() {
  66. notification.removeAttribute('show')
  67. }, 5000);
  68. }
  69. else {
  70. setting.thread_preload = 1;
  71. if (setting_node) {
  72. setting_node.textContent = '关闭自动加载';
  73. setting_node_2.textContent = '关闭自动加载';
  74. }
  75. if (PageData.product == 'pb' && typeof loader != 'undefined' && loader == null) preload_listener();
  76. show_notification(1, '已开启自动加载......');
  77. }
  78. break;
  79. case 2:
  80. if (setting.thread_list_preload == 1) {
  81. alert('当开启首页贴子列表预加载时,该项不能关闭。');
  82. }
  83. else if (setting.thread_list_lazyload == 1) {
  84. setting.thread_list_lazyload = 0;
  85. if (PageData.product == 'frs' && typeof loader != 'undefined' && loader != null) {
  86. clearInterval(loader);
  87. loader = null;
  88. if (document.getElementById('frs_list_pager')) document.getElementById('frs_list_pager').onclick = null;
  89. }
  90. show_notification(1, '已禁用自动去除首页 lazyload......');
  91. }
  92. else {
  93. setting.thread_list_lazyload = 1;
  94. //setting_node.textContent='禁用自动去除首页 lazyload';
  95. if (PageData.product == 'frs' && typeof loader != 'undefined' && loader == null) {
  96. kill_tieba_lazyload_listener();
  97. }
  98. show_notification(1, '已开启自动去除首页 lazyload......');
  99. }
  100. break;
  101. case 3:
  102. if (setting.thread_list_preload == 1) {
  103. setting.thread_list_preload = 0;
  104. if (PageData.product == 'frs' && typeof loader != 'undefined' && loader != null) {
  105. clearInterval(loader);
  106. loader = null;
  107. if (document.getElementById('frs_list_pager')) document.getElementById('frs_list_pager').onclick = null;
  108. }
  109. show_notification(1, '已关闭自动加载......');
  110. }
  111. else {
  112. setting.thread_list_preload = 1;
  113. setting.thread_list_lazyload = 1;
  114. if (PageData.product == 'frs' && typeof loader != 'undefined' && loader == null) {
  115. preload_listener();
  116. }
  117. show_notification(1, '已开启自动加载......');
  118. }
  119. break;
  120. }
  121. if (k == true) window.localStorage.setItem('tb_preload_setting', JSON.stringify(setting));
  122. update_setting_panel();
  123. }
  124.  
  125. function call_setting() {
  126. if (setting_panel == false) {
  127. setting_panel = true;
  128. var panel = document.createElement('div'),
  129. panel_bg = document.createElement('div');
  130. panel_bg.className = 'tb_preload_setting_panel_bg';
  131. panel.className = 'tb_preload_setting_panel';
  132. panel_bg.onclick = function() {
  133. panel.setAttribute('close', true);
  134. setTimeout(function() {
  135. panel.parentElement.removeChild(panel);
  136. }, 700);
  137. panel_bg.parentElement.removeChild(panel_bg);
  138. setting_panel = false;
  139. }
  140. panel_bg.setAttribute('title', '点击以关闭设置界面');
  141. panel.innerHTML = '<p align="center">Tieba Preload Setting</p><span class="tbpreload_setting_panel_thread_preload">自动加载贴子下一页</span><span class="tbpreload_setting_panel_thread_list_lazyload">去除贴吧首页 lazyload</span><span class="tbpreload_setting_panel_thread_list_preload">自动加载贴子列表下一页</span><a href="http://tieba.baidu.com/p/2939166340">Feedback</a>';
  142. document.body.appendChild(panel);
  143. document.body.appendChild(panel_bg);
  144. var panel_thread_preload = document.getElementsByClassName('tbpreload_setting_panel_thread_preload')[0],
  145. panel_thread_list_lazyload = document.getElementsByClassName('tbpreload_setting_panel_thread_list_lazyload')[0],
  146. panel_thread_list_preload = document.getElementsByClassName('tbpreload_setting_panel_thread_list_preload')[0];
  147. panel_thread_preload.onclick = function() {
  148. set_function(1);
  149. }
  150. panel_thread_list_lazyload.onclick = function() {
  151. set_function(2);
  152. }
  153. panel_thread_list_preload.onclick = function() {
  154. set_function(3);
  155. }
  156. update_setting_panel();
  157. }
  158. }
  159.  
  160. function update_setting_panel() {
  161. var panel_thread_preload = document.getElementsByClassName('tbpreload_setting_panel_thread_preload')[0],
  162. panel_thread_list_lazyload = document.getElementsByClassName('tbpreload_setting_panel_thread_list_lazyload')[0],
  163. panel_thread_list_preload = document.getElementsByClassName('tbpreload_setting_panel_thread_list_preload')[0];
  164. if (setting.thread_preload == 1) panel_thread_preload.setAttribute('active', 'true');
  165. else if (panel_thread_preload.hasAttribute('active')) panel_thread_preload.removeAttribute('active');
  166. if (setting.thread_list_lazyload == 1) panel_thread_list_lazyload.setAttribute('active', 'true');
  167. else if (panel_thread_list_lazyload.hasAttribute('active')) panel_thread_list_lazyload.removeAttribute('active');
  168. if (setting.thread_list_preload == 1) panel_thread_list_preload.setAttribute('active', 'true');
  169. else if (panel_thread_list_preload.hasAttribute('active')) panel_thread_list_preload.removeAttribute('active');
  170. }
  171.  
  172. function show_notification(n, c) {
  173. notification.textContent = c;
  174. notification.setAttribute('show', 'true');
  175. if (notification_timer != null) {
  176. clearTimeout(notification_timer);
  177. notification_timer = null;
  178. }
  179. if (n == 1) {
  180. notification_timer = setTimeout(function() {
  181. notification.removeAttribute('show');
  182. notification_timer = null;
  183. }, 5000);
  184. }
  185. }
  186.  
  187. if (typeof PageData != 'undefined' && PageData && (PageData.product == 'pb' || PageData.product == 'frs')) { // 判断是否载入
  188. var loading = false,
  189. loader = null,
  190. pager_timer = null;
  191.  
  192. // 以下语句中代码来自 Kill Tieba Lazy Load (by 864907600cc, Licence: WTFPL)
  193.  
  194. function kill_tieba_lazyload() {
  195. //console.log(1);
  196. var codearea = document.getElementById('pblistCodeArea') || document.getElementById('frslistCodeArea') || null;
  197. if (codearea != null) {
  198. //document.getElementById('list_loading').outerHTML=document.getElementById('pblistCodeArea').value;
  199. //document.getElementById('pblistCodeArea').parentElement.removeChild(document.getElementById('pblistCodeArea'));
  200. var lazyload_t_nodes = codearea.childNodes;
  201. for (var i = 0; i < lazyload_t_nodes.length; i++) {
  202. if (lazyload_t_nodes[i].nodeType == 8) {
  203. codearea.outerHTML = lazyload_t_nodes[i].data;
  204. //lazyload_nodes[i].parentElement.removeChild(lazyload_nodes[i])
  205. break;
  206. }
  207. }
  208. }
  209. if (document.querySelectorAll('img[data-tb-lazyload]')) {
  210. var lazyload_i_nodes = document.querySelectorAll('img[data-tb-lazyload]');
  211. for (var i = 0; i < lazyload_i_nodes.length; i++) {
  212. lazyload_i_nodes[i].setAttribute('src', lazyload_i_nodes[i].getAttribute('data-tb-lazyload'));
  213. lazyload_i_nodes[i].removeAttribute('data-tb-lazyload');
  214. }
  215. }
  216. if (document.querySelectorAll('img[onloadfun="listimg"][original]')) {
  217. var lazyload_i_nodes = document.querySelectorAll('img[onloadfun="listimg"][original]');
  218. for (var i = 0; i < lazyload_i_nodes.length; i++) {
  219. lazyload_i_nodes[i].setAttribute('src', lazyload_i_nodes[i].getAttribute('original'));
  220. //lazyload_i_nodes[i].setAttribute('immediate', '');
  221. lazyload_i_nodes[i].addEventListener('load', function(){loadPic(this,0,{"width":400,"height":90});});
  222. }
  223. }
  224. if (PageData.product == 'pb') {
  225. //_.Module.use("pb/widget/ForumListV3", {});
  226. _.Module.use('pb/component/NoAutoVideo', [{
  227. text_videofrom: '视频来自: '
  228. }]);
  229. //rebind_image();
  230. //Page.bindOpenImg();
  231. //console.log('触发强制显示楼中楼');
  232. //if(typeof window.forumlist=='object'&&typeof window.forumlist._loadSubList=='object'){
  233. /*if(is_firefox)setTimeout(function(){kill_lzl();},500);
  234. else*/
  235. kill_lzl();
  236. //}
  237. //else var k=setInterval(function(){if(typeof window.forumlist=='object'&&typeof window.forumlist._loadSubList=='object')clearInterval(k);kill_lzl();},1000)
  238. //console.log('触发强制显示楼中楼完成');
  239. }
  240. if (PageData.product == 'frs' && document.cookie.match(/batch_delete_mode=(\w{4,5})/) && document.cookie.match(/batch_delete_mode=(\w{4,5})/)[1] == 'true') {
  241. var tls = document.getElementsByClassName('j_thread_list');
  242. for (var i = 0; i < tls.length; i++) {
  243. if (!tls[i].getElementsByClassName('batch_delete')[0]) {
  244. 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>';
  245. var t = document.createElement('div');
  246. tls[i].appendChild(t);
  247. t.outerHTML = s;
  248. }
  249. }
  250. }
  251. }
  252.  
  253. function show_lzl(c) {
  254. var t = document.createElement('iframe');
  255. t.style.cssText = 'width:0;height:0;position:absolute'
  256. t.setAttribute('onload', c);
  257. document.body.appendChild(t);
  258. // kill_lzl();
  259. //document.body.removeChild(t); // 取消自动移除 iframe,因为该操作可能导致在 Firefox 下无法运行
  260. }
  261.  
  262. function kill_lzl() {
  263. // Fuck LZL Thanks To 8qwe24657913
  264. //console.log('执行强制显示楼中楼');
  265. /* var a = document.getElementsByClassName('hideLzl'),
  266. b = window.forumlist._loadSubList,
  267. c = a.length;
  268. /*while (--c > -1) {
  269. console.log(a)
  270. console.log('开始处理 - '+(c+1));
  271. b.showLzlPost(a[c]);
  272. console.log('showLzlPost - '+(c+1));
  273. a[c].setAttribute('class',a[c].getAttribute('class').replace(/ ?hideLzl/,''));
  274. console.log('处理完成 - '+(c+1));
  275. }*/
  276. //for(var i=0;i<c;i++){
  277. /* while(a[0]){
  278. console.log(a)
  279. //console.log('开始处理 - ');
  280. console.log(a[0])
  281. b.showLzlPost(a[0]);
  282. //console.log('showLzlPost - ');
  283. a[0].setAttribute('class',a[0].getAttribute('class').replace(/ ?hideLzl/,''));
  284. //console.log('处理完成 - ');
  285. }
  286. //console.log('楼中楼处理完成!');
  287. /*for(var i=0;i<c.length;i++){
  288. b.showLzlPost(a[i]);
  289. a[i].className=a[i].className.replace(/ ?hideLzl/,'');
  290. }*/
  291. // Fuck LZL Thanks To 网络孤独行客
  292. /*(function($){
  293. $(document).ajaxSuccess(function(e,xhr,s){
  294. (s.url.search("pn") !== -1) && lazyload();
  295. });
  296. var lazyload = function lazyload() {*/
  297. if ("undefined" !== typeof unsafeWindow.datalazyload) {
  298. if (unsafeWindow.datalazyload.userConfig.diff === 500) {
  299. unsafeWindow.datalazyload.userConfig.diff = 99999;
  300. return window.scrollTo(0, (document.body.scrollTop || document.documentElement.scrollTop) - 1);
  301. }
  302. else return setTimeout( /*lazyload*/ kill_lzl, 200);
  303. }
  304. else setTimeout(kill_lzl, 200);
  305. /*
  306. setTimeout(lazyload,200);
  307. }
  308. lazyload();
  309. /})(unsafeWindow.jQuery);*/
  310. }
  311.  
  312. // 以下语句中代码基于 Tieba Preload 修改 (by 864907600cc, Licence: GPLv3 | http://ccloli.com/201212/tieba-preload-source/)
  313.  
  314. function whether_fetching() {
  315. //console.log('whether_fetching');
  316. var getscroll = document.documentElement.scrollTop || document.body.scrollTop,
  317. scroll_psi = document.body.scrollHeight - getscroll,
  318. psi = PageData.product == 'pb' ? 5000 : PageData.forum.version == 2 ? 2500 : 1500;
  319. if (scroll_psi <= psi && loading == false) {
  320. if (cur_page < total_page) {
  321. loading = true;
  322. fetch_next_page();
  323. }
  324. //else window.onscroll=null;
  325. else {
  326. clearInterval(loader);
  327. loader = null
  328. }
  329. }
  330. }
  331.  
  332. function fetch_next_page() {
  333. //console.log('fetch_next_page');
  334. kill_lzl();
  335. switch (PageData.product) {
  336. case 'pb':
  337. var url = page_url + '?see_lz=' + see_lz + '&pn=' + next_page;
  338. xhr.onreadystatechange = function() {
  339. if (xhr.readyState == 4 && xhr.status == 200) {
  340. var str = xhr.responseText;
  341. if (PageData.page == 'v1') var data = str.split('"p_postlist"')[1].split('<div class="p_thread')[0].match(/>([\s\S]+)/)[1]; // 旧版贴吧
  342. 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)
  343. var node = document.getElementsByClassName('p_postlist')[0],
  344. t_node = document.createElement('div'),
  345. p_node = document.createElement('div');
  346. p_node.className = 'tb_preload_page';
  347. p_node.innerHTML = '<a href="' + url + '">第 ' + next_page + ' 页,共 ' + total_page + ' 页</a>';
  348. node.appendChild(p_node);
  349. node.appendChild(t_node);
  350. t_node.outerHTML = data;
  351.  
  352. if (str.match(/<script>.*(_.Module.use\("pp?b\/widget\/postList",[\s\S]+?)<\/script>/)) {
  353. var lzl = str.match(/<script>.*(_.Module.use\("pp?b\/widget\/postList",[\s\S]+?)<\/script>/)[1];
  354. show_lzl(lzl);
  355. }
  356. kill_tieba_lazyload();
  357. ++cur_page;
  358. ++next_page;
  359. PageData.pager.cur_page = cur_page;
  360. see_lz = window.location.href.match(/see_lz/) ? window.location.href.match(/see_lz=(\d)/)[1] : PageData.special.lz_only;
  361. loading = false;
  362. if (cur_page == total_page) {
  363. clearInterval(loader);
  364. loader = null;
  365. }
  366. notification.removeAttribute('show');
  367. }
  368. }
  369. xhr.open('GET', url);
  370. xhr.send();
  371. notification.setAttribute('show', 'true');
  372. show_notification(0, '正在加载第 ' + next_page + ' 页......');
  373. break;
  374. case 'frs':
  375. var url = page_url + '&pn=' + next_page;
  376. if (PageData.forum.version == 2) {
  377. var xurl = page_url + '&apage=1&pn=' + next_page;
  378. }
  379. else {
  380. if (location.hash.indexOf('#!') >= 0) {
  381. var hash_data = location.hash.match(/#\!\/(.)/)[1];
  382. if (hash_data != last_hash_data) {
  383. switch (hash_data) {
  384. case 'm':
  385. page_url = page_url.replace(/f.+kw=/, 'f?kw=');
  386. last_hash_data = 'm';
  387. cur_page = 0;
  388. next_page = cur_page + 50;
  389. total_page = document.querySelector('a.last').href.match(/pn=(\d+)/)[1];
  390. break;
  391. case 'n':
  392. page_url = page_url.replace(/f.+kw=/, 'f/good?kw=');
  393. last_hash_data = 'n';
  394. cur_page = 0;
  395. next_page = cur_page + 50;
  396. total_page = document.querySelector('a.last').href.match(/pn=(\d+)/)[1];
  397. break;
  398. case 'v':
  399. page_url = page_url.replace(/f.+kw=/, 'f/vote?kw=');
  400. last_hash_data = 'v';
  401. cur_page = 0;
  402. next_page = cur_page + 50;
  403. total_page = document.querySelector('a.last').href.match(/pn=(\d+)/)[1];
  404. break;
  405. }
  406. }
  407. }
  408. var xurl = page_url + '&ajax=1&pn=' + next_page
  409. }
  410. xhr.onreadystatechange = function() {
  411. if (xhr.readyState == 4 && xhr.status == 200) {
  412. var str = xhr.responseText;
  413. if (PageData.forum.version == 2) {
  414. var data = str.match(/<ul id="thread_list"[\s\S]*?>([\s\S]+)<\/ul>/)[1],
  415. node = document.getElementById('thread_list');
  416. p_node = document.createElement('div'),
  417. context = '<a href="' + url + '">第 ' + (parseInt(next_page / 50) + 1) + ' 页,共 ' + (parseInt(total_page / 50) + 1) + ' 页</a>';
  418. }
  419. else {
  420. var data = JSON.parse(str).data.content.split('<tbody>')[1].split('</tbody>')[0], //str.split('<tbody>')[1].split('</tbody>')[0],
  421. node = document.querySelector('#thread_list tbody'),
  422. p_node = document.createElement('tr'),
  423. context = '<td colspan="5"><a href="' + url + '">第 ' + (parseInt(next_page / 50) + 1) + ' 页,共 ' + (parseInt(total_page / 50) + 1) + ' 页</a></td>';
  424. }
  425. var //node=document.getElementById('thread_list'),
  426. t_node = document.createElement('div')
  427. /*,
  428. p_node=document.createElement('div')*/
  429. ;
  430. p_node.className = 'tb_preload_page';
  431. p_node.innerHTML = context;
  432. node.appendChild(p_node);
  433. node.appendChild(t_node);
  434. t_node.outerHTML = data;
  435. kill_tieba_lazyload();
  436. cur_page += 50;
  437. next_page += 50;
  438. loading = false;
  439. if (cur_page == total_page) {
  440. clearInterval(loader);
  441. loader = null;
  442. }
  443. notification.removeAttribute('show');
  444. }
  445. }
  446. xhr.open('GET', xurl);
  447. xhr.send();
  448. notification.setAttribute('show', 'true');
  449. show_notification(0, '正在加载第 ' + (parseInt(next_page / 50) + 1) + ' 页......');
  450. break;
  451. }
  452. }
  453.  
  454. function preload_listener() {
  455. if (cur_page != total_page) {
  456. //console.log('aaa');
  457. //window.onscroll=function(){ // 改用定时器以节省资源
  458. loader = setInterval(function() {
  459. whether_fetching()
  460. }, 1000);
  461. //}
  462. }
  463. }
  464.  
  465. function kill_tieba_lazyload_listener() {
  466. kill_tieba_lazyload();
  467. if (document.getElementById('frs_list_pager')) document.getElementById('frs_list_pager').onclick = function() {
  468. datalazyload.userConfig.diff = 99999;
  469. if (loader == null) loader = setInterval(function() {
  470. /*if (document.getElementById('frslistCodeArea')) {
  471. //kill_tieba_lazyload();
  472. clearInterval(loader);
  473. loader = null;
  474. kill_tieba_lazyload_listener();
  475. }*/
  476. if (datalazyload.userConfig.diff === 500) {
  477. kill_tieba_lazyload();
  478. clearInterval(loader);
  479. loader = null;
  480. kill_tieba_lazyload_listener();
  481. }
  482. }, 1000);
  483. }
  484. }
  485.  
  486. function rebind_image() {
  487. var imglist = document.querySelectorAll('.d_post_content img[pic_type="0"]'); //.BDE_Image
  488. $(imglist).unbind();
  489. Page._bindOpenImg();
  490.  
  491. }
  492.  
  493. function bind_pager() {
  494. var pager1 = document.querySelectorAll('.pb_list_pager,.jump_btn_bright');
  495. var pager2 = document.querySelectorAll('.jump_input_bright');
  496. for (var i = 0; i < pager1.length; i++) {
  497. pager1[i].addEventListener('click', rebind_pager);
  498. }
  499. for (var i = 0; i < pager2.length; i++) {
  500. pager2[i].addEventListener('keypress', function(e) {
  501. if (e.keyCode == 13) rebind_pager();
  502. });
  503. }
  504. }
  505.  
  506. function rebind_pager() {
  507. if (pager_timer == null) {
  508. pager_timer = setInterval(function() {
  509. if (PageData.pager.cur_page != cur_page) {
  510. clearInterval(pager_timer);
  511. pager_timer = null;
  512. kill_tieba_lazyload();
  513. bind_pager();
  514. cur_page = PageData.pager.cur_page;
  515. next_page = cur_page + 1;
  516. total_page = PageData.pager.total_page;
  517. if (loader == null && setting.thread_preload == 1) preload_listener();
  518. inner_setting();
  519. }
  520. }, 1000)
  521. }
  522. }
  523.  
  524. function inner_setting() {
  525. setting_node = document.createElement('li');
  526. page_node = document.getElementsByClassName('l_posts_num');
  527. setting_node.textContent = setting.thread_preload == 1 ? '关闭自动加载' : '开启自动加载';
  528. setting_node.className = 'tb_preload_setting';
  529. setting_node_2 = setting_node.cloneNode(true);
  530. setting_node.onclick = function(e) {
  531. if (e.ctrlKey) set_function(1, false);
  532. else set_function(1);
  533. };
  534. setting_node_2.onclick = function(e) {
  535. if (e.ctrlKey) set_function(1, false);
  536. else set_function(1);
  537. };
  538. setting_node.oncontextmenu = function() {
  539. call_setting();
  540. return false
  541. };
  542. setting_node_2.oncontextmenu = function() {
  543. call_setting();
  544. return false;
  545. };
  546. if (page_node[0]) page_node[0].insertBefore(setting_node, page_node[0].getElementsByClassName('l_pager')[0].nextSibling);
  547. if (page_node[1]) page_node[1].insertBefore(setting_node_2, page_node[1].getElementsByClassName('l_pager')[0].nextSibling);
  548. }
  549.  
  550. //notification.onclick=function(){set_function(1)};
  551.  
  552. if (PageData.product == 'frs') {
  553. /*if(document.getElementsByClassName('th_footer_l')[0]){
  554. var setting_node=document.createElement('a'),
  555. page_node=document.getElementsByClassName('th_footer_l')[0];
  556. setting_node.textContent=setting.thread_list_lazyload==1?'禁用自动去除首页 lazyload':'开启自动去除首页 lazyload'; // 好长 _(:з」∠)_
  557. setting_node.className='tb_preload_setting';
  558. setting_node.onclick=function(){set_function(2)};
  559. page_node.appendChild(setting_node);
  560. }*/
  561. notification.style.pointerEvents = 'none';
  562. var xhr = new XMLHttpRequest(),
  563. page_url = no_refresh_url_prefix ? ('//tieba.baidu.com' + no_refresh_url_prefix) : location.href.replace(/&?pn=\d+/, ''),
  564. cur_page = location.href.match(/pn=(\d+)/) ? parseInt(location.href.match(/pn=(\d+)/)[1]) : 0,
  565. next_page = cur_page + 50,
  566. total_page = PageData.forum.thread_num - PageData.forum.thread_num % 50 || document.querySelector('a.last').href.match(/pn=(\d+)/)[1],
  567. last_hash_data = '';
  568. if (setting.thread_list_preload == 1) {
  569. kill_tieba_lazyload();
  570. preload_listener();
  571. }
  572. else if (setting.thread_list_lazyload == 1) kill_tieba_lazyload_listener();
  573. }
  574. else if (PageData.product == 'pb' && PageData.pager) {
  575. //Page._bindOpenImg=Page.bindOpenImg;
  576. //Page.bindOpenImg=rebind_image;
  577. kill_tieba_lazyload();
  578. // 以下语句中部分判断代码来自 Chrome 扩展程序 KK 贴吧工具箱 (by kookxiang | https://chrome.google.com/webstore/detail/bcmemjlkdbeephmnklgimfnjlmfhfdgi)
  579. /*cur_page=0,
  580. total_page=0,
  581. page_url='',
  582. see_lz=0,*/
  583. var xhr = new XMLHttpRequest(),
  584. page_url = PageData.thread_url ? '//tieba.baidu.com' + PageData.thread_url : location.href.match(/\/\/tieba\.baidu\.com\/p\/\d+/)[0],
  585. see_lz = window.location.href.match(/see_lz/) ? window.location.href.match(/see_lz=(\d)/)[1] : PageData.special.lz_only,
  586. cur_page = PageData.pager.cur_page,
  587. total_page = PageData.pager.total_page,
  588. next_page = cur_page + 1,
  589. setting_node,
  590. setting_node_2,
  591. page_node;
  592. if (setting.thread_preload == 1) preload_listener();
  593. bind_pager();
  594. inner_setting();
  595. /*for(var i in document.scripts){
  596. if(document.scripts[i].textContent.indexOf('ForumListV3')>=0){
  597. var lzl=document.scripts[i].textContent;
  598. show_lzl(lzl);
  599. break;
  600. }
  601. }*/
  602. }
  603. }
  604.  
  605. typeof GM_registerMenuCommand != 'undefined' && GM_registerMenuCommand('Tieba Preload Setting', call_setting);