libraryComment

图书馆评论区接入

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/465118/1326626/libraryComment.js

  1. // ==UserScript==
  2. // @name 图书馆评论区
  3. // @namespace https://shequ.codemao.cn/user/438403
  4. // @version 1.1.10
  5. // @description 图书馆评论区接入
  6. // @author 小鱼yuzifu
  7. // @match *://shequ.codemao.cn/*
  8. // @icon https://static.codemao.cn/coco/player/unstable/B1F3qc2Hj.image/svg+xml?hash=FlHXde3J3HLj1PtOWGgeN9fhcba3
  9. // @grant GM_xmlhttpRequest
  10. // @require https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js
  11. // @require https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/sweetalert/2.1.2/sweetalert.min.js
  12. // @license 616 SB License
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17. function timestampToTime(timestamp) {
  18. timestamp = timestamp ? timestamp : null;
  19. let date = new Date(timestamp * 1000);
  20. let Y = date.getFullYear() + "-";
  21. let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
  22. let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  23. let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  24. let m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
  25. let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  26. return Y + M + D + h + m + s;
  27. }
  28. setInterval(() => {
  29. if (document.querySelector("div.index__novel-chapter___Avuy1") && !document.querySelector("#comment_container")) {
  30. $("div.index__novel-chapter___Avuy1").after(`
  31. <div id="comment_container">
  32. <div id="header">
  33. <i class="index__circle-label___1EBso" data-reactid=".0.0.3.3.0.1.1.0.2.0.2.0.0"></i>
  34. <p style="font-size:18px;"><span></span> 评论</p>
  35. </div>
  36. </div>
  37. <div id="comment_sender">
  38. <textarea id="content"></textarea>
  39. <button id="post">发表评论</button>
  40. </div>
  41. <style>
  42. @font-face{
  43. font-family: 'icomoon2';
  44. src: url('https://cdn-community.codemao.cn/community_frontend/fonts/icomoon.e025c6491ae7ebcfe7a1b43fc20cef0e.ttf');
  45. }
  46. #comment_container #header {
  47. display: flex;
  48. align-items: center;
  49. border-bottom: 1px dashed #e6e6e6;
  50. padding: 30px 0 10px;
  51. }
  52. #comment_container div[comment-id] .info img {
  53. width: 50px;
  54. height: 50px;
  55. border-radius: 100%;
  56. margin: 0 8px 0 0;
  57. }
  58. #comment_container div[comment-id] .info {
  59. margin: 0 0 15px 0;
  60. display: flex;
  61. justify-content: space-between;
  62. align-items: center;
  63. }
  64. #comment_container div[comment-id] {
  65. padding: 25px 13px;
  66. border-bottom: 1px dashed #e6e6e6;
  67. }
  68. #page {
  69. display: flex;
  70. align-items: center;
  71. justify-content: center;
  72. }
  73. #page div {
  74. border: 1px solid #e6e6e6;
  75. height: 40px;
  76. padding: 0 15px;
  77. display: flex;
  78. align-items: center;
  79. margin: 20px;
  80. border-radius: 4px;
  81. cursor: pointer;
  82. }
  83. div[disable] {
  84. opacity: 0.5;
  85. }
  86. #comment_container .dock {
  87. display: flex;
  88. justify-content: space-between;
  89. align-items: center;
  90. font-size: 14px;
  91. }
  92. .like {
  93. cursor: pointer;
  94. }
  95. .icon-like_phone {
  96. font-family: icomoon2!important;
  97. }
  98. .icon-like_phone:before {
  99. content: "\\E945";
  100. }
  101. .like[active] {
  102. color: var(--main-color);
  103. }
  104. #comment_sender {
  105. margin: 30px 0 0 0;
  106. }
  107. #comment_sender textarea {
  108. width: 100%;
  109. border: 1px solid #e6e6e6;
  110. resize: none;
  111. min-height: 100px;
  112. border-radius: 4px;
  113. padding: 10px;
  114. overflow: hidden;
  115. }
  116. #comment_sender #post {
  117. float: right;
  118. padding: 10px 15px;
  119. background: var(--second-color);
  120. color: #e6e6e6;
  121. border-radius: 4px;
  122. margin: 10px 0;
  123. cursor: pointer;
  124. }
  125. .del {
  126. font-size: 12px;
  127. cursor: pointer;
  128. }
  129. </style>
  130. `);
  131. var textarea = document.querySelector("#content");
  132.  
  133. textarea.addEventListener("input", function () {
  134. this.style.height = "102px";
  135. this.style.height = this.scrollHeight + "px";
  136. });
  137.  
  138. $("#post").on("click", function () {
  139. console.log(document.querySelector("#content").value);
  140. $.ajax({
  141. type: "POST",
  142. url: `https://api.codemao.cn/api/fanfic/comments/${fanficID}`,
  143. contentType: "application/json;charset=UTF-8",
  144. xhrFields: {
  145. withCredentials: true,
  146. },
  147. data: JSON.stringify({
  148. content: html2Escape(document.querySelector("#content").value),
  149. }),
  150. dataType: "json",
  151. success: function (res) {
  152. if (document.querySelector("#num")) {
  153. upload_comment(parseInt(document.querySelector("#num").innerHTML) - 1);
  154. } else {
  155. upload_comment(0);
  156. }
  157. document.querySelector("#content").value = "";
  158. },
  159. error: function (res) {
  160. console.log(res.responseJSON);
  161. },
  162. });
  163. });
  164.  
  165. function html2Escape(sHtml) {
  166. return sHtml.replace(/[<>&"]/g, function (c) {
  167. return { "<": "&lt;", ">": "&gt;", "&": "&amp;", '"': "&quot;" }[c];
  168. });
  169. }
  170.  
  171. let fanficID = window.location.href.split("/").pop();
  172. let total_num = null;
  173. let total_page = null;
  174.  
  175. function upload_comment(page) {
  176. $.ajax({
  177. type: "GET",
  178. url: `https://api.codemao.cn/api/fanfic/comments/list/${fanficID}?page=${page}&limit=10`,
  179. contentType: "application/json;charset=UTF-8",
  180. xhrFields: {
  181. withCredentials: true,
  182. },
  183. success: function (res) {
  184. console.log(res);
  185. $("#comment_container div[comment-id]").remove();
  186. $("#page").remove();
  187. let a = res.data.commentList;
  188. total_num = res.data["total_num"];
  189. total_page = res.data["total_page"];
  190. $("#comment_container #header p span").text(total_num);
  191. if (total_page > 1) {
  192. $("#comment_container").after(`
  193. <div id="page">
  194. <div id="up" ${page + 1 == 1 ? "disable" : ""}>上一页</div>
  195. <div id="num">${page + 1}</div>
  196. <div id="down" ${page + 1 == total_page ? "disable" : ""}>下一页</div>
  197. </div>
  198. `);
  199. }
  200. $("#up:not(#up[disable])").on("click", function () {
  201. upload_comment(parseInt(document.querySelector("#num").innerHTML) - 2);
  202. });
  203. $("#down:not(#down[disable])").on("click", function () {
  204. upload_comment(parseInt(document.querySelector("#num").innerHTML));
  205. });
  206. $("#num").on("click", function () {
  207. swal(`请输入你要跳转到的页码(最多${total_page}页)`, {
  208. content: "input",
  209. }).then((value) => {
  210. if (value && parseInt(value) && parseInt(value) >= 1 && parseInt(value) <= total_page) {
  211. upload_comment(parseInt(value - 1));
  212. }
  213. });
  214. });
  215.  
  216. for (let i of a) {
  217. let b = document.createElement("div");
  218. b.innerHTML = i.content;
  219. for (let q of b.querySelectorAll("iframe,embed")) {
  220. q.parentNode.removeChild(q);
  221. }
  222. $("#comment_container").append(`
  223. <div comment-id="${i.id}">
  224. <div class="info">
  225. <div>
  226. <img src="${i["user_avatar"]}" />
  227. <a target="_blank" href="https://shequ.codemao.cn/user/${i["user_id"]}"><span>${i.nickname}</span></a>
  228. </div>
  229. ${localStorage.getItem("user_id") == i["user_id"] ? "<div class='del'>删除</div>" : ""}
  230. </div>
  231. <div style="margin: 10px 0;">${b.innerHTML}</div>
  232. <div class="dock">
  233. <span style="opacity: 0.7;">${timestampToTime(i.create_time)}</span>
  234. <div class="like" ${i.praised ? " active" : ""}>
  235. <i class="icon-like_phone"></i>
  236. <span>${i.praise_times}</span>
  237. </div>
  238. </div>
  239. </div>
  240. `);
  241. }
  242. $(".like").on("click", function () {
  243. console.log(this.parentNode.parentNode.getAttribute("comment-id"), this.hasAttribute("active"));
  244. $.ajax({
  245. type: this.hasAttribute("active") ? "DELETE" : "POST",
  246. url: `https://api.codemao.cn/api/fanfic/comments/praise/${this.parentNode.parentNode.getAttribute("comment-id")}`,
  247. contentType: "application/json;charset=UTF-8",
  248. xhrFields: {
  249. withCredentials: true,
  250. },
  251. success: function (res) {
  252. if (document.querySelector("#num")) {
  253. upload_comment(parseInt(document.querySelector("#num").innerHTML) - 1);
  254. } else {
  255. upload_comment(0);
  256. }
  257. },
  258. error: function (res) {
  259. console.log(res.responseJSON);
  260. },
  261. });
  262. });
  263. $(".del").on("click", function () {
  264. console.log(this.parentNode.parentNode.getAttribute("comment-id"));
  265. $.ajax({
  266. type: "DELETE",
  267. url: `https://api.codemao.cn/api/fanfic/comments/${this.parentNode.parentNode.getAttribute("comment-id")}`,
  268. contentType: "application/json;charset=UTF-8",
  269. xhrFields: {
  270. withCredentials: true,
  271. },
  272. success: function (res) {
  273. if (document.querySelector("#num")) {
  274. upload_comment(parseInt(document.querySelector("#num").innerHTML) - 1);
  275. } else {
  276. upload_comment(0);
  277. }
  278. },
  279. error: function (res) {
  280. console.log(res.responseJSON);
  281. },
  282. });
  283. });
  284. },
  285. error: function (res) {
  286. console.log(res.responseJSON);
  287. },
  288. });
  289. }
  290. upload_comment(0);
  291. }
  292. }, 100);
  293. })();

QingJ © 2025

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