齐乐 - 展现所有内容

在齐乐(原蒸汽动力)攻略页面一次性展开所有内容。

  1. // ==UserScript==
  2. // @name 齐乐 - 展现所有内容
  3. // @namespace moe.jixun.steamcn
  4. // @version 1.0.1
  5. // @description 在齐乐(原蒸汽动力)攻略页面一次性展开所有内容。
  6. // @author Jixun Moe<https://jixun.moe/>
  7. // @include https://keylol.com/t*
  8. // @include https://keylol.com/forum.php?mod=viewthread&*
  9. // @grant none
  10. // @run-at document-start
  11. // @homepage https://jixun.moe/
  12. // @connect self
  13. // @nocompat Chrome
  14. // @require https://gf.qytechs.cn/scripts/20844/code/console-log.js
  15. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js
  16. // ==/UserScript==
  17.  
  18. /* jshint esnext:true, jquery: true */
  19.  
  20. $(($) => {
  21. 'use strict';
  22. $.noConflict();
  23. var logger = new Logger();
  24.  
  25. //// 填充元素
  26. var _dummy = $('<div>');
  27.  
  28. //// 帖子数据
  29. var postContainer = $('#postlist>div').first();
  30. var tid = window.tid;
  31. var pid = getFirstPid();
  32.  
  33. //// 检测帖子目录是否存在
  34. var threadIndex = postContainer.find('.tindex');
  35. if (threadIndex.length !== 1)
  36. return ;
  37.  
  38. //// 开始获取数据
  39. var threadTitles = threadIndex.find('li');
  40. var threadOffset = 0;
  41. if (threadTitles.first().attr('onclick').indexOf('cp=1') == -1) {
  42. threadOffset = -1;
  43. }
  44. var maxPage = threadTitles.length;
  45.  
  46. //// 隐藏之前的帖子数据
  47. var postCell = postContainer.find('.t_f');
  48. var postTable = postCell.closest('table');
  49. var cacheText = postCell.find('.pstatus').text() || '_raw';
  50.  
  51. getPageContent(1);
  52.  
  53. //// 辅助函数
  54. function getFirstPid () {
  55. return parseInt(postContainer.attr('id').replace('post_', ''));
  56. }
  57.  
  58. function getTitleOfPage (page) {
  59. var index = page - 1 + threadOffset;
  60. if (index < 0) {
  61. return _dummy;
  62. }
  63. return threadTitles.eq(index);
  64. }
  65.  
  66. function getPageName (page) {
  67. return getTitleOfPage(page).text();
  68. }
  69.  
  70. function updatePageIndex (page) {
  71. var title = getTitleOfPage(page);
  72. title.prop('onclick', null);
  73.  
  74. var link = $('<a>').text(title.text());
  75. link.attr('href', `${location.pathname}${location.search}#page_${page}`);
  76.  
  77. title.html(link);
  78. }
  79.  
  80. function tryLoadCache (page) {
  81. // cacheText
  82. var cache = localStorage[`_cache_${tid}_${pid}_${page}`];
  83. if (!cache) return false;
  84.  
  85. var data;
  86. try {
  87. data = JSON.parse(cache);
  88. } catch (err) {
  89. return false;
  90. }
  91.  
  92. if (data.cache == cacheText) {
  93. showPage(page, data.html);
  94. return true;
  95. }
  96.  
  97. return false;
  98. }
  99.  
  100. function saveCache (page, html) {
  101. var data = {
  102. cache: cacheText,
  103. html: html.wrapAll('<div>').parent().html()
  104. };
  105. localStorage[`_cache_${tid}_${pid}_${page}`] = JSON.stringify(data);
  106. }
  107.  
  108. function showPage (page, html) {
  109. var pageName = getPageName(page);
  110. var threadContent = $(html);
  111.  
  112. var header = $('<h2>').text(pageName);
  113. threadContent.prepend(header);
  114. threadContent.append(document.createElement('hr'));
  115.  
  116. // threadContent 是 td, 把父 tr 插进去。
  117. postTable.append(threadContent.wrap('<tr>').parent());
  118. header.attr('id', `page_${page}`);
  119. updatePageIndex(page);
  120.  
  121. // 藏起来
  122. postCell.hide();
  123. }
  124.  
  125. function nextPage (page) {
  126. if (page >= maxPage) {
  127. logger.show('全部数据载入完毕!');
  128. } else {
  129. getPageContent(page + 1);
  130. }
  131. }
  132.  
  133. function getPageContent (page) {
  134. if (tryLoadCache (page)) {
  135. nextPage (page);
  136. return ;
  137. }
  138.  
  139. var pageName = getPageName(page);
  140. var logLoadingPage = logger.stick(`正在读取第 ${page} - ${pageName} ...`);
  141. var url = `/forum.php?mod=viewthread&threadindex=yes&tid=${tid}&viewpid=${pid}&cp=${page}`;
  142.  
  143. $.ajax({
  144. url: url,
  145. method: 'GET',
  146. dataType: 'xml'
  147. }).done((doc) => {
  148. var cell = $(doc.getElementsByTagName('root')[0].textContent).find('.t_f');
  149. saveCache(page, cell);
  150. showPage(page, cell);
  151. }).always(() => {
  152. logger.remove(logLoadingPage);
  153. nextPage(page);
  154. });
  155. }
  156. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址