巴哈姆特調整檢視回覆留言

點開來才能看到回覆的訊息?不需要,只要將游標移上去就可以看到留言!

  1. // ==UserScript==
  2. // @name 巴哈姆特調整檢視回覆留言
  3. // @version 2024-07-26
  4. // @description 點開來才能看到回覆的訊息?不需要,只要將游標移上去就可以看到留言!
  5. // @author Peugin_
  6. // @namespace https://home.gamer.com.tw/homeindex.php?owner=f31033103
  7. // @match https://forum.gamer.com.tw/C.php?*
  8. // @match https://forum.gamer.com.tw/Co.php?*
  9. // @require https://unpkg.com/popper.js@1
  10. // @require https://unpkg.com/tippy.js@5
  11. // @resource TIPPY_LIGHT_THEME https://unpkg.com/tippy.js@5/themes/light.css
  12. // @license MIT
  13. // @grant GM_getResourceText
  14. // @grant GM_addStyle
  15. // @run-at document-end
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. // Your code here...
  22. //避免重複查詢,浪費資源
  23. const replyMap = new Map();
  24. GM_addStyle(GM_getResourceText("TIPPY_LIGHT_THEME"));
  25.  
  26. console.info('[BahaFastViewReplay] Start');
  27.  
  28. //對初始留言的回應新增監聽器
  29. addReplyMouseoverListener(document);
  30.  
  31. //對整個留言區做監聽,當展開/收縮留言時巴哈會重新建立元素,這時候我們也需要重新建立相對應的監聽器
  32. document.querySelectorAll("[id^=Commendlist]").forEach(commentList => {
  33. let observer = new MutationObserver(mutations => {
  34. addReplyMouseoverListener(mutations[0].target);
  35. }).observe(commentList, {childList: true});
  36. });
  37.  
  38. console.info('[BahaFastViewReplay] End');
  39.  
  40.  
  41.  
  42. //對有回覆的留言新增監聽器
  43. function addReplyMouseoverListener(node) {
  44. let commentEles = new Set();
  45.  
  46. node.querySelectorAll('.reply-content a[href^="javascript:Forum.C.openCommentDialog"]').forEach(replyEle => {
  47. let commentEle = replyEle.closest(".c-reply__item");
  48. if(!commentEles.has(commentEle)) {
  49. addReplyTooltip(commentEle);
  50. }
  51. });
  52. };
  53.  
  54. //對該回覆新增 tooltip
  55. function addReplyTooltip(commentEle) {
  56. commentEle.querySelectorAll('a[href^="javascript:Forum.C.openCommentDialog"]').forEach(replyEle => {
  57. let bsn = replyEle.getAttribute("href").match(/\d+/g)[0];
  58. let snB = replyEle.getAttribute("href").match(/\d+/g)[1];
  59. let snC = replyEle.getAttribute("href").match(/\d+/g)[2];
  60.  
  61. getReplyApiEle(bsn,snB,snC).then(replyApiEle => {
  62. replyEle.setAttribute("data-placement", "top");
  63. replyEle.setAttribute("data-toggle", "tooltip");
  64. replyEle.setAttribute("data-tooltipped", "");
  65. replyEle.setAttribute("data-tippy-content", `<div class="dialogify" style="display: contents; text-align:left;"><div class="dialogify__body">${replyApiEle}</div></div>`);
  66.  
  67. replyEle.addEventListener('mouseover', () => showReplyTooltip(replyEle));
  68. });
  69. });
  70. }
  71.  
  72. //顯示回覆的 tooltip
  73. function showReplyTooltip(replyEle) {
  74. tippy(replyEle, {
  75. maxWidth: 560,
  76. interactive: true,
  77. appendTo: document.body,
  78. theme: document.querySelector("html").dataset.theme === undefined ? 'light' : 'dark',
  79. onShow(instance) {
  80. instance.setContent(instance.reference.dataset.tippyContent);
  81. Forum.C.commentFormatter(instance.popper.querySelector("span"));
  82. }});
  83. }
  84.  
  85. //回覆 API,回傳組裝好的 html 元素
  86. function getReplyApiEle(bsn,snB,snC) {
  87. return new Promise((resolve,reject) => {
  88. if(!replyMap.has(snC)) {
  89. return fetch(`https://api.gamer.com.tw/forum/v1/comment_get.php?bsn=${bsn}&snB=${snB}&snC=${snC}&type=pc`, {
  90. credentials: "include"
  91. }).then(res => {
  92. return res.json().then(json => {
  93. replyMap.set(snC, json.data.comment.html);
  94. return resolve(replyMap.get(snC));
  95. });
  96. });
  97. }
  98.  
  99. return resolve(replyMap.get(snC));
  100. });
  101. }
  102. })();

QingJ © 2025

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