JustMoeComments

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

安装此脚本?
作者推荐脚本

您可能也喜欢灰机wiki查看版本历史

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