TencentCloudDeveloperNock

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

当前为 2024-06-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TencentCloudDeveloperNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.2
  5. // @description BlogNock 系列,腾讯云开发者社区文章的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match cloud.tencent.com/developer/article/*
  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: [".mod-header__date .date-text"],
  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: [".mod-content", ".mod-header__detail"],
  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. hidden: {
  31. day_loading_activity_modal: {
  32. enabled: GM_getValue("day_loading_activity_modal", true),
  33. selector: [
  34. ".cdc-portal-wrapper:has(.mod-activity)",
  35. ".cdc-portal-wrapper:has(.cdc-activity-modal)",
  36. ],
  37. },
  38. top_activity_ad_pic: {
  39. enabled: GM_getValue("top_activity_ad_pic", true),
  40. selector: [".cdc-header__capsule"],
  41. },
  42. right_notice_action_btn: {
  43. enabled: GM_getValue("right_notice_action_btn", true),
  44. selector: [".cdc-widget-global:has(.announcement)", "#tea-overlay-root"],
  45. },
  46. right_coupon_action_btn: {
  47. enabled: GM_getValue("right_coupon_action_btn", true),
  48. selector: [".cdc-widget-global__btn:has(.cdc-widget-global__btn-tag)"],
  49. },
  50. right_qrcode_group_ad: {
  51. enabled: GM_getValue("right_qrcode_group_ad", true),
  52. selector: [".cdc-commercial-card.mod-group-card"],
  53. },
  54. right_pic_ad: {
  55. enabled: GM_getValue("right_pic_ad", true),
  56. selector: [".cdc-commercial-card:has(.cdc-commercial-card__img)"],
  57. },
  58. right_related_product_and_service: {
  59. enabled: GM_getValue("right_related_product_and_service", true),
  60. selector: [".cdc-mod-product2:has(.cdc-product-info2__list)"],
  61. },
  62. right_discussion_card: {
  63. enabled: GM_getValue("right_discussion_card", true),
  64. selector: [".cdc-discussion-card:has(.cdc-discussion-card__recommend)"],
  65. },
  66. right_related_lesson_card: {
  67. enabled: GM_getValue("right_related_lesson_card", true),
  68. selector: [".cdc-free-course-card:has(.cdc-free-course-card__detail)"],
  69. },
  70. },
  71. };
  72.  
  73. 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> <hr /> <div class="setting-item"> <span> 隐藏每日活动提示的模态框 </span> <span> <input type="checkbox" id="feature-hidden-day-loading-activity-modal" aria-nock="day_loading_activity_modal" /> <label for="feature-hidden-day-loading-activity-modal"></label> </span> </div> <div class="setting-item"> <span> 隐藏顶部活动广告图片 </span> <span> <input type="checkbox" id="feature-hidden-top-activity-ad-pic" aria-nock="top_activity_ad_pic" /> <label for="feature-hidden-top-activity-ad-pic"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧公告提示操作按钮 </span> <span> <input type="checkbox" id="feature-hidden-right-notice-action-btn" aria-nock="right_notice_action_btn" /> <label for="feature-hidden-right-notice-action-btn"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧领券操作按钮 </span> <span> <input type="checkbox" id="feature-hidden-right-coupon-action-btn" aria-nock="right_coupon_action_btn" /> <label for="feature-hidden-right-coupon-action-btn"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧群二维码广告 </span> <span> <input type="checkbox" id="feature-hidden-right-qrcode-group-ad" aria-nock="right_qrcode_group_ad" /> <label for="feature-hidden-right-qrcode-group-ad"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧图片广告 </span> <span> <input type="checkbox" id="feature-hidden-right-pic-ad" aria-nock="right_pic_ad" /> <label for="feature-hidden-right-pic-ad"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧相关产品和服务 </span> <span> <input type="checkbox" id="feature-hidden-right-related-product-and-service" aria-nock="right_related_product_and_service" /> <label for="feature-hidden-right-related-product-and-service"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧加入讨论 </span> <span> <input type="checkbox" id="feature-hidden-right-discussion-card" aria-nock="right_discussion_card" /> <label for="feature-hidden-right-discussion-card"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧相关课程 </span> <span> <input type="checkbox" id="feature-hidden-right-related-lesson-card" aria-nock="right_related_lesson_card" /> <label for="feature-hidden-right-related-lesson-card"></label> </span> </div> </div> </div> </div>`;
  74. 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; }`;
  75.  
  76. const dStyle = document.createElement("style");
  77. dStyle.innerHTML = setStyle;
  78. document.head.appendChild(dStyle);
  79. const modal = document.createElement("div");
  80. modal.innerHTML = setModal;
  81. document.body.appendChild(modal);
  82.  
  83. const checkboxList = document.querySelectorAll(".setting-item input");
  84. Array.from(checkboxList).forEach((checkbox) => {
  85. const nock = checkbox.getAttribute("aria-nock");
  86. checkbox.checked = GM_getValue(nock, true);
  87. });
  88.  
  89. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  90. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  91.  
  92. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  93. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  94. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  95. document.querySelector(".modal-body").addEventListener("click", (e) => {
  96. if (e.target.type !== "checkbox") {
  97. return;
  98. }
  99. const nock = e.target.getAttribute("aria-nock");
  100. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  101. GM_setValue(nock, flag);
  102. });
  103. GM_registerMenuCommand("功能设置", showSetting);
  104.  
  105. if (features.mark.datetime.enabled) {
  106. const datePublished = document.querySelector(features.mark.datetime.selector[0]);
  107. const regex = /\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}/g;
  108. const postTime = datePublished.innerText.match(regex)[0];
  109.  
  110. const postTimeAgo = calculateTimeAgo(postTime);
  111. const formattedPostTime = `发布于 ${postTime}(${postTimeAgo})`;
  112.  
  113. const timeBlock = document.createElement("span");
  114. timeBlock.innerText = formattedPostTime;
  115.  
  116. datePublished.innerHTML = `<span style="display:flex;align-items:center">${features.mark.datetime.icon}</span>`;
  117. datePublished.appendChild(timeBlock);
  118. datePublished.style.textDecoration = "underline";
  119. datePublished.style.display = "flex";
  120. datePublished.style.alignItems = "center";
  121.  
  122. const icon = datePublished.querySelector("svg");
  123. icon.style.width = "14px";
  124. icon.style.height = "14px";
  125. icon.style.marginRight = "5px";
  126. }
  127.  
  128. if (features.mark.readtime.enabled) {
  129. const readbox = document.createElement("span");
  130. readbox.style.display = "flex";
  131. readbox.style.alignItems = "center";
  132. readbox.style.marginLeft = "24px";
  133.  
  134. const article = document.querySelector(features.mark.readtime.selector[0]);
  135.  
  136. const textCount = article.innerText.length;
  137. const readtime = textCount / 400;
  138. if (readtime >= 1440) {
  139. const days = Math.floor(readtime / 1440);
  140. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${days} 天</span>`;
  141. }
  142.  
  143. if (readtime >= 60) {
  144. const hours = Math.floor(readtime / 60);
  145. const minutes = Math.floor(readtime % 60);
  146. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</span>`;
  147. }
  148.  
  149. if (readtime >= 1) {
  150. const minutes = Math.round(readtime);
  151. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${minutes} 分钟</span>`;
  152. }
  153.  
  154. if (readtime < 1) {
  155. const seconds = Math.round(readtime * 60);
  156. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${seconds} 秒</span>`;
  157. }
  158. const icon = readbox.querySelector("svg");
  159. icon.style.width = "12px";
  160. icon.style.height = "12px";
  161. icon.style.marginRight = "5px";
  162. readbox.style.fontSize = "12px";
  163. readbox.style.cursor = "pointer";
  164. readbox.style.color = "#4b5b76";
  165. readbox.style.textDecoration = "underline";
  166. readbox.addEventListener("click", () => article.scrollIntoView(false));
  167. document.querySelector(features.mark.readtime.selector[1]).appendChild(readbox);
  168. }
  169.  
  170. const hiddenStyle = document.createElement("style");
  171. document.head.appendChild(hiddenStyle);
  172.  
  173. Object.entries(features.hidden).forEach(([key, option]) => {
  174. if (option.enabled) {
  175. const cssRule = `${option.selector} { display: none !important; }`;
  176. hiddenStyle.sheet.insertRule(cssRule);
  177. }
  178. });
  179.  
  180. function calculateTimeAgo(datetime) {
  181. const SECOND = 1000;
  182. const MINUTE = 60 * SECOND;
  183. const HOUR = 60 * MINUTE;
  184. const DAY = 24 * HOUR;
  185. const MONTH = 30 * DAY;
  186. const YEAR = 12 * MONTH;
  187.  
  188. const postDate = new Date(datetime);
  189. const currentDate = new Date();
  190. const timeDiff = currentDate - postDate;
  191.  
  192. if (timeDiff >= YEAR) {
  193. return Math.floor(timeDiff / YEAR) + " 年前";
  194. }
  195. if (timeDiff >= MONTH) {
  196. return Math.floor(timeDiff / MONTH) + " 个月前";
  197. }
  198. if (timeDiff >= DAY) {
  199. return Math.floor(timeDiff / DAY) + " 天前";
  200. }
  201. if (timeDiff >= HOUR) {
  202. return Math.floor(timeDiff / HOUR) + " 小时前";
  203. }
  204. if (timeDiff >= MINUTE) {
  205. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  206. }
  207. return Math.floor(timeDiff / SECOND) + " 秒前";
  208. }
  209. })();

QingJ © 2025

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