v站增强

v2ex站点增强插件.增强评论查看模式。

目前为 2021-04-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name v站增强
  3. // @namespace https://www.yffjglcms.com/
  4. // @version 0.1.1.20210402
  5. // @description v2ex站点增强插件.增强评论查看模式。
  6. // @author yffjglcms
  7. // @match https://v2ex.com/t/*
  8. // @match https://www.v2ex.com/t/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. // Your code here...
  16.  
  17. let config = {
  18. replyMode: -1 //1 对应楼层;-1对应用户
  19. };
  20. // 获取数据
  21. let data = [];
  22.  
  23. // 作者数据map
  24. let map = new Map();
  25.  
  26. // 楼层数据map
  27. let floorMap = new Map();
  28.  
  29. let boxName = "v2Box";
  30. let boxNameSelector = ".v2Box";
  31. let v2BtnName = "v2Btn";
  32. let v2BtnSelector = ".v2Btn";
  33. let v2Style = `<style>
  34. ${v2BtnSelector}{
  35. border: none; background-color: white; color: grey; margin: 0px 0 5px 0; padding-bottom: 5px; width: 100%; border-bottom: 1px solid #c5c5c5;
  36. }
  37. ${v2BtnSelector}:focus{
  38. outline: none;
  39. }
  40. ${v2BtnSelector}:hover{
  41. cursor: pointer;
  42. }
  43.  
  44. ${boxNameSelector}{
  45. position: absolute;width: 270px; padding: 5px 8px;overflow-y:scroll; max-height: 80%;
  46. }
  47.  
  48. </style>`;
  49. let msgBox = `<div class='box ${boxName}'></div>`;
  50.  
  51. loadV2Style();
  52. fetchData();
  53.  
  54. function loadV2Style() {
  55. $("head").append(v2Style);
  56. }
  57.  
  58. function fetchData() {
  59. $(".cell strong a").each((idx, e) => {
  60. let _this = $(e);
  61. let _cell = _this.parents(".cell");
  62.  
  63. let author = _this.html();
  64. let no = _cell.find(".no").html();
  65.  
  66. // console.log(author);
  67. // console.log(no);
  68.  
  69. if (!map.has(author)) {
  70. map.set(author, []);
  71. }
  72.  
  73. floorMap.set(no, _cell.html());
  74.  
  75. map.get(author).push(_cell.html());
  76. });
  77.  
  78. // console.log(map);
  79.  
  80. // console.log(floorMap);
  81. }
  82.  
  83. function getSwitchTable() {
  84. return `<button class='${v2BtnName}'>${
  85. config.replyMode > 0 ? "本页对应楼层评论" : "本页所有评论"
  86. }</button>`;
  87. }
  88.  
  89. // 绑定事件
  90. $(".reply_content a").hover(
  91. (e) => {
  92. let _this = $(e.currentTarget);
  93. let parent = _this.parents(".cell");
  94. let author = _this.html();
  95.  
  96. if ($(boxNameSelector).length > 0) {
  97. $(boxNameSelector).remove();
  98. }
  99.  
  100. // 包装一下 nodeType 为 Text 的文本,方便选择
  101. $(parent)
  102. .find(".reply_content")
  103. .contents()
  104. .filter(function () {
  105. return this.nodeType == 3;
  106. })
  107. .wrap("<span></span>");
  108. let next = $(_this).next().html();
  109. let floor = next?.match(/(#)(\d+)/)?.[2];
  110. // console.log(next);
  111. // console.log(floor);
  112.  
  113. let _content =
  114. config.replyMode > 0 && floor ? floorMap.get(floor) : map.get(author);
  115.  
  116. if (!_content) return;
  117.  
  118. $(parent).before(msgBox);
  119.  
  120. $(boxNameSelector).css("left", Rightbar.offsetLeft);
  121. $(boxNameSelector).html(_content);
  122. $(boxNameSelector).prepend(getSwitchTable());
  123. $(v2BtnSelector).data("floor", floor);
  124. $(v2BtnSelector).data("author", author);
  125. },
  126. () => {
  127. // console.log("2-1");
  128. // $(boxNameSelector).remove();
  129. // console.log("2-2");
  130. }
  131. );
  132.  
  133. // 绑定切换事件
  134. $("#Main").on("click", ".v2Btn", function () {
  135. config.replyMode = -config.replyMode;
  136. let author = $(this).data("author");
  137. let floor = $(this).data("floor");
  138. let _content =
  139. config.replyMode > 0 && floor ? floorMap.get(floor) : map.get(author);
  140.  
  141. if (!_content) return;
  142. $(boxNameSelector).html(_content);
  143. $(boxNameSelector).prepend(getSwitchTable());
  144. $(v2BtnSelector).data("floor", floor);
  145. $(v2BtnSelector).data("author", author);
  146. });
  147. })();

QingJ © 2025

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