看趣闻快捷查看引用楼层内容

在看趣闻中方便快捷地查看引用楼层的内容

  1. // ==UserScript==
  2. // @name 看趣闻快捷查看引用楼层内容
  3. // @namespace https://gf.qytechs.cn/users/4514
  4. // @author 喵拉布丁
  5. // @homepage https://gf.qytechs.cn/scripts/16794
  6. // @description 在看趣闻中方便快捷地查看引用楼层的内容
  7. // @include http://kanquwen.com/*/*
  8. // @include http://m.kanquwen.com/*/*
  9. // @version 1.0
  10. // @grant none
  11. // @run-at document-end
  12. // @license MIT
  13. // ==/UserScript==
  14. var Kqw = {
  15. // 网站类型,0:桌面版;1:移动版(无需在此修改)
  16. type: 0,
  17. // 本页楼层列表
  18. floorList: {},
  19.  
  20. /**
  21. * 获取本页楼层列表
  22. * @param {string} content 正文内容的HTML代码
  23. */
  24. getFloorList: function (content) {
  25. if (!content) return;
  26. var floorMatches = content.match(/<p.+?\d+:.*?\d+\/\d+\/\d+(.|\r|\n)+?(?=<p.+?\d+:.*?\d+\/\d+\/\d+|<p class="article_page_info">)/ig);
  27. if (!floorMatches) return;
  28.  
  29. Kqw.floorList = {};
  30. for (var i in floorMatches) {
  31. var textMatches = /^<p.+?(\d+):(.|\r|\n)+/i.exec(floorMatches[i]);
  32. if (!textMatches) return;
  33. var floor = parseInt(textMatches[1]);
  34. if (typeof Kqw.floorList[floor] === 'undefined') Kqw.floorList[floor] = textMatches[0];
  35. else Kqw.floorList[floor] += '<hr />' + textMatches[0];
  36. }
  37. //console.log(Kqw.floorList);
  38. },
  39.  
  40. /**
  41. * 添加CSS
  42. */
  43. appendCss: function () {
  44. $('head').append(
  45. '<style>' +
  46. ' a.pd_anchor:hover { text-decoration: underline; }' +
  47. ' .pd_panel {' +
  48. ' position: absolute; overflow-y: auto; background-color: #FCFCFC; border: 1px solid #BBB; opacity: 0.95; z-index: 100;' +
  49. ' padding: 0 5px; min-height: 48px;' +
  50. '} ' +
  51. '</style>'
  52. );
  53. },
  54.  
  55. /**
  56. * 处理本页的引用楼层链接
  57. * @param {jQuery} $content 正文内容的jQuery节点
  58. * @param {string} content 正文内容的HTML代码
  59. */
  60. handleAnchorLinks: function ($content, content) {
  61. var linkMatches = content.match(/&gt;&gt;\d+/ig);
  62. var replacedFloorList = {};
  63. for (var i in linkMatches) {
  64. var floor = parseInt(/\d+/.exec(linkMatches[i])[0]);
  65. if (floor === 1) continue;
  66. if (typeof Kqw.floorList[floor] !== 'undefined' && typeof replacedFloorList[floor] === 'undefined') {
  67. content = content.replace(new RegExp('&gt;&gt;' + floor + '(?!\\d)', 'ig'),
  68. '<a class="pd_anchor" href="#" data-floor="{0}">&gt;&gt;{0}</a>'.replace(/\{0\}/g, floor)
  69. );
  70. replacedFloorList[floor] = true;
  71. }
  72. }
  73. $content.html(content);
  74. },
  75.  
  76. /**
  77. * 处理引用楼层的文本内容
  78. * @param {string} content 原文本内容
  79. * @returns {string} content 处理后的文本内容
  80. */
  81. handleFloorContent: function (content) {
  82. return content.replace(/<p class="article_page[^"]+">.+?<\/p>/ig, '')
  83. .replace(/<p( style="[^"]+")?>&nbsp;<\/p>\n((?=<hr \/>)|$)/ig, '');
  84. },
  85.  
  86. /**
  87. * 绑定引用楼层链接点击事件
  88. */
  89. bindAnchorLinkClick: function () {
  90. $(document).click(function (e) {
  91. var $target = $(e.target);
  92. if ($target.is('.pd_anchor')) {
  93. e.preventDefault();
  94. $('.pd_panel').remove();
  95. var $this = $(e.target);
  96. var floor = parseInt($this.data('floor'));
  97. if (isNaN(floor)) return;
  98.  
  99. if (typeof Kqw.floorList[floor] === 'undefined') return;
  100. var $panel = $('<div class="pd_panel">{0}</div>'.replace('{0}', Kqw.handleFloorContent(Kqw.floorList[floor]))).appendTo('body');
  101.  
  102. var offsetTop = $this.offset().top;
  103. var scrollTop = $(window).scrollTop();
  104. var windowHeight = $(window).height();
  105. var min = windowHeight / 4, max = windowHeight / 2;
  106. var panelMaxHeight = 0;
  107. var isUnder = offsetTop - scrollTop < min + 18;
  108. if (isUnder)
  109. panelMaxHeight = scrollTop + windowHeight - offsetTop - (Kqw.type === 1 ? 18 : 7);
  110. else
  111. panelMaxHeight = offsetTop - scrollTop - (Kqw.type === 1 ? 18 : 7);
  112. if (panelMaxHeight > max) panelMaxHeight = max;
  113.  
  114. var $line = $this.closest('p');
  115. if (!$line.length) $line = $this.closest('div');
  116. if (Kqw.type === 1) {
  117. $panel.css('padding-top', '5px')
  118. .css('padding-bottom', '5px')
  119. .css('max-width', $this.closest('#post_content').width() - 10)
  120. .css('max-height', panelMaxHeight)
  121. .css('left', $line.offset().left);
  122. }
  123. else {
  124. $panel.css('max-width', $line.width() - 30)
  125. .css('max-height', panelMaxHeight)
  126. .css('left', $line.offset().left + 30)
  127. }
  128. $panel.css('top', isUnder ? offsetTop + $this.height() + 3 : offsetTop - $panel.outerHeight() - 3);
  129. }
  130. else if (!($target.is('.pd_panel') || $target.closest('.pd_panel').length > 0)) {
  131. $('.pd_panel').remove();
  132. }
  133. });
  134. },
  135.  
  136. /**
  137. * 初始化
  138. */
  139. init: function () {
  140. if (typeof jQuery === 'undefined') return;
  141. if (!/\/\d+\/(\?|#|$)/.test(location.href)) return;
  142.  
  143. var $content = null;
  144. if (location.hostname.indexOf('m.') === 0) {
  145. Kqw.type = 1;
  146. $content = $('#post_content');
  147. }
  148. else {
  149. Kqw.type = 0;
  150. $content = $('.post .single_text');
  151. }
  152. if (!$content.length) return;
  153.  
  154. var content = $content.html();
  155. Kqw.getFloorList(content);
  156. if (!Kqw.floorList || $.isEmptyObject(Kqw.floorList)) return;
  157.  
  158. Kqw.appendCss();
  159. Kqw.handleAnchorLinks($content, content);
  160. Kqw.bindAnchorLinkClick();
  161. }
  162. };
  163.  
  164. Kqw.init();

QingJ © 2025

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