ZhiHuNock

BlogNock系列,知乎文章的标识优化

当前为 2024-09-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ZhiHuNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.7
  5. // @description BlogNock系列,知乎文章的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match zhuanlan.zhihu.com/p/*
  9. // @supportURL https://github.com/Exisi/BlogNock/issues/
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17. const features = {
  18. mark: {
  19. datetime: {
  20. enabled: GM_getValue("datetime", true),
  21. selector: [".ContentItem-time", ".Post-Header"],
  22. icon: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path fill="currentcolor" d="M256 8C119 8 8 119 8 256S119 504 256 504 504 393 504 256 393 8 256 8zm92.5 313h0l-20 25a16 16 0 0 1 -22.5 2.5h0l-67-49.7a40 40 0 0 1 -15-31.2V112a16 16 0 0 1 16-16h32a16 16 0 0 1 16 16V256l58 42.5A16 16 0 0 1 348.5 321z"/></svg>`,
  23. },
  24. readtime: {
  25. enabled: GM_getValue("readtime", true),
  26. selector: [".Post-RichText", ".Post-NormalMain"],
  27. icon: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path fill="currentcolor" d="M360 0H24C10.7 0 0 10.7 0 24v16c0 13.3 10.7 24 24 24 0 91 51 167.7 120.8 192C75 280.3 24 357 24 448c-13.3 0-24 10.7-24 24v16c0 13.3 10.7 24 24 24h336c13.3 0 24-10.7 24-24v-16c0-13.3-10.7-24-24-24 0-91-51-167.7-120.8-192C309 231.7 360 155 360 64c13.3 0 24-10.7 24-24V24c0-13.3-10.7-24-24-24zm-64 448H88c0-77.5 46.2-144 104-144 57.8 0 104 66.5 104 144z"/></svg>`,
  28. },
  29. },
  30. action_btn_adjust: {
  31. enabled: GM_getValue("action_btn_adjust", true),
  32. selector: [
  33. ".ContentItem-actions",
  34. ".BottomActions-CommentBtn",
  35. ".Post-Main",
  36. ".AppHeader-profileAvatar",
  37. ],
  38. },
  39. hidden_login: {
  40. enabled: GM_getValue("login_hidden", true),
  41. selector: [
  42. ".AppHeader-profileAvatar",
  43. ".Modal-closeButton",
  44. ".ZDI--Xmark16",
  45. "svg[width='26'][height='8']",
  46. ],
  47. },
  48. };
  49.  
  50. const setModal = `<div class="modal-dialog"> <div class="modal-setting" onClick="event.cancelBubble = true"> <div class="modal-header"> <h3>功能设置</h3> <span class="btn-dialog-close">×</span> </div> <div class="modal-body"> <div class="setting-item"> <span> 文章显示时间优化 </span> <span> <input type="checkbox" id="feature-mark-datetime" aria-nock="datetime" /> <label for="feature-mark-datetime"></label> </span> </div> <div class="setting-item"> <span> 文章阅读时长 </span> <span> <input type="checkbox" id="feature-mark-readtime" aria-nock="readtime" /> <label for="feature-mark-readtime"></label> </span> </div> <div class="setting-item"> <span> 知乎文章按钮位置调整 </span> <span> <input type="checkbox" id="feature-action-btn-adjust" aria-nock="action_btn_adjust" /> <label for="feature-action-btn-adjust"></label> </span> </div> <div class="setting-item"> <span> 登录提示自动隐藏 </span> <span> <input type="checkbox" id="feature-login-hidden" aria-nock="login_hidden" /> <label for="feature-login-hidden"></label> </span> </div> </div> </div> </div>`;
  51. const setStyle = `@keyframes fall { 0% { transform: translate(0%, -100%); opacity: 0; } 100% { transform: translate(0%, 0%); opacity: 1; } } .setting-item input[type=checkbox] { height: 0; width: 0; display: none; } .setting-item label { cursor: pointer; text-indent: -9999px; width: 40px; height: 20px; background: pink; display: block; border-radius: 100px; position: relative; } .setting-item label:after { content: ''; position: absolute; top: 2px; left: 2px; width: 15px; height: 15px; background: #fff; border-radius: 90px; transition: 0.2s; } .setting-item input:checked+label { background: #57a; } .setting-item input:checked+label:after { left: calc(100% - 2px); transform: translateX(-100%); } .setting-item label:active:after { width: 28px; } .modal-dialog { display:none; border: none; position: fixed; z-index: 9999; left: 0; top: 0; width: 100%; min-width: 100vw; min-height: 100vh; height: 100%; background-color: rgba(0, 0, 0, 0.4); } .modal-setting { width: 450px; margin: auto; background-color: #ffffff; border-radius: 5px; padding: 20px; margin-top: 40px; position: relative; box-sizing: border-box; animation: fall 0.5s ease-in-out; } .modal-header { border-bottom: 1px solid #000000; } .modal-header h3 { padding: 10px 0; margin: 0; } .modal-header span { font-size: 24px; color: #ccc; position: absolute; right: 5px; top: 0; cursor: pointer; } .setting-item { margin: 10px 0; display: flex; justify-content: space-between; }`;
  52.  
  53. const dStyle = document.createElement("style");
  54. dStyle.innerHTML = setStyle;
  55. document.head.appendChild(dStyle);
  56. const modal = document.createElement("div");
  57. modal.innerHTML = setModal;
  58. document.body.appendChild(modal);
  59.  
  60. const checkboxList = document.querySelectorAll(".setting-item input");
  61. Array.from(checkboxList).forEach((checkbox) => {
  62. const nock = checkbox.getAttribute("aria-nock");
  63. checkbox.checked = GM_getValue(nock, true);
  64. });
  65.  
  66. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  67. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  68.  
  69. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  70. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  71. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  72. document.querySelector(".modal-body").addEventListener("click", (e) => {
  73. if (e.target.type !== "checkbox") {
  74. return;
  75. }
  76. const nock = e.target.getAttribute("aria-nock");
  77. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  78. GM_setValue(nock, flag);
  79. });
  80. GM_registerMenuCommand("功能设置", showSetting);
  81.  
  82. const messageblock = document.createElement("div");
  83. if (features.mark.datetime.enabled) {
  84. const getRawTimeAgo = (element) => calculateTimeAgo(element.innerText.match(regex)[0]);
  85.  
  86. const postTime = document.querySelector(features.mark.datetime.selector[0]);
  87. const regex = /\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}/g;
  88. const rawTimeAgo = getRawTimeAgo(postTime);
  89.  
  90. const iconItem = document.createElement("span");
  91. iconItem.innerHTML = features.mark.datetime.icon;
  92. const icon = iconItem.querySelector("svg");
  93. icon.style.width = "15px";
  94. icon.style.height = "15px";
  95. icon.style.marginRight = "5px";
  96.  
  97. const formattedTimeElement = document.createElement("span");
  98. formattedTimeElement.textContent = `(${rawTimeAgo})`;
  99.  
  100. const originalClick = postTime.onclick;
  101. postTime.style.marginTop = 0;
  102. postTime.style.cursor = "pointer";
  103. postTime.style.textDecoration = "underline";
  104. postTime.style.display = "inline-flex";
  105. postTime.style.alignItems = "center";
  106. postTime.prepend(icon);
  107. postTime.appendChild(formattedTimeElement);
  108. postTime.addEventListener("click", () => {
  109. originalClick();
  110. setTimeout(() => {
  111. const time = document.querySelector(features.mark.datetime.selector[0]);
  112. const rawTimeAgo = getRawTimeAgo(time);
  113. formattedTimeElement.textContent = `(${rawTimeAgo})`;
  114. }, 50);
  115. });
  116. messageblock.appendChild(postTime);
  117. }
  118.  
  119. if (features.mark.readtime.enabled) {
  120. const readbox = document.createElement("div");
  121. const textCount = document.querySelector(features.mark.readtime.selector[0]).innerText.length;
  122. const readtime = textCount / 400;
  123. if (readtime >= 1440) {
  124. const days = Math.floor(readtime / 1440);
  125. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${days} 天</span>`;
  126. }
  127.  
  128. if (readtime >= 60) {
  129. const hours = Math.floor(readtime / 60);
  130. const minutes = Math.floor(readtime % 60);
  131. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</span>`;
  132. }
  133.  
  134. if (readtime >= 1) {
  135. const minutes = Math.round(readtime);
  136. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${minutes} 分钟</span>`;
  137. }
  138.  
  139. if (readtime < 1) {
  140. const seconds = Math.round(readtime * 60);
  141. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${seconds} 秒</span>`;
  142. }
  143. readbox.style.textDecoration = "underline";
  144. readbox.style.display = "inline-flex";
  145. readbox.style.color = "#8491a5";
  146. readbox.style.fontSize = "14px";
  147. readbox.style.alignItems = "center";
  148. readbox.style.cursor = "pointer";
  149. readbox.addEventListener("click", () =>
  150. document.querySelector(features.mark.readtime.selector[1]).scrollIntoView(false)
  151. );
  152.  
  153. const icon = readbox.querySelector("svg");
  154. icon.style.width = "15px";
  155. icon.style.height = "15px";
  156. icon.style.marginRight = "5px";
  157.  
  158. messageblock.appendChild(readbox);
  159. }
  160.  
  161. if (features.action_btn_adjust.enabled) {
  162. const style = document.createElement("style");
  163. style.textContent = ".RichContent-actions.is-fixed { position: inherit !important; }";
  164. document.head.appendChild(style);
  165.  
  166. const contentItemActions = document.querySelector(features.action_btn_adjust.selector[0]);
  167. const clonedContentItemActions = contentItemActions.cloneNode(true);
  168.  
  169. Array.from(contentItemActions.children).forEach((child, index) => {
  170. if (clonedContentItemActions.children.item(index)) {
  171. clonedContentItemActions.children
  172. .item(index)
  173. .addEventListener("click", () => child.click());
  174. }
  175. });
  176.  
  177. clonedContentItemActions.children.item(0).remove();
  178. clonedContentItemActions.children.item(0).remove();
  179. clonedContentItemActions.children.item(0).remove();
  180. clonedContentItemActions.children.item(clonedContentItemActions.children.length - 1).remove();
  181.  
  182. const plainItemActionBtn = clonedContentItemActions.children.item(0);
  183. const plainIcon = plainItemActionBtn.querySelector("span");
  184.  
  185. const plainActionBtnEvent = () => {
  186. const loginStatus = document.querySelector(features.action_btn_adjust.selector[3]);
  187.  
  188. if (!loginStatus) {
  189. return;
  190. }
  191.  
  192. const text =
  193. plainItemActionBtn.innerText.replace(/[^\u4e00-\u9fa5]/g, "") == "喜欢"
  194. ? "​取消喜欢"
  195. : "喜欢";
  196.  
  197. plainItemActionBtn.innerHTML = text;
  198. plainItemActionBtn.prepend(plainIcon);
  199. };
  200.  
  201. contentItemActions.children.item(3).addEventListener("click", () => plainActionBtnEvent());
  202.  
  203. const actionCommentBtn = document.querySelector(features.action_btn_adjust.selector[1]);
  204. const clonedActionCommentBtn = actionCommentBtn.cloneNode(true);
  205.  
  206. clonedActionCommentBtn.addEventListener("click", () => {
  207. const commentsContainer = document.querySelector(".Comments-container");
  208. commentsContainer.scrollIntoView({
  209. behavior: "smooth",
  210. block: "start",
  211. });
  212. });
  213. clonedContentItemActions.prepend(clonedActionCommentBtn);
  214.  
  215. Array.from(clonedContentItemActions.children).forEach((child) => {
  216. child.style.margin = 0;
  217. });
  218. Array.from(clonedContentItemActions.querySelectorAll("span")).forEach((span) => {
  219. span.style.display = "block";
  220. });
  221. Array.from(clonedContentItemActions.querySelectorAll(".Button-zi")).forEach((icon) => {
  222. icon.style.width = "1.3em";
  223. icon.style.height = "1.3em";
  224. });
  225.  
  226. clonedContentItemActions.style.display = "flex";
  227. clonedContentItemActions.style.position = "fixed";
  228. clonedContentItemActions.style.top = "260px";
  229. clonedContentItemActions.style.width = "96px";
  230. clonedContentItemActions.style.justifyContent = "center";
  231. clonedContentItemActions.style.right = "calc(50vw - 495px)";
  232. clonedContentItemActions.style.gap = "20px";
  233. clonedContentItemActions.style.margin = 0;
  234. clonedContentItemActions.style.padding = 0;
  235. clonedContentItemActions.style.opacity = 0;
  236. clonedContentItemActions.style.backgroundColor = "#00000000";
  237. clonedContentItemActions.style.flexDirection = "column";
  238. clonedContentItemActions.style.transition = "opacity 0.55s ease-in-out";
  239. actionCommentBtn.parentNode.appendChild(clonedContentItemActions);
  240.  
  241. const postMain = document.querySelector(features.action_btn_adjust.selector[2]);
  242. const postMainHeight = postMain.offsetHeight + 40;
  243.  
  244. window.addEventListener("scroll", () => {
  245. const scrollTop = document.documentElement.scrollTop;
  246. const windowHeight = window.innerHeight;
  247. const postMainTop = postMain.offsetTop;
  248. const postMainBottom = postMainTop + postMainHeight;
  249.  
  250. if (scrollTop === 0 || scrollTop + windowHeight >= postMainBottom) {
  251. clonedContentItemActions.style.opacity = 0;
  252. clonedContentItemActions.style.display = "none";
  253. } else {
  254. clonedContentItemActions.style.opacity = 1;
  255. clonedContentItemActions.style.display = "flex";
  256. }
  257. });
  258. }
  259.  
  260. if (features.hidden_login.enabled && !document.querySelector(features.hidden_login.selector[0])) {
  261. const createObserver = (selector, callback) => {
  262. const observer = new MutationObserver(() => {
  263. const closeBtn = document.querySelector(selector);
  264.  
  265. if (!closeBtn) {
  266. return;
  267. }
  268.  
  269. callback();
  270. observer.disconnect();
  271. });
  272.  
  273. observer.observe(document.body, { childList: true, subtree: true });
  274. };
  275.  
  276. createObserver(features.hidden_login.selector[1], () => {
  277. document.querySelector(features.hidden_login.selector[1]).click();
  278. });
  279.  
  280. createObserver(features.hidden_login.selector[2], () => {
  281. document.querySelector(
  282. features.hidden_login.selector[2]
  283. ).parentNode.parentNode.parentNode.style.display = "none";
  284. });
  285.  
  286. createObserver(features.hidden_login.selector[3], () => {
  287. document.querySelector(features.hidden_login.selector[3]).closest("div").style.display =
  288. "none";
  289. });
  290. }
  291.  
  292. messageblock.style.display = "flex";
  293. messageblock.style.alignItems = "center";
  294. messageblock.style.gap = "10px";
  295. document.querySelector(features.mark.datetime.selector[1]).appendChild(messageblock);
  296.  
  297. function calculateTimeAgo(datetime) {
  298. const SECOND = 1000;
  299. const MINUTE = 60 * SECOND;
  300. const HOUR = 60 * MINUTE;
  301. const DAY = 24 * HOUR;
  302. const MONTH = 30 * DAY;
  303. const YEAR = 12 * MONTH;
  304.  
  305. const postDate = new Date(datetime);
  306. const currentDate = new Date();
  307. const timeDiff = currentDate - postDate;
  308.  
  309. if (timeDiff >= YEAR) {
  310. return Math.floor(timeDiff / YEAR) + " 年前";
  311. }
  312. if (timeDiff >= MONTH) {
  313. return Math.floor(timeDiff / MONTH) + " 个月前";
  314. }
  315. if (timeDiff >= DAY) {
  316. return Math.floor(timeDiff / DAY) + " 天前";
  317. }
  318. if (timeDiff >= HOUR) {
  319. return Math.floor(timeDiff / HOUR) + " 小时前";
  320. }
  321. if (timeDiff >= MINUTE) {
  322. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  323. }
  324. return Math.floor(timeDiff / SECOND) + " 秒前";
  325. }
  326. })();