Greasy Fork is available in English.

Tieba Preload

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

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