TencentCloudDeveloperNock

BlogNock 系列,腾讯云开发者社区文章的标识优化

  1. // ==UserScript==
  2. // @name TencentCloudDeveloperNock
  3. // @namespace https://github.com/Exisi/BlogNock
  4. // @version 0.0.7
  5. // @icon https://raw.githubusercontent.com/Exisi/BlogNock/main/doc/icon/nock.ico
  6. // @description BlogNock 系列,腾讯云开发者社区文章的标识优化
  7. // @author Exisi
  8. // @license MIT License
  9. // @match *://cloud.tencent.com/developer/article/*
  10. // @supportURL https://github.com/Exisi/BlogNock/issues/
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. "use strict";
  18. const features = {
  19. mark: {
  20. datetime: {
  21. enabled: GM_getValue("datetime", true),
  22. selector: ["meta[name='subjectTime'", ".mod-article-content .date-text"],
  23. 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>`,
  24. },
  25. readtime: {
  26. enabled: GM_getValue("readtime", true),
  27. selector: [".mod-content", ".mod-article-content .date-text"],
  28. 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>`,
  29. },
  30. },
  31. sleep: {
  32. left_emoji_animation: {
  33. enabled: GM_getValue("left_emoji_animation", true),
  34. selector: [".cdc-suspend-pill .emoji-item-icon"],
  35. },
  36. },
  37. hidden: {
  38. day_loading_activity_modal: {
  39. enabled: GM_getValue("day_loading_activity_modal", true),
  40. selector: [
  41. ".cdc-portal-wrapper:has(.mod-activity)",
  42. ".cdc-portal-wrapper:has(.cdc-activity-modal)",
  43. ],
  44. },
  45. top_product_ad_pic: {
  46. enabled: GM_getValue("top_product_ad_pic", true),
  47. selector: ["#community-top-header-product-container:has(.cdc-top-ad)"],
  48. },
  49. top_activity_ad_pic: {
  50. enabled: GM_getValue("top_activity_ad_pic", true),
  51. selector: [".cdc-header__capsule"],
  52. },
  53. right_notice_action_btn: {
  54. enabled: GM_getValue("right_notice_action_btn", true),
  55. selector: [".cdc-widget-global:has(.announcement)", "#tea-overlay-root"],
  56. },
  57. right_coupon_action_btn: {
  58. enabled: GM_getValue("right_coupon_action_btn", true),
  59. selector: [".cdc-widget-global__btn:has(.cdc-widget-global__btn-tag)"],
  60. },
  61. right_qrcode_group_ad: {
  62. enabled: GM_getValue("right_qrcode_group_ad", true),
  63. selector: [".cdc-commercial-card.mod-group-card"],
  64. },
  65. right_pic_ad: {
  66. enabled: GM_getValue("right_pic_ad", true),
  67. selector: [
  68. ".cdc-commercial-card:has(.cdc-commercial-card__img)",
  69. ".cdc-commercial-card:has(img[class*='ad-image'])",
  70. ],
  71. },
  72. right_related_product_and_service: {
  73. enabled: GM_getValue("right_related_product_and_service", true),
  74. selector: [".cdc-mod-product2:has(.cdc-product-info2__list)"],
  75. },
  76. right_discussion_card: {
  77. enabled: GM_getValue("right_discussion_card", true),
  78. selector: [".cdc-discussion-card:has(.cdc-discussion-card__recommend)"],
  79. },
  80. right_related_lesson_card: {
  81. enabled: GM_getValue("right_related_lesson_card", true),
  82. selector: [".cdc-free-course-card:has(.cdc-free-course-card__detail)"],
  83. },
  84. left_corner_bubble_card: {
  85. enabled: GM_getValue("left_corner_bubble_card", true),
  86. selector: [".cdc-dark-tip-bubble"],
  87. },
  88. },
  89. };
  90.  
  91. const setModal = `<div class=modal-dialog><div class=modal-setting onclick="event.cancelBubble=!0"><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 aria-nock=datetime id=feature-mark-datetime type=checkbox> <label for=feature-mark-datetime></label></span></div><div class=setting-item><span>文章阅读时长 </span><span><input aria-nock=readtime id=feature-mark-readtime type=checkbox> <label for=feature-mark-readtime></label></span></div><hr><div class=setting-item><span>停止文章左侧按钮图标动画 </span><span><input aria-nock=left_emoji_animation id=feature-sleep-left-emoji-animation type=checkbox> <label for=feature-sleep-left-emoji-animation></label></span></div><hr><div class=setting-item><span>隐藏每日活动提示的模态框 </span><span><input aria-nock=day_loading_activity_modal id=feature-hidden-day-loading-activity-modal type=checkbox> <label for=feature-hidden-day-loading-activity-modal></label></span></div><div class=setting-item><span>隐藏顶部产品广告图片 </span><span><input aria-nock=top_product_ad_pic id=feature-hidden-top-product-ad-pic type=checkbox> <label for=feature-hidden-top-product-ad-pic></label></span></div><div class=setting-item><span>隐藏顶部活动广告图片 </span><span><input aria-nock=top_activity_ad_pic id=feature-hidden-top-activity-ad-pic type=checkbox> <label for=feature-hidden-top-activity-ad-pic></label></span></div><div class=setting-item><span>隐藏右侧公告提示操作按钮 </span><span><input aria-nock=right_notice_action_btn id=feature-hidden-right-notice-action-btn type=checkbox> <label for=feature-hidden-right-notice-action-btn></label></span></div><div class=setting-item><span>隐藏右侧领券操作按钮 </span><span><input aria-nock=right_coupon_action_btn id=feature-hidden-right-coupon-action-btn type=checkbox> <label for=feature-hidden-right-coupon-action-btn></label></span></div><div class=setting-item><span>隐藏右侧群二维码广告 </span><span><input aria-nock=right_qrcode_group_ad id=feature-hidden-right-qrcode-group-ad type=checkbox> <label for=feature-hidden-right-qrcode-group-ad></label></span></div><div class=setting-item><span>隐藏右侧图片广告 </span><span><input aria-nock=right_pic_ad id=feature-hidden-right-pic-ad type=checkbox> <label for=feature-hidden-right-pic-ad></label></span></div><div class=setting-item><span>隐藏右侧相关产品和服务 </span><span><input aria-nock=right_related_product_and_service id=feature-hidden-right-related-product-and-service type=checkbox> <label for=feature-hidden-right-related-product-and-service></label></span></div><div class=setting-item><span>隐藏右侧加入讨论 </span><span><input aria-nock=right_discussion_card id=feature-hidden-right-discussion-card type=checkbox> <label for=feature-hidden-right-discussion-card></label></span></div><div class=setting-item><span>隐藏右侧相关课程 </span><span><input aria-nock=right_related_lesson_card id=feature-hidden-right-related-lesson-card type=checkbox> <label for=feature-hidden-right-related-lesson-card></label></span></div><div class=setting-item><span>隐藏界面左下角弹出卡片 </span><span><input aria-nock=left_corner_bubble_card id=feature-hidden-left-corner-bubble-card type=checkbox> <label for=feature-hidden-left-corner-bubble-card></label></span></div></div></div></div>`;
  92. 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: 99999; 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; }`;
  93.  
  94. const dStyle = document.createElement("style");
  95. dStyle.innerHTML = setStyle;
  96. document.head.appendChild(dStyle);
  97. const modal = document.createElement("div");
  98. modal.innerHTML = setModal;
  99. document.body.appendChild(modal);
  100.  
  101. const checkboxList = document.querySelectorAll(".setting-item input");
  102. Array.from(checkboxList).forEach((checkbox) => {
  103. const nock = checkbox.getAttribute("aria-nock");
  104. checkbox.checked = GM_getValue(nock, true);
  105. });
  106.  
  107. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  108. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  109.  
  110. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  111. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  112. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  113. document.querySelector(".modal-body").addEventListener("click", (e) => {
  114. if (e.target.type !== "checkbox") {
  115. return;
  116. }
  117. const nock = e.target.getAttribute("aria-nock");
  118. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  119. GM_setValue(nock, flag);
  120. });
  121. GM_registerMenuCommand("功能设置", showSetting);
  122.  
  123. if (features.mark.datetime.enabled) {
  124. const datePublished = document.querySelector(features.mark.datetime.selector[0]);
  125. const dateModified = document.querySelector(features.mark.datetime.selector[1]).parentElement;
  126.  
  127. const regex = /\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}/g;
  128. let rawPostTime = datePublished.getAttribute("content");
  129. let rawUpdateTime = dateModified.innerText.match(regex)[0];
  130.  
  131. [rawPostTime, rawUpdateTime] = [rawPostTime, rawUpdateTime].sort(
  132. (a, b) => new Date(a) - new Date(b)
  133. );
  134.  
  135. const postTimeAgo = calculateTimeAgo(rawPostTime);
  136. const updateTimeAgo = calculateTimeAgo(rawUpdateTime);
  137.  
  138. const formattedPostTime = `发布于 ${rawPostTime}(${postTimeAgo})`;
  139. const formattedUpdateTime = `编辑于 ${rawUpdateTime}(${updateTimeAgo})`;
  140.  
  141. const timeBlock = document.createElement("span");
  142. timeBlock.innerText = formattedPostTime;
  143. timeBlock.style.fontSize = "12px";
  144.  
  145. dateModified.innerHTML = `<span style="display:flex;align-items:center">${features.mark.datetime.icon}</span>`;
  146. dateModified.appendChild(timeBlock);
  147. dateModified.style.textDecoration = "underline";
  148. dateModified.style.display = "flex";
  149. dateModified.style.alignItems = "center";
  150.  
  151. dateModified.addEventListener("click", () => {
  152. const text =
  153. timeBlock.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime;
  154. timeBlock.innerHTML = text;
  155. });
  156.  
  157. const icon = dateModified.querySelector("svg");
  158. icon.style.width = "12px";
  159. icon.style.height = "12px";
  160. icon.style.marginRight = "5px";
  161. }
  162.  
  163. if (features.mark.readtime.enabled) {
  164. const readbox = document.createElement("span");
  165. readbox.style.display = "flex";
  166. readbox.style.alignItems = "center";
  167. readbox.style.marginLeft = "24px";
  168.  
  169. const article = document.querySelector(features.mark.readtime.selector[0]);
  170.  
  171. const textCount = article.innerText.length;
  172. const readtime = textCount / 400;
  173. if (readtime >= 1440) {
  174. const days = Math.floor(readtime / 1440);
  175. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${days} 天</span>`;
  176. }
  177.  
  178. if (readtime >= 60) {
  179. const hours = Math.floor(readtime / 60);
  180. const minutes = Math.floor(readtime % 60);
  181. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</span>`;
  182. }
  183.  
  184. if (readtime >= 1) {
  185. const minutes = Math.round(readtime);
  186. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${minutes} 分钟</span>`;
  187. }
  188.  
  189. if (readtime < 1) {
  190. const seconds = Math.round(readtime * 60);
  191. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${seconds} 秒</span>`;
  192. }
  193. const icon = readbox.querySelector("svg");
  194. icon.style.width = "12px";
  195. icon.style.height = "12px";
  196. icon.style.marginRight = "5px";
  197. readbox.style.fontSize = "12px";
  198. readbox.style.cursor = "pointer";
  199. readbox.style.color = "#4b5b76";
  200. readbox.style.textDecoration = "underline";
  201. readbox.addEventListener("click", () => article.scrollIntoView(false));
  202.  
  203. const toolbar = document.querySelector(features.mark.readtime.selector[1]).parentElement
  204. .parentElement;
  205. toolbar.appendChild(readbox);
  206. }
  207.  
  208. if (features.sleep.left_emoji_animation.enabled) {
  209. const animationStyle = document.createElement("style");
  210. document.head.appendChild(animationStyle);
  211.  
  212. const cssRule = `${features.sleep.left_emoji_animation.selector[0]} { animation : none !important; }`;
  213. animationStyle.sheet.insertRule(cssRule);
  214. }
  215.  
  216. const hiddenStyle = document.createElement("style");
  217. document.head.appendChild(hiddenStyle);
  218.  
  219. Object.entries(features.hidden).forEach(([key, option]) => {
  220. if (option.enabled) {
  221. const cssRule = `${option.selector} { display: none !important; }`;
  222. hiddenStyle.sheet.insertRule(cssRule);
  223. }
  224. });
  225.  
  226. function calculateTimeAgo(datetime) {
  227. const SECOND = 1000;
  228. const MINUTE = 60 * SECOND;
  229. const HOUR = 60 * MINUTE;
  230. const DAY = 24 * HOUR;
  231. const MONTH = 30 * DAY;
  232. const YEAR = 12 * MONTH;
  233.  
  234. const postDate = new Date(datetime);
  235. const currentDate = new Date();
  236. const timeDiff = currentDate - postDate;
  237.  
  238. if (timeDiff >= YEAR) {
  239. return Math.floor(timeDiff / YEAR) + " 年前";
  240. }
  241. if (timeDiff >= MONTH) {
  242. return Math.floor(timeDiff / MONTH) + " 个月前";
  243. }
  244. if (timeDiff >= DAY) {
  245. return Math.floor(timeDiff / DAY) + " 天前";
  246. }
  247. if (timeDiff >= HOUR) {
  248. return Math.floor(timeDiff / HOUR) + " 小时前";
  249. }
  250. if (timeDiff >= MINUTE) {
  251. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  252. }
  253. return Math.floor(timeDiff / SECOND) + " 秒前";
  254. }
  255. })();