JueJinNock

BlogNock系列,掘金文章的标识优化

目前为 2024-09-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name JueJinNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.5
  5. // @description BlogNock系列,掘金文章的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match juejin.cn/post/*
  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: [
  22. ".meta-box time",
  23. "script[type='application/ld+json']",
  24. ".author-info-block",
  25. "block-hidden",
  26. ".meta-box.team .views-count[style]",
  27. ],
  28. icon: `<svg height="21" viewBox="0 0 21 21" width="21" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" transform="translate(2 2)"><path stroke-width="1.2" d="m2.5.5h12c1.1045695 0 2 .8954305 2 2v12c0 1.1045695-.8954305 2-2 2h-12c-1.1045695 0-2-.8954305-2-2v-12c0-1.1045695.8954305-2 2-2z"/><path d="m.5 4.5h16" stroke-width="1.2"/><path stroke-width="1.2" d="m8.5 7.5v6" transform="matrix(0 1 -1 0 19 2)"/></g></svg>`,
  29. },
  30. readtime: {
  31. enabled: GM_getValue("readtime", true),
  32. selector: [".meta-box .read-time", "#article-root"],
  33. },
  34. },
  35. allow_copy_with_btn: {
  36. enabled: GM_getValue("allow_copy_with_btn", true),
  37. selector: [".code-block-extension-copyCodeBtn"],
  38. },
  39. copyright_text: {
  40. enabled: GM_getValue("copyright_text", true),
  41. selector: ["#article-root"],
  42. },
  43. hidden_login_tips: {
  44. enabled: GM_getValue("hidden_login_tips", true),
  45. selector: [".avatar-wrapper", ".bottom-login-guide"],
  46. },
  47. hidden: {
  48. ai_assistant_notification: {
  49. enabled: GM_getValue("ai_assistant_notification", true),
  50. selector: [".ai-assistant-notification"],
  51. },
  52. special_activity: {
  53. enabled: GM_getValue("special_activity", true),
  54. selector: [".special-activity-item"],
  55. },
  56. feedback_btn: {
  57. enabled: GM_getValue("feedback_btn", true),
  58. selector: [".meiqia-btn"],
  59. },
  60. more_btn: {
  61. enabled: GM_getValue("more_btn", true),
  62. selector: [".more-btn"],
  63. },
  64. ad_container: {
  65. enabled: GM_getValue("ad_container", true),
  66. selector: [".ad-container"],
  67. },
  68. },
  69. };
  70.  
  71. 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-allow-copy-with-btn" aria-nock="allow_copy_with_btn" /> <label for="feature-allow-copy-with-btn"></label> </span> </div> <div class="setting-item"> <span> 移除复制附加的版权声明 </span> <span> <input type="checkbox" id="feature-copyright-text" aria-nock="copyright_text" /> <label for="feature-copyright-text"></label> </span> </div> <hr /> <div class="setting-item"> <span> 隐藏登录提示 </span> <span> <input type="checkbox" id="feature-hidden-login-tips" aria-nock="hidden_login_tips" /> <label for="feature-hidden-login-tips"></label> </span> </div> <div class="setting-item"> <span> 隐藏界面首次加载时右侧的 AI 助手提示 </span> <span> <input type="checkbox" id="feature-hidden-ai-assistant-notification" aria-nock="ai_assistant_notification" /> <label for="feature-hidden-ai-assistant-notification"></label> </span> </div> <div class="setting-item"> <span> 隐藏顶部活动图片 </span> <span> <input type="checkbox" id="feature-hidden-special-activity" aria-nock="special_activity" /> <label for="feature-hidden-special-activity"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧反馈按钮 </span> <span> <input type="checkbox" id="feature-hidden-feedback-btn" aria-nock="feedback_btn" /> <label for="feature-hidden-feedback-btn"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧更多按钮 </span> <span> <input type="checkbox" id="feature-hidden-more-btn" aria-nock="more_btn" /> <label for="feature-hidden-more-btn"></label> </span> </div> <div class="setting-item"> <span> 隐藏右栏底部群广告 </span> <span> <input type="checkbox" id="feature-hidden-ad-container" aria-nock="ad_container" /> <label for="feature-hidden-ad-container"></label> </span> </div> </div> </div> </div>`;
  72. 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 { pointer-events: auto !important; 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; font-size: 14px; display: flex; justify-content: space-between; }`;
  73.  
  74. const dStyle = document.createElement("style");
  75. dStyle.innerHTML = setStyle;
  76. document.head.appendChild(dStyle);
  77. const modal = document.createElement("div");
  78. modal.innerHTML = setModal;
  79. document.body.appendChild(modal);
  80.  
  81. const checkboxList = document.querySelectorAll(".setting-item input");
  82. Array.from(checkboxList).forEach((checkbox) => {
  83. const nock = checkbox.getAttribute("aria-nock");
  84. checkbox.checked = GM_getValue(nock, true);
  85. });
  86.  
  87. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  88. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  89.  
  90. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  91. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  92. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  93. document.querySelector(".modal-body").addEventListener("click", (e) => {
  94. if (e.target.type !== "checkbox") {
  95. return;
  96. }
  97. const nock = e.target.getAttribute("aria-nock");
  98. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  99. GM_setValue(nock, flag);
  100. });
  101. GM_registerMenuCommand("功能设置", showSetting);
  102.  
  103. if (features.mark.datetime.enabled) {
  104. const datePublished = document.querySelector(features.mark.datetime.selector[0]);
  105. const jsonData = JSON.parse(
  106. document.querySelector(features.mark.datetime.selector[1])?.innerText ?? "{}"
  107. );
  108.  
  109. const rawPublishedTime = datePublished.getAttribute("datetime");
  110. const rawModifiedTime = jsonData[0]?.dateModified ?? rawPublishedTime;
  111.  
  112. const postTime = new Date(rawPublishedTime).toLocaleString().replaceAll("/", "-");
  113. const updateTime = new Date(rawModifiedTime).toLocaleString().replaceAll("/", "-");
  114.  
  115. const postTimeAgo = calculateTimeAgo(rawPublishedTime);
  116. const updateTimeAgo = calculateTimeAgo(rawModifiedTime);
  117.  
  118. const formattedPostTime = `发布于 ${postTime}(${postTimeAgo})`;
  119. const formattedUpdateTime = `编辑于 ${updateTime}(${updateTimeAgo})`;
  120.  
  121. const timeBlock = document.createElement("span");
  122. timeBlock.style.textDecoration = "underline";
  123. timeBlock.innerText = formattedPostTime;
  124.  
  125. datePublished.style.display = "flex";
  126. datePublished.style.alignItems = "center";
  127. datePublished.style.cursor = "pointer";
  128. datePublished.addEventListener("click", () => {
  129. const text =
  130. timeBlock.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime;
  131. timeBlock.innerText = text;
  132. });
  133.  
  134. const observer = new MutationObserver(() => {
  135. const infoBlock = document.querySelector(features.mark.datetime.selector[2]);
  136. if (infoBlock && infoBlock.classList.contains(features.mark.datetime.selector[3])) {
  137. return;
  138. }
  139.  
  140. const viewCountBlock = document.querySelector(features.mark.datetime.selector[4]);
  141. if (viewCountBlock && viewCountBlock.style.display == "none") {
  142. return;
  143. }
  144.  
  145. datePublished.innerHTML = features.mark.datetime.icon;
  146. datePublished.appendChild(timeBlock);
  147. const icon = datePublished.querySelector("svg");
  148. icon.style.width = "13px";
  149. icon.style.height = "13px";
  150. icon.style.marginRight = "5px";
  151.  
  152. observer.disconnect();
  153. });
  154.  
  155. observer.observe(document.body, { childList: true, subtree: true });
  156. }
  157.  
  158. if (features.mark.readtime.enabled) {
  159. const readtimeItem = document.querySelector(features.mark.readtime.selector[0]);
  160. const article = document.querySelector(features.mark.readtime.selector[1]);
  161. readtimeItem.style.cursor = "pointer";
  162. readtimeItem.style.textDecoration = "underline";
  163. readtimeItem.addEventListener("click", () => article.scrollIntoView(false));
  164. }
  165.  
  166. if (features.allow_copy_with_btn.enabled) {
  167. const observer = new MutationObserver(() => {
  168. const copyBtns = document.querySelectorAll(features.allow_copy_with_btn.selector[0]);
  169.  
  170. if (copyBtns.length === 0) {
  171. return;
  172. }
  173.  
  174. copyBtns.forEach((btn) => {
  175. btn.onclick = () => {
  176. const preBlock = btn.closest("pre");
  177. const codeBlock = preBlock.querySelector("code");
  178. navigator.clipboard.writeText(codeBlock.innerText);
  179. btn.innerText = "已复制";
  180. setTimeout(() => (btn.innerText = "复制代码"), 1500);
  181. };
  182. });
  183. observer.disconnect();
  184. });
  185. observer.observe(document.body, { childList: true, subtree: true });
  186. }
  187.  
  188. if (features.copyright_text.enabled) {
  189. const article = document.querySelector(features.copyright_text.selector[0]);
  190. article.addEventListener("copy", (e) => {
  191. e.preventDefault();
  192. e.stopImmediatePropagation();
  193. const selectedText = window.getSelection().toString();
  194. e.clipboardData.setData("text/plain", selectedText);
  195. });
  196. }
  197.  
  198. if (
  199. features.hidden_login_tips.enabled &&
  200. !document.querySelector(features.hidden_login_tips.selector[0])
  201. ) {
  202. const observer = new MutationObserver(() => {
  203. const loginTip = document.querySelector(features.hidden_login_tips.selector[1]);
  204.  
  205. if (!loginTip) {
  206. return;
  207. }
  208.  
  209. loginTip.style.display = "none";
  210. observer.disconnect();
  211. });
  212. observer.observe(document.body, { childList: true, subtree: true });
  213. }
  214.  
  215. const hiddenStyle = document.createElement("style");
  216. document.head.appendChild(hiddenStyle);
  217.  
  218. Object.entries(features.hidden).forEach(([key, option]) => {
  219. if (option.enabled) {
  220. const cssRule = `${option.selector} { display: none !important; }`;
  221. hiddenStyle.sheet.insertRule(cssRule);
  222. }
  223. });
  224.  
  225. function calculateTimeAgo(datetime) {
  226. const SECOND = 1000;
  227. const MINUTE = 60 * SECOND;
  228. const HOUR = 60 * MINUTE;
  229. const DAY = 24 * HOUR;
  230. const MONTH = 30 * DAY;
  231. const YEAR = 12 * MONTH;
  232.  
  233. const postDate = new Date(datetime);
  234. const currentDate = new Date();
  235. const timeDiff = currentDate - postDate;
  236.  
  237. if (timeDiff >= YEAR) {
  238. return Math.floor(timeDiff / YEAR) + " 年前";
  239. }
  240. if (timeDiff >= MONTH) {
  241. return Math.floor(timeDiff / MONTH) + " 个月前";
  242. }
  243. if (timeDiff >= DAY) {
  244. return Math.floor(timeDiff / DAY) + " 天前";
  245. }
  246. if (timeDiff >= HOUR) {
  247. return Math.floor(timeDiff / HOUR) + " 小时前";
  248. }
  249. if (timeDiff >= MINUTE) {
  250. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  251. }
  252. return Math.floor(timeDiff / SECOND) + " 秒前";
  253. }
  254. })();