JustMoeComments

萌娘百科看Lih的镜像站的评论,同时集成了作品讨论的评论

目前为 2023-09-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name JustMoeComments
  3. // @namespace https://github.com/gui-ying233/JustMoeComments
  4. // @version 2.9.3
  5. // @description 萌娘百科看Lih的镜像站的评论,同时集成了作品讨论的评论
  6. // @author 鬼影233
  7. // @license MIT
  8. // @match zh.moegirl.org.cn/*
  9. // @match mzh.moegirl.org.cn/*
  10. // @match mobile.moegirl.org.cn/*
  11. // @icon https://moegirl.uk/images/a/a2/%E7%B2%89%E8%89%B2%E5%A4%A7%E7%8C%9B%E5%AD%97.png
  12. // @supportURL https://github.com/gui-ying233/JustMoeComments/issues
  13. // ==/UserScript==
  14.  
  15. (async function () {
  16. "use strict";
  17. await new Promise(resolve => {
  18. const intervId = setInterval(() => {
  19. if (typeof mw !== "undefined" && typeof wgULS !== "undefined") {
  20. clearInterval(intervId);
  21. resolve();
  22. }
  23. }, 50);
  24. });
  25. if (
  26. mw.config.get("wgAction") === "view" &&
  27. [0, 2, 12, 274].includes(mw.config.get("wgNamespaceNumber"))
  28. ) {
  29. const api = new mw.Api();
  30. function generatePost(post) {
  31. let timestamp;
  32. if (typeof post.timestamp === "number") {
  33. const diff = Date.now() - post.timestamp * 1000;
  34. if (diff > 0 && diff < 86400000) {
  35. timestamp = moment(post.timestamp * 1000)
  36. .locale(mw.config.get("wgUserLanguage"))
  37. .fromNow();
  38. } else {
  39. timestamp = moment(post.timestamp * 1000)
  40. .locale(mw.config.get("wgUserLanguage"))
  41. .format("LL, HH:mm:ss");
  42. }
  43. } else {
  44. timestamp = post.timestamp;
  45. }
  46. const postDiv = document.createElement("div");
  47. postDiv.className = "comment-thread";
  48. postDiv.innerHTML = `<div class="comment-post"><div class="comment-avatar"><a href="//moegirl.uk/U:${
  49. post.username
  50. }"><img src="//moegirl.uk/extensions/Avatar/avatar.php?user=${
  51. post.username
  52. }" decofing="async" loading="lazy" fetchpriority="low"></a></div><div class="comment-body"><div class="comment-user"><a href="//moegirl.uk/U:${
  53. post.username
  54. }">${post.username}</a></div><div class="comment-text">${
  55. post.text
  56. }</div><div class="comment-footer"><span class="comment-time">${timestamp}</span>${
  57. post.like
  58. ? `<span class="comment-like">赞 ${post.like}</span>`
  59. : ""
  60. }</div></div></div>`;
  61. [...postDiv.querySelectorAll("img[src^='/images/']")].forEach(i => {
  62. i.src = `//img.moegirl.org.cn/common/${new URL(
  63. i.src
  64. ).pathname.slice(8)}`;
  65. i.srcset = i.srcset.replaceAll(
  66. "/images/",
  67. "//img.moegirl.org.cn/common/"
  68. );
  69. });
  70. [
  71. ...postDiv.querySelectorAll(
  72. 'img[src*="thumb"][src$=".svg.png"], img[src*="thumb"][data-lazy-src$=".svg.png"], img[src*="thumb"][src$=".gif"], img[data-lazy-src*="thumb"][data-lazy-src$=".gif"]'
  73. ),
  74. ].forEach(i => {
  75. try {
  76. const _i = i.cloneNode();
  77. if (
  78. new mw.Uri(_i.src || _i.dataset.lazySrc).host ===
  79. "img.moegirl.org.cn"
  80. ) {
  81. _i.src = _i.src
  82. .replace("/thumb/", "/")
  83. .replace(/\.svg\/[^/]+\.svg\.png$/, ".svg")
  84. .replace(/\.gif\/[^/]+\.gif$/, ".gif");
  85. _i.removeAttribute("srcset");
  86. _i.removeAttribute("data-lazy-src");
  87. _i.removeAttribute("data-lazy-srcset");
  88. _i.removeAttribute("data-lazy-state");
  89. _i.classList.remove("lazyload");
  90. _i.onload = function () {
  91. i.replaceWith(_i);
  92. };
  93. }
  94. } catch {}
  95. });
  96. [
  97. ...postDiv.querySelectorAll(
  98. "a.extiw[title^='moe:'], a.extiw[title^='zhmoe:']"
  99. ),
  100. ].forEach(a => {
  101. a.classList.remove("extiw");
  102. api.get({
  103. action: "query",
  104. format: "json",
  105. titles: decodeURI(a.pathname.slice(1)),
  106. utf8: 1,
  107. formatversion: 2,
  108. }).done(b => {
  109. if (b.query.pages[0].missing) {
  110. a.href = `/index.php?title=${a.pathname.slice(
  111. 1
  112. )}&action=edit&redlink=1`;
  113. a.title += wgULS(" (页面不存在)", "(頁面不存在)");
  114. a.classList += "new";
  115. } else if (
  116. b.query.pages[0].pageid === mw.config.get("wgArticleId")
  117. ) {
  118. a.href = "";
  119. a.title = "";
  120. a.classList += "mw-selflink selflink";
  121. } else {
  122. a.href = a.pathname;
  123. a.title = a.title.replace(/moe:|zhmoe:/, "");
  124. }
  125. });
  126. });
  127. [...postDiv.getElementsByTagName("script")].forEach(s => {
  128. const _s = document.createElement("script");
  129. _s.innerHTML = s.innerHTML;
  130. [...s.attributes].forEach(a => {
  131. _s.setAttribute(a.name, a.value);
  132. });
  133. s.parentNode.replaceChild(_s, s);
  134. });
  135. return postDiv;
  136. }
  137. mw.loader.using(["moment"]).done(() => {
  138. fetch(
  139. `https://moegirl.uk/api.php?${new URLSearchParams({
  140. action: "query",
  141. format: "json",
  142. prop: "pageprops",
  143. titles: mw.config.get("wgPageName"),
  144. utf8: 1,
  145. formatversion: 2,
  146. origin: "*",
  147. })}`
  148. )
  149. .then(a => a.json())
  150. .then(a => {
  151. const commentCSS = document.createElement("style");
  152. commentCSS.innerHTML =
  153. "#flowthread{clear:both;padding:1.5em}body.skin-moeskin #flowthread{background-color:var(--theme-background-color)}.comment-container-top:not(:empty){border:1px #ccc solid;border-radius:5px}body.skin-vector .comment-container-top{background-color:rgb(191 234 181 / 20%)}body.skin-moeskin .comment-container-top{background-color:var(--theme-card-background-color)}.comment-container-top>div:first-child{height:24px;line-height:24px;text-indent:1em;font-size:small;border-radius:5px 5px 0 0;font-weight:bold}body.skin-vector .comment-container-top>div:first-child{background-color:rgb(18 152 34 / 47%);color:#fff}body.skin-moeskin .comment-container-top>div:first-child{background-color:var(--theme-accent-color);color:var(--theme-accent-link-color)}.comment-thread{border-top:1px solid rgba(0,0,0,0.13)}.comment-thread .comment-thread{margin-left:40px}.comment-post{padding:10px}.comment-avatar{float:left}.comment-avatar img{width:50px;height:50px}.comment-body{padding-left:60px}.comment-thread>div:not(:first-of-type) .comment-avatar img{width:30px;height:30px}.comment-thread>div:not(:first-of-type) .comment-body{padding-left:40px}.comment-user,.comment-user a{color:#777;font-size:13px;margin-right:8px}.post-content .comment-text{position:static}.comment-text{font-size:13px;line-height:1.5em;margin:.5em 0;word-wrap:break-word;position:relative;overflow:hidden;min-height:1em}.comment-footer{font-size:12px;margin-right:8px;color:#999}.comment-like{margin-left:5px}";
  154. document.head.appendChild(commentCSS);
  155. const containerTop = document.createElement("div");
  156. containerTop.className = "comment-container-top";
  157. const container = document.createElement("div");
  158. container.className = "comment-container";
  159. const postContent = document.createElement("div");
  160. postContent.id = "flowthread";
  161. postContent.className = "post-content";
  162. postContent.appendChild(containerTop);
  163. postContent.appendChild(container);
  164. switch (mw.config.get("skin")) {
  165. case "vector":
  166. document
  167. .getElementById("footer")
  168. .appendChild(postContent);
  169. break;
  170. case "moeskin":
  171. default:
  172. document
  173. .getElementById("moe-global-footer")
  174. .appendChild(postContent);
  175. break;
  176. }
  177. (function getComment(offset) {
  178. fetch(
  179. `https://moegirl.uk/api.php?${new URLSearchParams({
  180. action: "flowthread",
  181. format: "json",
  182. type: "list",
  183. pageid: a.query.pages[0].pageid,
  184. limit: 15,
  185. offset,
  186. utf8: 1,
  187. formatversion: 2,
  188. origin: "*",
  189. })}`
  190. )
  191. .then(b => b.json())
  192. .then(b => {
  193. if (b.flowthread.popular.length) {
  194. document.body.getElementsByClassName(
  195. "comment-container-top"
  196. )[0].innerHTML = "<div>热门评论</div>";
  197. for (const post of b.flowthread.popular) {
  198. const _post = generatePost(post);
  199. _post.classList.add("comment-popular");
  200. document.body
  201. .getElementsByClassName(
  202. "comment-container-top"
  203. )[0]
  204. .appendChild(_post);
  205. }
  206. }
  207. if (mw.config.get("skin") === "moeskin") {
  208. setTimeout(() => {
  209. if (
  210. [
  211. (mw.config.get("wgPageName"),
  212. mw.config
  213. .get("wgPageName")
  214. .replace(
  215. /^(.+)\(.+?\)$/,
  216. "$1"
  217. ),
  218. document.getElementById(
  219. "firstHeading"
  220. ).innerText),
  221. ].includes(
  222. document.body.getElementsByClassName(
  223. "artwork-title"
  224. )[0]?.innerText
  225. )
  226. ) {
  227. document.body
  228. .getElementsByClassName(
  229. "comment-item"
  230. )
  231. .forEach(c => {
  232. if (
  233. c.getElementsByClassName(
  234. "comment-title"
  235. )[0].innerText ||
  236. c.getElementsByClassName(
  237. "comment-content"
  238. )[0].innerText
  239. ) {
  240. const post = {
  241. like: +c
  242. .getElementsByClassName(
  243. "n-button-group"
  244. )[0]
  245. .innerText.split(
  246. "\n"
  247. )[0],
  248. username:
  249. c.getElementsByClassName(
  250. "comment-author"
  251. )[0].innerText,
  252. text:
  253. c.getElementsByClassName(
  254. "comment-title"
  255. )[0].innerText +
  256. (c.getElementsByClassName(
  257. "comment-title"
  258. )[0]
  259. .innerText &&
  260. c.getElementsByClassName(
  261. "comment-content"
  262. )[0].innerText
  263. ? document.createElement(
  264. "br"
  265. )
  266. .outerHTML
  267. : "") +
  268. c.getElementsByClassName(
  269. "comment-content"
  270. )[0].innerText,
  271. timestamp:
  272. c.getElementsByClassName(
  273. "comment-time"
  274. )[0].innerText,
  275. };
  276. document
  277. .getElementsByClassName(
  278. "comment-container"
  279. )[0]
  280. .appendChild(
  281. generatePost(
  282. post
  283. )
  284. );
  285. }
  286. });
  287. }
  288. }, 5000);
  289. }
  290. for (const post of b.flowthread.posts) {
  291. const _post = generatePost(post);
  292. _post.id = `comment-${post.id}`;
  293. if (post.parentid) {
  294. document
  295. .getElementById(
  296. `comment-${post.parentid}`
  297. )
  298. .appendChild(_post);
  299. } else {
  300. document
  301. .getElementsByClassName(
  302. "comment-container"
  303. )[0]
  304. .appendChild(_post);
  305. }
  306. }
  307. if (b.flowthread.count > offset + 15) {
  308. new IntersectionObserver(
  309. (entries, observer) => {
  310. entries.forEach(entry => {
  311. if (entry.isIntersecting) {
  312. getComment(offset + 15);
  313. observer.unobserve(
  314. entry.target
  315. );
  316. }
  317. });
  318. }
  319. ).observe(
  320. document.querySelector(
  321. ".comment-container > div.comment-thread:last-of-type"
  322. )
  323. );
  324. }
  325. });
  326. })(0);
  327. });
  328. });
  329. }
  330. })();