OSChinaNock

BlogNock 系列,开源中国(OSChina)文章的标识优化

目前为 2024-05-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name OSChinaNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.2
  5. // @description BlogNock 系列,开源中国(OSChina)文章的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match my.oschina.net/*/blog/*
  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: ["meta[property='bytedance:published_time']", ".article-box__meta div.item.lm"],
  22. },
  23. readtime: {
  24. enabled: GM_getValue("readtime", true),
  25. selector: [".article-detail", ".article-box__meta .item-list"],
  26. },
  27. },
  28. hidden: {
  29. right_sidebar_top_pic_ad: {
  30. enabled: GM_getValue("right_sidebar_top_pic_ad", true),
  31. selector: [".sidebar-box__inner .ad-box:has(a[name='blog_detail_right_sidebar_1'])"],
  32. },
  33. right_sidebar_popular_info: {
  34. enabled: GM_getValue("right_popular_info", true),
  35. selector: [
  36. ".sidebar-section-list .sidebar-section-list--compact:has(.new-other-list-news)",
  37. ],
  38. },
  39. right_sidebar_recommended_user: {
  40. enabled: GM_getValue("right_sidebar_recommended_user", true),
  41. selector: [
  42. ".sidebar-section-list .sidebar-section-list--compact:has(.section-recommend-blog)",
  43. ],
  44. },
  45. right_sidebar_popular_software: {
  46. enabled: GM_getValue("right_sidebar_popular_software", true),
  47. selector: [
  48. ".sidebar-section-list .sidebar-section-list--compact:has(.new-other-list-blog)",
  49. ],
  50. },
  51. right_sidebar_bottom_pic_ad: {
  52. enabled: GM_getValue("right_sidebar_bottom_pic_ad", true),
  53. selector: [".sidebar-box__inner .ad-box:has(a[name='blog_detail_right_sidebar_3'])"],
  54. },
  55. right_qrcode_action_btn: {
  56. enabled: GM_getValue("right_qrcode_action_btn", true),
  57. selector: [".pusher .codeBlock"],
  58. },
  59. bottom_popular_content: {
  60. enabled: GM_getValue("bottom_popular_content", true),
  61. selector: [".other-articles-box__inner:has(.other-articles-box__content .item)"],
  62. },
  63. bottom_global_popular_comment: {
  64. enabled: GM_getValue("bottom_global_popular_comment", true),
  65. selector: [".other-articles-box__inner:has(.article-hot-list-box)"],
  66. },
  67. },
  68. };
  69.  
  70. 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-right-sidebar-top-pic-ad" aria-nock="right_sidebar_top_pic_ad" /> <label for="feature-hidden-right-sidebar-top-pic-ad"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧底部图片广告 </span> <span> <input type="checkbox" id="feature-hidden-right-sidebar-bottom-pic-ad" aria-nock="right_sidebar_bottom_pic_ad" /> <label for="feature-hidden-right-sidebar-bottom-pic-ad"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧热门资讯 </span> <span> <input type="checkbox" id="feature-hidden-right-popular-info" aria-nock="right_popular_info" /> <label for="feature-hidden-right-popular-info"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧推荐关注 </span> <span> <input type="checkbox" id="feature-hidden-right-sidebar-recommended-user" aria-nock="right_sidebar_recommended_user" /> <label for="feature-hidden-right-sidebar-recommended-user"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧热门软件 </span> <span> <input type="checkbox" id="feature-hidden-right-sidebar-popular-software" aria-nock="right_sidebar_popular_software" /> <label for="feature-hidden-right-sidebar-popular-software"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧二维码操作按钮 </span> <span> <input type="checkbox" id="feature-hidden-right-qrcode-action-btn" aria-nock="right_qrcode_action_btn" /> <label for="feature-hidden-right-qrcode-action-btn"></label> </span> </div> <div class="setting-item"> <span> 隐藏文章底部热门内容 </span> <span> <input type="checkbox" id="feature-hidden-bottom-popular-content" aria-nock="bottom_popular_content" /> <label for="feature-hidden-bottom-popular-content"></label> </span> </div> <div class="setting-item"> <span> 隐藏文章底部全站热门评论 </span> <span> <input type="checkbox" id="feature-hidden-bottom-global-popular-comment" aria-nock="bottom_global_popular_comment" /> <label for="feature-hidden-bottom-global-popular-comment"></label> </span> </div> </div> </div> </div>`;
  71. 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; }`;
  72.  
  73. const dStyle = document.createElement("style");
  74. dStyle.innerHTML = setStyle;
  75. document.head.appendChild(dStyle);
  76. const modal = document.createElement("div");
  77. modal.innerHTML = setModal;
  78. document.body.appendChild(modal);
  79.  
  80. const checkboxList = document.querySelectorAll(".setting-item input");
  81. Array.from(checkboxList).forEach((checkbox) => {
  82. const nock = checkbox.getAttribute("aria-nock");
  83. checkbox.checked = GM_getValue(nock, true);
  84. });
  85.  
  86. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  87. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  88.  
  89. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  90. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  91. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  92. document.querySelector(".modal-body").addEventListener("click", (e) => {
  93. if (e.target.type !== "checkbox") {
  94. return;
  95. }
  96. const nock = e.target.getAttribute("aria-nock");
  97. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  98. GM_setValue(nock, flag);
  99. });
  100. GM_registerMenuCommand("功能设置", showSetting);
  101.  
  102. if (features.mark.datetime.enabled) {
  103. const publishedTime = document.querySelector(features.mark.datetime.selector[0]);
  104. let postTime = publishedTime.getAttribute("content");
  105. postTime = new Date(postTime).toLocaleString().replaceAll("/", "-");
  106. const postTimeAgo = calculateTimeAgo(postTime);
  107. const formattedPostTime = `发布于 ${postTime}(${postTimeAgo})`;
  108.  
  109. const postTimeItem = document.querySelector(features.mark.datetime.selector[1]);
  110. postTimeItem.innerText = formattedPostTime;
  111. postTimeItem.style.textDecoration = "underline";
  112. }
  113.  
  114. if (features.mark.readtime.enabled) {
  115. const readbox = document.createElement("div");
  116. const article = document.querySelector(features.mark.readtime.selector[0]);
  117. const textCount = article.innerText.length;
  118. const readtime = textCount / 400;
  119. if (readtime >= 1440) {
  120. const days = Math.floor(readtime / 1440);
  121. readbox.innerHTML = `<span> 预计阅读时长 ${days} 天</span>`;
  122. }
  123. if (readtime >= 60) {
  124. const hours = Math.floor(readtime / 60);
  125. const minutes = Math.floor(readtime % 60);
  126. readbox.innerHTML = `<span> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</sp>`;
  127. }
  128. if (readtime >= 1) {
  129. const minutes = Math.round(readtime);
  130. readbox.innerHTML = `<span> 预计阅读时长 ${minutes} 分钟</sp>`;
  131. }
  132. if (readtime < 1) {
  133. const seconds = Math.round(readtime * 60);
  134. readbox.innerHTML = `<span> 预计阅读时长 ${seconds} 秒</span>`;
  135. }
  136. readbox.style.fontSize = "14px";
  137. readbox.style.cursor = "pointer";
  138. readbox.style.textDecoration = "underline";
  139. readbox.addEventListener("click", () => article.scrollIntoView(false));
  140. document.querySelector(features.mark.readtime.selector[1]).appendChild(readbox);
  141. }
  142.  
  143. const hiddenStyle = document.createElement("style");
  144. document.head.appendChild(hiddenStyle);
  145.  
  146. Object.entries(features.hidden).forEach(([key, option]) => {
  147. if (option.enabled) {
  148. const cssRule = `${option.selector} { display: none !important; }`;
  149. hiddenStyle.sheet.insertRule(cssRule);
  150. }
  151. });
  152.  
  153. function calculateTimeAgo(datetime) {
  154. const SECOND = 1000;
  155. const MINUTE = 60 * SECOND;
  156. const HOUR = 60 * MINUTE;
  157. const DAY = 24 * HOUR;
  158. const MONTH = 30 * DAY;
  159. const YEAR = 12 * MONTH;
  160.  
  161. const postDate = new Date(datetime);
  162. const currentDate = new Date();
  163. const timeDiff = currentDate - postDate;
  164.  
  165. if (timeDiff >= YEAR) {
  166. return Math.floor(timeDiff / YEAR) + " 年前";
  167. }
  168. if (timeDiff >= MONTH) {
  169. return Math.floor(timeDiff / MONTH) + " 个月前";
  170. }
  171. if (timeDiff >= DAY) {
  172. return Math.floor(timeDiff / DAY) + " 天前";
  173. }
  174. if (timeDiff >= HOUR) {
  175. return Math.floor(timeDiff / HOUR) + " 小时前";
  176. }
  177. if (timeDiff >= MINUTE) {
  178. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  179. }
  180. return Math.floor(timeDiff / SECOND) + " 秒前";
  181. }
  182. })();