51CTONock

BlogNock系列,51CTO 文章的标识优化

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

  1. // ==UserScript==
  2. // @name 51CTONock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  5. // @description BlogNock系列,51CTO 文章的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match blog.51cto.com/*
  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: [".messbox time", "script[type='application/ld+json']"],
  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 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200zm61.8-104.4l-84.9-61.7c-3.1-2.3-4.9-5.9-4.9-9.7V116c0-6.6 5.4-12 12-12h32c6.6 0 12 5.4 12 12v141.7l66.8 48.6c5.4 3.9 6.5 11.4 2.6 16.8L334.6 349c-3.9 5.3-11.4 6.5-16.8 2.6z"/></svg>`,
  23. },
  24. readtime: {
  25. enabled: GM_getValue("readtime", true),
  26. selector: [".messbox time", ".article-content-wrap"],
  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="M368 48h4c6.6 0 12-5.4 12-12V12c0-6.6-5.4-12-12-12H12C5.4 0 0 5.4 0 12v24c0 6.6 5.4 12 12 12h4c0 80.6 32.2 165.8 97.2 208C47.9 298.4 16 383.9 16 464h-4c-6.6 0-12 5.4-12 12v24c0 6.6 5.4 12 12 12h360c6.6 0 12-5.4 12-12v-24c0-6.6-5.4-12-12-12h-4c0-80.6-32.2-165.8-97.2-208C336.1 213.6 368 128.1 368 48zM64 48h256c0 101.6-57.3 184-128 184S64 149.6 64 48zm256 416H64c0-101.6 57.3-184 128-184s128 82.4 128 184z"/></svg>`,
  28. },
  29. },
  30. allow_copy: {
  31. enabled: GM_getValue("allow_copy", true),
  32. selector: [".code-toolbar", ".article-content-wrap"],
  33. },
  34. allow_copy_with_btn: {
  35. enabled: GM_getValue("allow_copy_with_btn", true),
  36. selector: [".copy_btn", "code"],
  37. },
  38. hidden: {
  39. first_publish_tips: {
  40. enabled: GM_getValue("first_publish_tips", true),
  41. selector: [".header-content li.first"],
  42. },
  43. publish_task_tips: {
  44. enabled: GM_getValue("publish_task_tips", true),
  45. selector: [".header-content>.task", ".header-right .creative"],
  46. },
  47. min_menu_pic: {
  48. enabled: GM_getValue("min_menu_pic", true),
  49. selector: [".hover-ball"],
  50. },
  51. },
  52. special_hidden: {
  53. first_publish_tips: {
  54. enabled: GM_getValue("first_publish_tips", true),
  55. selector: [".detail-content-right section", "section.common-fix"],
  56. },
  57. },
  58. };
  59.  
  60. 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" aria-nock="allow_copy" /> <label for="feature-allow-copy"></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> <hr /> <div class="setting-item"> <span> 隐藏首次发文提示 </span> <span> <input type="checkbox" id="feature-hidden-first-publish-tips" aria-nock="first_publish_tips" /> <label for="feature-hidden-first-publish-tips"></label> </span> </div> <div class="setting-item"> <span> 隐藏发文任务提示 </span> <span> <input type="checkbox" id="feature-hidden-publish-task-tips" aria-nock="publish_task_tips" /> <label for="feature-hidden-publish-task-tips"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧迷你按钮图片 </span> <span> <input type="checkbox" id="feature-hidden-min-menu-pic" aria-nock="min_menu_pic" /> <label for="feature-hidden-min-menu-pic"></label> </span> </div> </div> </div> </div>`;
  61. 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; }`;
  62.  
  63. const dStyle = document.createElement("style");
  64. dStyle.innerHTML = setStyle;
  65. document.head.appendChild(dStyle);
  66. const modal = document.createElement("div");
  67. modal.innerHTML = setModal;
  68. document.body.appendChild(modal);
  69.  
  70. const checkboxList = document.querySelectorAll(".setting-item input");
  71. Array.from(checkboxList).forEach((checkbox) => {
  72. const nock = checkbox.getAttribute("aria-nock");
  73. checkbox.checked = GM_getValue(nock, true);
  74. });
  75.  
  76. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  77. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  78.  
  79. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  80. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  81. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  82. document.querySelector(".modal-body").addEventListener("click", (e) => {
  83. if (e.target.type !== "checkbox") {
  84. return;
  85. }
  86. const nock = e.target.getAttribute("aria-nock");
  87. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  88. GM_setValue(nock, flag);
  89. });
  90. GM_registerMenuCommand("功能设置", showSetting);
  91.  
  92. if (features.mark.datetime.enabled) {
  93. const datePublished = document.querySelector(features.mark.datetime.selector[0]);
  94. const jsonData = JSON.parse(
  95. document.querySelector(features.mark.datetime.selector[1])?.innerText ?? "{}"
  96. );
  97. const rawPublishedTime = datePublished.getAttribute("pubdate");
  98. const rawModifiedTime = jsonData?.upDate ?? rawPublishedTime;
  99. const postTime = new Date(rawPublishedTime).toLocaleString().replaceAll("/", "-");
  100. const updateTime = new Date(rawModifiedTime).toLocaleString().replaceAll("/", "-");
  101. const postTimeAgo = calculateTimeAgo(rawPublishedTime);
  102. const updateTimeAgo = calculateTimeAgo(rawModifiedTime);
  103. const formattedPostTime = `发布于 ${postTime}(${postTimeAgo})`;
  104. const formattedUpdateTime = `编辑于 ${updateTime}(${updateTimeAgo})`;
  105. const timeBlock = document.createElement("span");
  106. timeBlock.style.textDecoration = "underline";
  107. timeBlock.innerText = formattedPostTime;
  108. datePublished.style.display = "flex";
  109. datePublished.style.alignItems = "center";
  110. datePublished.style.cursor = "pointer";
  111. datePublished.innerHTML = features.mark.datetime.icon;
  112. datePublished.appendChild(timeBlock);
  113. datePublished.addEventListener("click", () => {
  114. const text =
  115. timeBlock.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime;
  116. timeBlock.innerText = text;
  117. });
  118.  
  119. const icon = datePublished.querySelector("svg");
  120. icon.style.width = "14px";
  121. icon.style.height = "14px";
  122. icon.style.marginRight = "3px";
  123. }
  124.  
  125. if (features.mark.readtime.enabled) {
  126. const timeItem = document.querySelector(features.mark.readtime.selector[0]);
  127.  
  128. const readbox = document.createElement("span");
  129. const textCount = document.querySelector(features.mark.readtime.selector[1]).innerText.length;
  130. const readtime = textCount / 400;
  131. if (readtime >= 1440) {
  132. const days = Math.floor(readtime / 1440);
  133. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${days} 天</span>`;
  134. }
  135.  
  136. if (readtime >= 60) {
  137. const hours = Math.floor(readtime / 60);
  138. const minutes = Math.floor(readtime % 60);
  139. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</span>`;
  140. }
  141.  
  142. if (readtime >= 1) {
  143. const minutes = Math.round(readtime);
  144. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${minutes} 分钟</span>`;
  145. }
  146.  
  147. if (readtime < 1) {
  148. const seconds = Math.round(readtime * 60);
  149. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${seconds} 秒</span>`;
  150. }
  151. readbox.style.textDecoration = "underline";
  152. readbox.style.display = "inline-flex";
  153. readbox.style.alignItems = "center";
  154. readbox.style.cursor = "pointer";
  155. readbox.addEventListener("click", () =>
  156. document.querySelector(features.mark.readtime.selector[1]).scrollIntoView(false)
  157. );
  158.  
  159. const icon = readbox.querySelector("svg");
  160. icon.style.width = "14px";
  161. icon.style.height = "14px";
  162. icon.style.marginRight = "3px";
  163.  
  164. timeItem.after(readbox);
  165. }
  166.  
  167. if (features.allow_copy.enabled) {
  168. const observer = new MutationObserver(() => {
  169. const codeToolBar = document.querySelector(features.allow_copy.selector[0]);
  170.  
  171. if (!codeToolBar) {
  172. return;
  173. }
  174.  
  175. const article = document.querySelector(features.allow_copy.selector[1]);
  176. article.querySelectorAll("*").forEach((content) => {
  177. content.oncopy = (e) => e.stopPropagation();
  178. content.onkeydown = (e) => e.stopPropagation();
  179. });
  180. observer.disconnect();
  181. });
  182. observer.observe(document.body, { childList: true, subtree: true });
  183. }
  184.  
  185. if (features.allow_copy_with_btn.enabled) {
  186. console.log("object");
  187. const observer = new MutationObserver(() => {
  188. const copyBtns = document.querySelectorAll(features.allow_copy_with_btn.selector[0]);
  189.  
  190. if (copyBtns.length == 0) {
  191. return;
  192. }
  193.  
  194. copyBtns.forEach((btn) => {
  195. btn.innerText = "复制";
  196. btn.addEventListener("click", (e) => {
  197. e.stopPropagation();
  198. const codeBlock = e.target.parentNode.parentNode;
  199. const code = codeBlock.querySelector(features.allow_copy_with_btn.selector[1]);
  200. navigator.clipboard.writeText(code.innerText);
  201. btn.innerText = "已复制";
  202. setTimeout(() => (btn.innerText = "复制"), 1000);
  203. });
  204. });
  205.  
  206. observer.disconnect();
  207. });
  208. observer.observe(document.body, { childList: true, subtree: true });
  209. }
  210.  
  211. const hiddenStyle = document.createElement("style");
  212. document.head.appendChild(hiddenStyle);
  213.  
  214. Object.entries(features.hidden).forEach(([key, option]) => {
  215. if (option.enabled) {
  216. const cssRule = `${option.selector} { display: none !important; }`;
  217. hiddenStyle.sheet.insertRule(cssRule);
  218. }
  219. });
  220.  
  221. if (features.special_hidden.first_publish_tips.enabled) {
  222. const tipItem = document.querySelector(features.special_hidden.first_publish_tips.selector[1]);
  223. tipItem.style.display = "none";
  224. const cssRule = `${features.special_hidden.first_publish_tips.selector[0]}{ visibility: visible !important; }`;
  225. hiddenStyle.sheet.insertRule(cssRule);
  226. }
  227.  
  228. function calculateTimeAgo(datetime) {
  229. const SECOND = 1000;
  230. const MINUTE = 60 * SECOND;
  231. const HOUR = 60 * MINUTE;
  232. const DAY = 24 * HOUR;
  233. const MONTH = 30 * DAY;
  234. const YEAR = 12 * MONTH;
  235.  
  236. const postDate = new Date(datetime);
  237. const currentDate = new Date();
  238. const timeDiff = currentDate - postDate;
  239.  
  240. if (timeDiff >= YEAR) {
  241. return Math.floor(timeDiff / YEAR) + " 年前";
  242. }
  243. if (timeDiff >= MONTH) {
  244. return Math.floor(timeDiff / MONTH) + " 个月前";
  245. }
  246. if (timeDiff >= DAY) {
  247. return Math.floor(timeDiff / DAY) + " 天前";
  248. }
  249. if (timeDiff >= HOUR) {
  250. return Math.floor(timeDiff / HOUR) + " 小时前";
  251. }
  252. if (timeDiff >= MINUTE) {
  253. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  254. }
  255. return Math.floor(timeDiff / SECOND) + " 秒前";
  256. }
  257. })();