kf复数引用脚本

对kf的帖子进行复数引用

  1. // ==UserScript==
  2. // @name kf复数引用脚本
  3. // @namespace https://gf.qytechs.cn/users/14059
  4. // @description 对kf的帖子进行复数引用
  5. // @include https://bbs.ikfol.com/read.php*
  6. // @include https://kf.miaola.info/read.php*
  7. // @author setycyas
  8. // @icon https://gitee.com/miaolapd/KF_Online_Assistant/raw/master/icon.png
  9. // @version 0.05
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @run-at document-end
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. (function(){
  17. /* 脚本正式开始 */
  18.  
  19. 'use strict';
  20. console.log("KF复数引用 is running!");
  21.  
  22. /****************************************
  23. ######## 油猴API以及常用API示例 #########
  24. ****************************************/
  25.  
  26. //数据读写api
  27. //GM_setValue(key,value);
  28. //GM_getValue(key);
  29.  
  30. /****************************************
  31. ######## version 20200302 0.05 ###########
  32. ######## 脚本正式开始 ###################
  33. ****************************************/
  34.  
  35. /* Global Variable & Const */
  36. const QUOTE_GM = 'quote_gm'; // 在油猴中记录引用资料的数据key
  37. const LAST_TID_GM = 'last_tid_gm'; // 在油猴中记录最近引用的帖子的tid的数据key
  38. var G_quotes = {}; // 引用字典,每一项的key为楼层(文本格式),value为二元组[id, text]
  39. var G_datas = {}; // 帖子内容表,每一项的key为楼层(文本格式),value为二元组[id, text]
  40. /* Functions */
  41. // 从一个url中获取帖子的tid,返回的是一个字符串而不是数字,获取失败返回字符串'0'
  42. function getTidFromUrl(url){
  43. let reg = /tid=(\d+)/;
  44. let matchs = url.match(reg);
  45. if(matchs)
  46. return matchs[1];
  47. else
  48. return '0';
  49. }
  50.  
  51. // 从一个url获取帖子的页数,返回数字
  52. function getPageFromUrl(url){
  53. let reg = /&page=(\d+)/;
  54. let matchs = url.match(reg);
  55. if(matchs)
  56. return parseInt(matchs[1]);
  57. else
  58. return 1;
  59. }
  60. // 获取本页的帖子内容,制作公有变量G_datas
  61. function getG_datas(){
  62. let startFloor = (getPageFromUrl(window.location.href)-1)*10; // 开始楼层
  63. let $idA = $('.readidmsbottom a'); // 所有显示楼主名称的a标签,其innerText就是楼主名字
  64. let floorLength = $idA.length; // 当页总楼层
  65. let $textDiv = $('.readtext td > div'); // 所有楼层的文本内容
  66. for(let i = 0;i < floorLength;i++){
  67. G_datas[(i+startFloor)+''] = [$idA[i].innerText, $textDiv[i].innerText.replace(/[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, '')];
  68. //G_datas[(i+startFloor)+''] = [$idA[i].innerText, $($textDiv[i]).text()];
  69. // 把关键词,以往帖子的文本删除
  70. let text = $textDiv[i].innerText.replace(/[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, '');
  71. let fieldsets = $textDiv[i].getElementsByTagName('fieldset');
  72. for(let j = 0;j < fieldsets.length;j++){
  73. text = text.replace(fieldsets[j].innerText, '');
  74. }
  75. G_datas[(i+startFloor)+''] = [$idA[i].innerText, text];
  76. }
  77. }
  78. // 获取已记录的引用内容,若浏览帖子变更,则清空引用内容
  79. function getG_quotes(){
  80. let tid = getTidFromUrl(window.location.href); // 当前tid
  81. let lastTid = GM_getValue(LAST_TID_GM, '-1'); // 上次浏览的tid
  82. if(tid != lastTid){
  83. // 浏览tid改变,则清空引用
  84. GM_setValue(QUOTE_GM, '{}');
  85. G_quotes = {};
  86. }else{
  87. // 浏览tid不变,则获取引用
  88. G_quotes = JSON.parse(GM_getValue(QUOTE_GM, '{}'));
  89. }
  90. }
  91. // 增加一条引用,参数为引用楼层floor
  92. function addQuote(floor){
  93. if(floor in G_datas){
  94. G_quotes[floor] = G_datas[floor];
  95. GM_setValue(QUOTE_GM, JSON.stringify(G_quotes));
  96. }
  97. }
  98. // 使用所有引用
  99. function useQuote(){
  100. // 获取所有id,文本
  101. let idList = [];
  102. let textList = [];
  103. for (let floor in G_quotes){
  104. let id = G_quotes[floor][0];
  105. let text = G_quotes[floor][1];
  106. idList.push(id);
  107. text = ['[quote]引用', floor, '楼 ', id, '\n', text.trim(),'\n','[/quote]'].join('');
  108. textList.push(text);
  109. }
  110. // 写入所有id,文本
  111. let newIdList = []; // 排除重复用
  112. for(let i = 0;i < idList.length;i++){
  113. if($.inArray(idList[i], newIdList) < 0){
  114. newIdList.push(idList[i]);
  115. }
  116. }
  117. $('input.input').val(newIdList.join(','));
  118. //$('div.dcol textarea').val(textList.join('\n\n')).focus();
  119. $('div.drow textarea').val(textList.join('\n\n')).focus();
  120. }
  121. // 改变所有'引用'链接的点击行为
  122. function changeQuote(){
  123. // 查找相对复杂,先找到所有符合的a,然后匹配引用楼层
  124. //let $pagesA = $('div.readlou ul.pages>li>a');
  125. let $pagesA = $('a.readcza');
  126. let reg = /post.php\?action=quote.*article=(\d+)/;
  127. for(let i = 0;i < $pagesA.length;i++){
  128. let a = $pagesA[i];
  129. let matchs = a.href.match(reg);
  130. if(matchs){
  131. let floor = matchs[1];
  132. a.href = 'javascript:';
  133. $(a).click(function(){
  134. addQuote(floor);
  135. useQuote();
  136. });
  137. }
  138. }
  139. }
  140. // 测试用
  141. function test(){
  142. console.log('tid = ',getTidFromUrl(window.location.href));
  143. console.log('page = ',getPageFromUrl(window.location.href));
  144. console.log('G_quotes = ',JSON.stringify(G_quotes));
  145. }
  146. /* Main Script */
  147. getG_datas(); // 获取帖子内容
  148. getG_quotes(); // 获取已记录的引用,没有则是空
  149. changeQuote(); // 改变'引用'链接的行为
  150. GM_setValue(LAST_TID_GM, getTidFromUrl(window.location.href)); // 记录最后访问的tid
  151. test(); // 测试显示用,可不要
  152. //2020-2-17自制的css,临时加上
  153. $('.readidms').css({"margin-top":"15px","margin-left":"-30px"});
  154. $('.readtext').css({"padding-left":"190px","width":"80%","float":"left"});
  155.  
  156. /* 脚本结束 */
  157. })();

QingJ © 2025

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