SegmentFaultNock

BlogNock系列,思否文章的标识优化

Fra 09.05.2024. Se den seneste versjonen.

  1. // ==UserScript==
  2. // @name SegmentFaultNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.5
  5. // @description BlogNock系列,思否文章的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match segmentfault.com/a/*
  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. modified_history: {
  20. enabled: GM_getValue("modified_history", true),
  21. selector: ["time[itemprop='datePublished']"],
  22. icon: `<svg height="21" viewBox="0 0 21 21" width="21" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" transform="matrix(0 1 1 0 0 2)"><!--!System UIcons by @CoreyGinnivan - https://systemuicons.com/ License - Unlicense license Copyright 2020 CoreyGinnivan--><path d="m8.54949429 2.5c-2.77910025-.01404818-5.48733216 1.42226095-6.97636172 4.0013358-2.209139 3.826341-.89813776 8.7190642 2.92820323 10.9282032s8.7190642.8981378 10.9282032-2.9282032.8981378-8.71906423-2.9282032-10.92820323" stroke="currentColor" stroke-linecap="round" stroke-width="1.8" stroke-linejoin="round"/><path d="m11.5 2.5-3 2.5v-5z" fill="currentColor" fill-rule="nonzero" stroke-width="1.8"/><path d="m4.5 10.5h5v3" stroke="currentColor" stroke-linecap="round" stroke-width="1.8" stroke-linejoin="round"/></g></svg>`,
  23. },
  24. adjust_position: {
  25. enabled: GM_getValue("adjust_position", true),
  26. selector: ["time[itemprop='datePublished']"],
  27. icon: `<svg height="21" viewBox="0 0 21 21" width="21" xmlns="http://www.w3.org/2000/svg"><!--!System UIcons by @CoreyGinnivan - https://systemuicons.com/ License - Unlicense license Copyright 2020 CoreyGinnivan--><g fill="none" fill-rule="evenodd" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.8" transform="translate(4 2)"><path d="m6.5 16.5407715.6311176-.7118691c.71585099-.8191184 1.36011688-1.5983525 1.93279767-2.3377022l.4733382-.6239608c1.97516433-2.6615039 2.96274653-4.77276704 2.96274653-6.33378943 0-3.33218241-2.6862915-6.03344997-6-6.03344997s-6 2.70126756-6 6.03344997c0 1.56102239.98758218 3.67228553 2.96274653 6.33378943l.4733382.6239608c.73630387.9505925 1.5909423 1.9671163 2.56391527 3.0495713z"/><circle cx="6.5" cy="6.5" r="2.5"/></g></svg>`,
  28. },
  29. datetime: {
  30. enabled: GM_getValue("datetime", true),
  31. selector: ["time[itemprop='datePublished']", "script[type='application/ld+json']"],
  32. 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>`,
  33. },
  34. readtime: {
  35. enabled: GM_getValue("readtime", true),
  36. selector: ["time[itemprop='datePublished']", "article"],
  37. },
  38. },
  39. };
  40.  
  41. 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-modified-history" aria-nock="modified_history" /> <label for="feature-mark-modified-history"></label> </span> </div> <div class="setting-item"> <span> 定位文字调整 </span> <span> <input type="checkbox" id="feature-mark-adjust-position" aria-nock="adjust_position" /> <label for="feature-mark-adjust-position"></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> </div> </div>`;
  42. 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; display: flex; justify-content: space-between; }`;
  43.  
  44. const dStyle = document.createElement("style");
  45. dStyle.innerHTML = setStyle;
  46. document.head.appendChild(dStyle);
  47. const modal = document.createElement("div");
  48. modal.innerHTML = setModal;
  49. document.body.appendChild(modal);
  50.  
  51. const checkboxList = document.querySelectorAll(".setting-item input");
  52. Array.from(checkboxList).forEach((checkbox) => {
  53. const nock = checkbox.getAttribute("aria-nock");
  54. checkbox.checked = GM_getValue(nock, true);
  55. });
  56.  
  57. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  58. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  59.  
  60. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  61. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  62. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  63. document.querySelector(".modal-body").addEventListener("click", (e) => {
  64. if (e.target.type !== "checkbox") {
  65. return;
  66. }
  67. const nock = e.target.getAttribute("aria-nock");
  68. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  69. GM_setValue(nock, flag);
  70. });
  71. GM_registerMenuCommand("功能设置", showSetting);
  72.  
  73. if (features.mark.modified_history.enabled) {
  74. const datePublished = document.querySelector(features.mark.datetime.selector[0]);
  75. const datetimeItem = datePublished.parentNode;
  76. const history = document.createElement("a");
  77. history.innerHTML = `${features.mark.modified_history.icon}<span>历史修改</span>`;
  78. history.style.cursor = "pointer";
  79. history.style.marginLeft = "10px";
  80. history.style.color = "rgba(108, 117, 125)";
  81. history.style.display = "flex";
  82. history.style.alignItems = "center";
  83. history.addEventListener("click", () => {
  84. document.querySelectorAll(features.mark.modified_history.selector[0])[1].click();
  85. });
  86. datetimeItem.before(history);
  87.  
  88. const icon = history.querySelector("svg");
  89. icon.style.width = "17px";
  90. icon.style.height = "17px";
  91. icon.style.marginRight = "3px";
  92. }
  93.  
  94. if (
  95. features.mark.adjust_position.enabled &&
  96. document.querySelector(features.mark.adjust_position.selector[0]).nextSibling
  97. ) {
  98. const time = document.querySelector(features.mark.adjust_position.selector[0]);
  99. const position = time.nextSibling;
  100. const positionBox = document.createElement("a");
  101.  
  102. positionBox.style.display = "flex";
  103. positionBox.style.alignItems = "center";
  104. positionBox.style.marginLeft = "10px";
  105. positionBox.style.color = "rgba(108, 117, 125)";
  106. positionBox.innerHTML = features.mark.adjust_position.icon;
  107. positionBox.appendChild(position);
  108. const icon = positionBox.querySelector("svg");
  109. icon.style.width = "16px";
  110. icon.style.height = "16px";
  111. time.parentNode.after(positionBox);
  112. }
  113.  
  114. if (features.mark.datetime.enabled) {
  115. const datePublished = document.querySelector(features.mark.datetime.selector[0]);
  116.  
  117. const jsonData = JSON.parse(
  118. document.querySelector(features.mark.datetime.selector[1])?.innerText ?? "{}"
  119. );
  120.  
  121. const rawPublishedTime = datePublished.getAttribute("datetime");
  122. const rawUpdatedTime = jsonData.dateModified ?? rawPublishedTime;
  123.  
  124. const postTimeAgo = calculateTimeAgo(rawPublishedTime);
  125. const updateTimeAgo = calculateTimeAgo(rawUpdatedTime);
  126.  
  127. const postTime = new Date(rawPublishedTime).toLocaleString().replaceAll("/", "-");
  128. const updateTime = new Date(rawUpdatedTime).toLocaleString().replaceAll("/", "-");
  129.  
  130. const formattedPostTime = `发布时间 ${postTime}(${postTimeAgo})`;
  131. const formattedUpdateTime = `更新时间 ${updateTime}(${updateTimeAgo})`;
  132.  
  133. const datetimeItem = datePublished.parentNode;
  134. datetimeItem.innerHTML = features.mark.datetime.icon;
  135.  
  136. datePublished.style.textDecoration = "underline";
  137. datePublished.innerHTML = formattedPostTime;
  138.  
  139. datetimeItem.style.display = "flex";
  140. datetimeItem.style.alignItems = "center";
  141. datetimeItem.appendChild(datePublished);
  142. datetimeItem.addEventListener("click", (e) => {
  143. e.preventDefault();
  144. const text =
  145. datePublished.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime;
  146. datePublished.innerText = text;
  147. });
  148.  
  149. const icon = datetimeItem.querySelector("svg");
  150. icon.style.width = "16px";
  151. icon.style.height = "15px";
  152. icon.style.marginRight = "3px";
  153. }
  154.  
  155. if (features.mark.readtime.enabled) {
  156. const time = document.querySelector(features.mark.readtime.selector[0]);
  157. const article = document.querySelector(features.mark.readtime.selector[1]);
  158. const readtime = time.closest("div").lastChild;
  159. readtime.lastChild.style.textDecoration = "underline";
  160. readtime.style.cursor = "pointer";
  161. readtime.addEventListener("click", () => article.scrollIntoView(false));
  162. }
  163.  
  164. function calculateTimeAgo(datetime) {
  165. const SECOND = 1000;
  166. const MINUTE = 60 * SECOND;
  167. const HOUR = 60 * MINUTE;
  168. const DAY = 24 * HOUR;
  169. const MONTH = 30 * DAY;
  170. const YEAR = 12 * MONTH;
  171.  
  172. const postDate = new Date(datetime);
  173. const currentDate = new Date();
  174. const timeDiff = currentDate - postDate;
  175.  
  176. if (timeDiff >= YEAR) {
  177. return Math.floor(timeDiff / YEAR) + " 年前";
  178. }
  179. if (timeDiff >= MONTH) {
  180. return Math.floor(timeDiff / MONTH) + " 个月前";
  181. }
  182. if (timeDiff >= DAY) {
  183. return Math.floor(timeDiff / DAY) + " 天前";
  184. }
  185. if (timeDiff >= HOUR) {
  186. return Math.floor(timeDiff / HOUR) + " 小时前";
  187. }
  188. if (timeDiff >= MINUTE) {
  189. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  190. }
  191. return Math.floor(timeDiff / SECOND) + " 秒前";
  192. }
  193. })();