ZhiHuNock

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

目前为 2024-05-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name ZhiHuNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  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: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAABXCAMAAAC3HXLTAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAHLUExURQAAAJmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpmaqpqbq62uu7CxvaOks8bGz/39/f////X19qeotZqbqvDw8tLT2tTV293d4vv7+8DBy5ucq/7+/t7e46GhsPLy9LKzv97e5NDR2J2erfv7/Ofn66mqt87O1vX297+/ycTEzdPU26ytuu/v8cLCzKChsNbW3by9yPj4+fz8/bGyvubm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mXHTGq1Orwk3q9UyzDQZd7W7TYZl6bBOe8ygLHXqNvQMydkkQQsy0y0xrm+1q+ennGcR0j5v/ghNeccpWvWt48R7oJC0UKVwlGydDMZpu/+JCf39lfdFLAaSmBjQLEg0WDD8iaVw7DjuKj4QPmHqo9BJ56mu2tOzmxvznmbxHWv0jM3mKvaLojhULA7bnNaVz4XLI38BDLUAuY9SUusAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjQtMDQtMzBUMTA6Mzc6MTUrMDA6MDCSbBCjAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDI0LTA0LTMwVDE1OjUxOjM2KzAwOjAwaHi86AAAACh0RVh0ZGF0ZTp0aW1lc3RhbXAAMjAyNC0wNC0zMFQxNTo1MTo0OSswMDowMMPg5McAAAAASUVORK5CYII=",
  23. },
  24. readtime: {
  25. enabled: GM_getValue("readtime", true),
  26. selector: [".Post-RichText", ".Post-NormalMain"],
  27. icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAFCUlEQVRoge1YWU8bVxQ+nrFnxuMVs5iyLy4GK0AILQ1QaCqBVBLx0ERtUiEUIar2F+UxQjwgdVFQk1ZVFaFEbSpKaEExiwsB0pYQGiiLsT1je2bsqc4E0pJAwdcW6cN8yEKyfO79vrPdcy/o0KFDhw4dOgAMJD5QVRXGf54uowyGpIGikhnxo6qCrCSYfHf207KyQtFgSI2akWTPL7787lzg10c+jmOuGch8cSBkRQELb+65crnrMwBIyUFEQoLbIWduTtY1k4nI/FBgpCMRcWhzKzgCAOup2FIkG9pslpAgRPsoKnPRwFSSZQX/92ZlObZStifZFD13Y/j220+e/HWP4xiSJV4CppWZY7tbWhpGq73lW6nWCFFEEL4aTyAWi2uiMgEpLkNujmsdRZAsRyyEogxJk4nOiAjYjbKJMSrEfEgNHQ5biGWZDxKJzHRfRUmA3W4NkdoTC8nPz0larZZIIpEgXeI5kskkGI3GXnde9lPSNYiFIAoL81awTlItzBeB3crptAUr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EomE0UhTIMn7Hx2O23r/DXnfGqr28BeXZO5EhHg8pYt+/3wPRRlihNl5AFSQJJXJz8/d8tVUbmRoUR06dOjQoeNoAMDfddMStWBVXeEAAAAASUVORK5CYII=",
  28. },
  29. },
  30. action_btn_adjust: {
  31. enabled: GM_getValue("action_btn_adjust", true),
  32. selector: [".ContentItem-actions", ".BottomActions-CommentBtn", ".Post-Main"],
  33. },
  34. hidden_login: {
  35. enabled: GM_getValue("login_hidden", true),
  36. selector: [".Modal-closeButton", ".ZDI--Xmark16", "svg[width='26'][height='8']"],
  37. },
  38. };
  39.  
  40. const setModal = `<dialog 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> </dialog>`;
  41. 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 { 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; }`;
  42.  
  43. const dStyle = document.createElement("style");
  44. dStyle.innerHTML = setStyle;
  45. document.head.appendChild(dStyle);
  46. const modal = document.createElement("div");
  47. modal.innerHTML = setModal;
  48. document.body.appendChild(modal);
  49.  
  50. const checkboxList = document.querySelectorAll(".setting-item input");
  51. Array.from(checkboxList).forEach((checkbox) => {
  52. const nock = checkbox.getAttribute("aria-nock");
  53. checkbox.checked = GM_getValue(nock, true);
  54. });
  55.  
  56. const showSetting = () => document.querySelector(".modal-dialog").show();
  57. const closeSetting = () => document.querySelector(".modal-dialog").close();
  58.  
  59. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  60. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  61. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  62. document.querySelector(".modal-body").addEventListener("click", (e) => {
  63. if (e.target.type !== "checkbox") {
  64. return;
  65. }
  66. const nock = e.target.getAttribute("aria-nock");
  67. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  68. GM_setValue(nock, flag);
  69. });
  70. GM_registerMenuCommand("功能设置", showSetting);
  71.  
  72. const messageblock = document.createElement("div");
  73. if (features.mark.datetime.enabled) {
  74. const getRawTimeAgo = (element) => calculateTimeAgo(element.innerText.match(regex)[0]);
  75.  
  76. const postTime = document.querySelector(features.mark.datetime.selector[0]);
  77. const regex = /\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}/g;
  78. const rawTimeAgo = getRawTimeAgo(postTime);
  79.  
  80. const icon = document.createElement("img");
  81. icon.src = features.mark.datetime.icon;
  82. icon.width = 15;
  83. icon.style.marginRight = "5px";
  84.  
  85. const formattedTimeElement = document.createElement("span");
  86. formattedTimeElement.textContent = `(${rawTimeAgo})`;
  87.  
  88. const originalClick = postTime.onclick;
  89. postTime.style.marginTop = 0;
  90. postTime.style.cursor = "pointer";
  91. postTime.style.textDecoration = "underline";
  92. postTime.style.display = "inline-flex";
  93. postTime.style.alignItems = "center";
  94. postTime.prepend(icon);
  95. postTime.appendChild(formattedTimeElement);
  96. postTime.addEventListener("click", () => {
  97. originalClick();
  98. setTimeout(() => {
  99. const time = document.querySelector(features.mark.datetime.selector[0]);
  100. const rawTimeAgo = getRawTimeAgo(time);
  101. formattedTimeElement.textContent = `(${rawTimeAgo})`;
  102. }, 50);
  103. });
  104. messageblock.appendChild(postTime);
  105. }
  106.  
  107. if (features.mark.readtime.enabled) {
  108. const readbox = document.createElement("div");
  109. const textCount = document.querySelector(features.mark.readtime.selector[0]).innerText.length;
  110. const readtime = textCount / 400;
  111. if (readtime >= 1440) {
  112. const days = Math.floor(readtime / 1440);
  113. readbox.innerHTML = `<img src=${features.mark.readtime.icon} width='20'><span style='cursor:pointer;'> 预计阅读时长 ${days} 天</span>`;
  114. }
  115.  
  116. if (readtime >= 60) {
  117. const hours = Math.floor(readtime / 60);
  118. const minutes = Math.floor(readtime % 60);
  119. readbox.innerHTML = `<img src=${features.mark.readtime.icon} width='20'><span style='cursor:pointer;'> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</span>`;
  120. }
  121.  
  122. if (readtime >= 1) {
  123. const minutes = Math.round(readtime);
  124. readbox.innerHTML = `<img src=${features.mark.readtime.icon} width='20'><span style='cursor:pointer;'> 预计阅读时长 ${minutes} 分钟</span>`;
  125. }
  126.  
  127. if (readtime < 1) {
  128. const seconds = Math.round(readtime * 60);
  129. readbox.innerHTML = `<img src=${features.mark.readtime.icon} width='20'><span style='cursor:pointer;'> 预计阅读时长 ${seconds} 秒</span>`;
  130. }
  131. readbox.style.textDecoration = "underline";
  132. readbox.style.display = "inline-flex";
  133. readbox.style.color = "#8491a5";
  134. readbox.style.fontSize = "14px";
  135. readbox.style.alignItems = "center";
  136. readbox.addEventListener("click", () =>
  137. document.querySelector(features.mark.readtime.selector[1]).scrollIntoView(false)
  138. );
  139.  
  140. messageblock.appendChild(readbox);
  141. }
  142.  
  143. if (features.action_btn_adjust.enabled) {
  144. const style = document.createElement("style");
  145. style.textContent = ".RichContent-actions.is-fixed { position: inherit !important; }";
  146. document.head.appendChild(style);
  147.  
  148. const contentItemActions = document.querySelector(features.action_btn_adjust.selector[0]);
  149. const clonedContentItemActions = contentItemActions.cloneNode(true);
  150.  
  151. Array.from(contentItemActions.children).forEach((child, index) => {
  152. if (clonedContentItemActions.children.item(index)) {
  153. clonedContentItemActions.children
  154. .item(index)
  155. .addEventListener("click", () => child.click());
  156. }
  157. });
  158.  
  159. clonedContentItemActions.children.item(0).remove();
  160. clonedContentItemActions.children.item(0).remove();
  161. clonedContentItemActions.children.item(0).remove();
  162. clonedContentItemActions.children.item(clonedContentItemActions.children.length - 1).remove();
  163.  
  164. const plainItemActionBtn = clonedContentItemActions.children.item(0);
  165. const plainIcon = plainItemActionBtn.querySelector("span");
  166. plainItemActionBtn.addEventListener("click", () => {
  167. const text =
  168. plainItemActionBtn.innerText.replace(/[^\u4e00-\u9fa5]/g, "") == "喜欢"
  169. ? "​取消喜欢"
  170. : "喜欢";
  171. console.log(plainItemActionBtn.innerText.replace(/[^\u4e00-\u9fa5]/g, ""));
  172. plainItemActionBtn.innerHTML = text;
  173. plainItemActionBtn.prepend(plainIcon);
  174. });
  175.  
  176. const actionCommentBtn = document.querySelector(features.action_btn_adjust.selector[1]);
  177. const clonedActionCommentBtn = actionCommentBtn.cloneNode(true);
  178.  
  179. clonedActionCommentBtn.addEventListener("click", () => {
  180. const commentsContainer = document.querySelector(".Comments-container");
  181. commentsContainer.scrollIntoView({
  182. behavior: "smooth",
  183. block: "start",
  184. });
  185. });
  186. clonedContentItemActions.prepend(clonedActionCommentBtn);
  187.  
  188. Array.from(clonedContentItemActions.children).forEach((child) => {
  189. child.style.margin = 0;
  190. });
  191. Array.from(clonedContentItemActions.querySelectorAll("span")).forEach((span) => {
  192. span.style.display = "block";
  193. });
  194. Array.from(clonedContentItemActions.querySelectorAll(".Button-zi")).forEach((icon) => {
  195. icon.style.width = "1.3em";
  196. icon.style.height = "1.3em";
  197. });
  198.  
  199. clonedContentItemActions.style.display = "flex";
  200. clonedContentItemActions.style.position = "fixed";
  201. clonedContentItemActions.style.top = "260px";
  202. clonedContentItemActions.style.width = "96px";
  203. clonedContentItemActions.style.justifyContent = "center";
  204. clonedContentItemActions.style.right = "calc(50vw - 495px)";
  205. clonedContentItemActions.style.gap = "20px";
  206. clonedContentItemActions.style.margin = 0;
  207. clonedContentItemActions.style.padding = 0;
  208. clonedContentItemActions.style.opacity = 0;
  209. clonedContentItemActions.style.flexDirection = "column";
  210. clonedContentItemActions.style.transition = "opacity 0.55s ease-in-out";
  211. actionCommentBtn.parentNode.appendChild(clonedContentItemActions);
  212.  
  213. const postMain = document.querySelector(features.action_btn_adjust.selector[2]);
  214. const postMainHeight = postMain.offsetHeight + 370;
  215.  
  216. window.addEventListener("scroll", () => {
  217. const scrollTop = document.documentElement.scrollTop;
  218. const windowHeight = window.innerHeight;
  219. const postMainTop = postMain.offsetTop;
  220. const postMainBottom = postMainTop + postMainHeight;
  221.  
  222. if (scrollTop === 0 || scrollTop + windowHeight >= postMainBottom) {
  223. clonedContentItemActions.style.opacity = 0;
  224. } else {
  225. clonedContentItemActions.style.opacity = 1;
  226. }
  227. });
  228. }
  229.  
  230. if (features.hidden_login.enabled) {
  231. const createObserver = (selector, callback) => {
  232. const observer = new MutationObserver(() => {
  233. const closeBtn = document.querySelector(selector);
  234.  
  235. if (!closeBtn) {
  236. return;
  237. }
  238.  
  239. callback();
  240. observer.disconnect();
  241. });
  242.  
  243. observer.observe(document.body, { childList: true, subtree: true });
  244. };
  245.  
  246. createObserver(features.hidden_login.selector[0], () => {
  247. document.querySelector(features.hidden_login.selector[0]).click();
  248. });
  249.  
  250. createObserver(features.hidden_login.selector[1], () => {
  251. document.querySelector(
  252. features.hidden_login.selector[1]
  253. ).parentNode.parentNode.parentNode.style.display = "none";
  254. });
  255.  
  256. createObserver(features.hidden_login.selector[2], () => {
  257. document.querySelector(features.hidden_login.selector[2]).closest("div").style.display =
  258. "none";
  259. });
  260. }
  261.  
  262. messageblock.style.display = "flex";
  263. messageblock.style.alignItems = "center";
  264. messageblock.style.gap = "10px";
  265. document.querySelector(features.mark.datetime.selector[1]).appendChild(messageblock);
  266.  
  267. function calculateTimeAgo(datetime) {
  268. const SECOND = 1000;
  269. const MINUTE = 60 * SECOND;
  270. const HOUR = 60 * MINUTE;
  271. const DAY = 24 * HOUR;
  272. const MONTH = 30 * DAY;
  273. const YEAR = 12 * MONTH;
  274.  
  275. const postDate = new Date(datetime);
  276. const currentDate = new Date();
  277. const timeDiff = currentDate - postDate;
  278.  
  279. if (timeDiff >= YEAR) {
  280. return Math.floor(timeDiff / YEAR) + " 年前";
  281. }
  282. if (timeDiff >= MONTH) {
  283. return Math.floor(timeDiff / MONTH) + " 个月前";
  284. }
  285. if (timeDiff >= DAY) {
  286. return Math.floor(timeDiff / DAY) + " 天前";
  287. }
  288. if (timeDiff >= HOUR) {
  289. return Math.floor(timeDiff / HOUR) + " 小时前";
  290. }
  291. if (timeDiff >= MINUTE) {
  292. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  293. }
  294. return Math.floor(timeDiff / SECOND) + " 秒前";
  295. }
  296. })();

QingJ © 2025

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