CSDNock

BlogNock系列,CSDN文章的标识优化

  1. // ==UserScript==
  2. // @name CSDNock
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.14
  5. // @icon https://raw.githubusercontent.com/Exisi/BlogNock/main/doc/icon/nock.ico
  6. // @description BlogNock系列,CSDN文章的标识优化
  7. // @author Exisi
  8. // @license MIT License
  9. // @match *://blog.csdn.net/*/article/details/*
  10. // @match *://blog.csdn.net/article/details/*
  11. // @supportURL https://github.com/Exisi/BlogNock/issues/
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. "use strict";
  19. const features = {
  20. recommend_type_download: {
  21. debug: false,
  22. enabled: GM_getValue("recommend_type_download", true),
  23. selector: ".recommend-item-box.type_download.clearfix",
  24. },
  25. mark: {
  26. copyright: {
  27. debug: false,
  28. enabled: GM_getValue("copyright", true),
  29. selector: [".article-type-img", ".article-info-box", ".article-source-link a"],
  30. original: "https://scriptcat.org/api/v2/resource/image/5ts345bqYL3F3Hd8",
  31. reprint: "https://scriptcat.org/api/v2/resource/image/4jdyz4euyOHjSPcQ",
  32. translate: "https://scriptcat.org/api/v2/resource/image/E7KmWSWcxesn9RrL",
  33. },
  34. datetime: {
  35. debug: false,
  36. enabled: GM_getValue("datetime", true),
  37. selector: [".time", ".up-time"],
  38. },
  39. readtime: {
  40. debug: false,
  41. enabled: GM_getValue("readtime", true),
  42. selector: ["#content_views", ".bar-content", ".blog-content-box"],
  43. 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="M360 0H24C10.7 0 0 10.7 0 24v16c0 13.3 10.7 24 24 24 0 91 51 167.7 120.8 192C75 280.3 24 357 24 448c-13.3 0-24 10.7-24 24v16c0 13.3 10.7 24 24 24h336c13.3 0 24-10.7 24-24v-16c0-13.3-10.7-24-24-24 0-91-51-167.7-120.8-192C309 231.7 360 155 360 64c13.3 0 24-10.7 24-24V24c0-13.3-10.7-24-24-24zm-64 448H88c0-77.5 46.2-144 104-144 57.8 0 104 66.5 104 144z"/></svg>`,
  44. },
  45. },
  46. source_redirct: {
  47. debug: false,
  48. enabled: GM_getValue("source_redirct", true),
  49. selector: [".href-article-edit", ".article-type-img", ".article-source-link a"],
  50. },
  51. unfixed_comment: {
  52. debug: false,
  53. enabled: GM_getValue("fixed_comment", true),
  54. selector: [".left-toolbox"],
  55. },
  56. hidden_login: {
  57. debug: false,
  58. enabled: GM_getValue("hidden_login", true),
  59. selector: [".hasAvatar", ".passport-login-container", ".passport-login-box>img"],
  60. },
  61. allow_copy: {
  62. debug: false,
  63. enabled: GM_getValue("allow_copy", true),
  64. selector: ["#content_views", "pre", "code"],
  65. },
  66. unfold_code: {
  67. debug: false,
  68. enabled: GM_getValue("unfold_code", true),
  69. selector: [".hide-preCode-bt"],
  70. },
  71. allow_copy_with_btn: {
  72. debug: false,
  73. enabled: GM_getValue("allow_copy_with_btn", true),
  74. selector: [".hljs-button.signin.active", "button.btn-code-notes.mdeditor", "code"],
  75. },
  76. hidden_sidebar: {
  77. debug: false,
  78. enabled: GM_getValue("hidden_sidebar", true),
  79. selector: [".blog_container_aside", "#mainBox"],
  80. },
  81. hidden: {
  82. ai_search_btn: {
  83. enabled: GM_getValue("ai_search_btn", true),
  84. selector: ["#toolbar-c-box-button.c-box"],
  85. },
  86. article_search_tip: {
  87. enabled: GM_getValue("article_search_tip", true),
  88. selector: ["#articleSearchTip"],
  89. },
  90. nav_vip_promotion_pic: {
  91. enabled: GM_getValue("nav_vip_promotion_pic", true),
  92. selector: [".toolbar-btn-vip img[src^='https://i-operation.csdnimg.cn']"],
  93. },
  94. login_tips: {
  95. enabled: GM_getValue("login_tips", true),
  96. selector: [".passport-login-tip-container"],
  97. },
  98. collection_tips: {
  99. enabled: GM_getValue("collection_tips", true),
  100. selector: [".tool-active-list", "#tool-active-list-collection"],
  101. },
  102. side_toolbar: {
  103. enabled: GM_getValue("side_toolbar", true),
  104. selector: [".csdn-side-toolbar"],
  105. },
  106. side_google_ad: {
  107. enabled: GM_getValue("side_google_ad", true),
  108. selector: [".box-shadow.mb8", "#footerRightAds"],
  109. },
  110. side_ai_ad: {
  111. enabled: GM_getValue("side_ai_ad", true),
  112. selector: ["#swiper-remuneration-container"],
  113. },
  114. write_guide_pic: {
  115. enabled: GM_getValue("write_guide_pic", true),
  116. selector: ["#asideWriteGuide"],
  117. },
  118. recommend_vote: {
  119. enabled: GM_getValue("recommend_vote", true),
  120. selector: ["#asideNewNps"],
  121. },
  122. latest_comments: {
  123. enabled: GM_getValue("latest_comment", true),
  124. selector: ["#asideNewComments"],
  125. },
  126. bottom_recommend_article_vote: {
  127. enabled: GM_getValue("bottom_recommend_article_vote", true),
  128. selector: ["#recommendNps"],
  129. },
  130. },
  131. };
  132.  
  133. class MenuHandler {
  134. constructor() {
  135. 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-copyright"aria-nock="copyright"/><label for="feature-mark-copyright"></label></span></div><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>移除底部推荐的CSDN下载</span><span><input type="checkbox"id="feature-recommend-type-download"aria-nock="recommend_type_download"/><label for="feature-recommend-type-download"></label></span></div><div class="setting-item"><span>自动转载原链重定向</span><span><input type="checkbox"id="feature-source-redirct"aria-nock="source_redirct"/><label for="feature-source-redirct"></label></span></div><div class="setting-item"><span>取消固定文章工具栏</span><span><input type="checkbox"id="feature-unfixed-comment"aria-nock="unfixed_comment"/><label for="feature-unfixed-comment"></label></span></div><div class="setting-item"><span>关闭界面加载后的登录(不可用)模态框</span><span><input type="checkbox"id="feature-hidden-login"aria-nock="hidden_login"/><label for="feature-hidden-login"></label></span></div><hr/><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-unfold-code"aria-nock="unfold_code"/><label for="feature-unfold-code"></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>隐藏AI搜索按钮</span><span><input type="checkbox"id="feature-hidden-ai-search-btn"aria-nock="ai_search_btn"/><label for="feature-hidden-ai-search-btn"></label></span></div><div class="setting-item"><span>隐藏顶部会员促销</span><span><input type="checkbox"id="feature-hidden-nav-vip-promotion-pic"aria-nock="nav_vip_promotion_pic"/><label for="feature-hidden-nav-vip-promotion-pic"></label></span></div><div class="setting-item"><span>隐藏文本复制的工具栏</span><span><input type="checkbox"id="feature-hidden-article-search-tip"aria-nock="article_search_tip"/><label for="feature-hidden-article-search-tip"></label></span></div><div class="setting-item"><span>隐藏右侧工具栏</span><span><input type="checkbox"id="feature-hidden-side-toolbar"aria-nock="side_toolbar"/><label for="feature-hidden-side-toolbar"></label></span></div><div class="setting-item"><span>隐藏登录(不可用)提示</span><span><input type="checkbox"id="feature-hidden-login-tips"aria-nock="login_tips"/><label for="feature-hidden-login-tips"></label></span></div><div class="setting-item"><span>隐藏收藏提示</span><span><input type="checkbox"id="feature-hidden-collection-tips"aria-nock="collection_tips"/><label for="feature-hidden-collection-tips"></label></span></div><div class="setting-item"><span>隐藏底部文章推荐评分</span><span><input type="checkbox"id="feature-hidden-bottom-recommend-article-vote"aria-nock="bottom_recommend_article_vote"/><label for="feature-hidden-bottom-recommend-article-vote"></label></span></div><hr/><div class="setting-item"><span>隐藏左侧侧边栏</span><span><input type="checkbox"id="feature-hidden-sidebar"aria-nock="hidden_sidebar"/><label for="feature-hidden-sidebar"></label></span></div><div class="setting-item"><span>隐藏左侧活动图片</span><span><input type="checkbox"id="feature-hidden-write-guide-pic"aria-nock="write_guide_pic"/><label for="feature-hidden-write-guide-pic"></label></span></div><div class="setting-item"><span>隐藏左侧AI活动图片</span><span><input type="checkbox"id="feature-hidden-write-side-ai-ad"aria-nock="side_ai_ad"/><label for="feature-hidden-write-side-ai-ad"></label></span></div><div class="setting-item"><span>隐藏左侧谷歌广告</span><span><input type="checkbox"id="feature-hidden-side-google-ad"aria-nock="side_google_ad"/><label for="feature-hidden-side-google-ad"></label></span></div><div class="setting-item"><span>隐藏左侧推荐评分</span><span><input type="checkbox"id="feature-hidden-recommend-vote"aria-nock="recommend_vote"/><label for="feature-hidden-recommend-vote"></label></span></div><div class="setting-item"><span>隐藏左侧最新评论</span><span><input type="checkbox"id="feature-hidden-latest-comment"aria-nock="latest_comment"/><label for="feature-hidden-latest-comment"></label></span></div></div></div></div>`;
  136. 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; height: 90%; overflow: scroll; 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; }`;
  137.  
  138. const dStyle = document.createElement("style");
  139. dStyle.innerHTML = setStyle;
  140. document.head.appendChild(dStyle);
  141. const modal = document.createElement("div");
  142. modal.innerHTML = setModal;
  143. document.body.appendChild(modal);
  144.  
  145. this.bindEvents();
  146. this.registerContextMenu();
  147. }
  148.  
  149. bindEvents() {
  150. const checkboxList = document.querySelectorAll(".setting-item input");
  151. Array.from(checkboxList).forEach((checkbox) => {
  152. const nock = checkbox.getAttribute("aria-nock");
  153. checkbox.checked = GM_getValue(nock, true);
  154. });
  155.  
  156. document.querySelector(".modal-dialog").addEventListener("click", this.closeSetting);
  157. document.querySelector(".btn-dialog-close").addEventListener("click", this.closeSetting);
  158. document.querySelector(".modal-body").addEventListener("click", (e) => {
  159. if (e.target.type !== "checkbox") {
  160. return;
  161. }
  162. const nock = e.target.getAttribute("aria-nock");
  163. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  164. GM_setValue(nock, flag);
  165. });
  166. }
  167.  
  168. showSetting() {
  169. document.querySelector(".modal-dialog").style.display = "block";
  170. }
  171.  
  172. closeSetting() {
  173. document.querySelector(".modal-dialog").style.display = "none";
  174. }
  175.  
  176. registerContextMenu() {
  177. GM_registerMenuCommand("功能设置", this.showSetting.bind(this));
  178. }
  179. }
  180.  
  181. const menuHandler = new MenuHandler();
  182.  
  183. class ErrorHandler {
  184. constructor(customConfig = {}) {
  185. const debugConfig = {
  186. global: false,
  187. };
  188.  
  189. // Merge custom configuration
  190. this.debugConfig = { ...debugConfig, ...customConfig };
  191. }
  192.  
  193. /**
  194. * Update configuration dynamically
  195. * @param {Object} newConfig - New configuration
  196. */
  197. setConfig(customConfig = {}) {
  198. this.debugConfig = { ...this.debugConfig, ...customConfig };
  199. }
  200.  
  201. /**
  202. * Unified error handling function
  203. * @param {Error} error - Error object
  204. * @param {string} functionName - Function name
  205. */
  206. handleError(error, functionName) {
  207. const isDebugMode = this.debugConfig[functionName] || this.debugConfig.global;
  208. // Re-throw error in debug mode
  209. if (isDebugMode) {
  210. console.error(`[DEBUG] Error in ${functionName}:`, error);
  211. throw error;
  212. } else {
  213. console.warn(`[WARNING] Error in ${functionName}:`, error.message);
  214. }
  215. }
  216.  
  217. /**
  218. * Safe execution wrapper function
  219. * @param {string} functionName - Function name
  220. * @param {Function} fn - Function to execute
  221. * @returns {*} Function execution result or null
  222. */
  223. safeExecute(functionName, fn) {
  224. try {
  225. return fn();
  226. } catch (error) {
  227. this.handleError(error, functionName);
  228. return null;
  229. }
  230. }
  231.  
  232. /**
  233. * Collect debug configuration from features object
  234. * @param {Object} features - Features configuration object
  235. * @returns {Object} Debug configuration
  236. */
  237. collectDebug(features) {
  238. if (!features || typeof features !== "object") {
  239. return {};
  240. }
  241.  
  242. let customConfig = {};
  243.  
  244. function traverse(obj) {
  245. Object.keys(obj).forEach((key) => {
  246. if (!obj.hasOwnProperty(key)) {
  247. return;
  248. }
  249.  
  250. const value = obj[key];
  251. const currentKey = key;
  252.  
  253. const isObj = value && typeof value === "object";
  254. const isDebug = value.hasOwnProperty("debug") && value.debug;
  255.  
  256. if (isObj && isDebug) {
  257. customConfig[currentKey] = true;
  258. }
  259.  
  260. if (isObj && !Array.isArray(value)) {
  261. traverse(value, currentKey);
  262. }
  263. });
  264. }
  265.  
  266. traverse(features);
  267. return customConfig;
  268. }
  269. }
  270.  
  271. const errorNock = new ErrorHandler();
  272. const debugConfig = errorNock.collectDebug(features);
  273. errorNock.setConfig(debugConfig);
  274.  
  275. errorNock.safeExecute("recommend_type_download", () => {
  276. if (!features.recommend_type_download.enabled) {
  277. return;
  278. }
  279.  
  280. const downloadItemList = document.querySelectorAll(features.recommend_type_download.selector);
  281. Array.from(downloadItemList)
  282. .filter((item) => {
  283. const link = item.querySelector("a")?.href;
  284. return link && link.includes("download.csdn");
  285. })
  286. .forEach((item) => (item.style.display = "none"));
  287. });
  288.  
  289. errorNock.safeExecute("copyright", () => {
  290. if (!features.mark.copyright.enabled) {
  291. return;
  292. }
  293.  
  294. let copyright = document.querySelector(features.mark.copyright.selector[0]);
  295. const infoBox = document.querySelector(features.mark.copyright.selector[1]);
  296. const sourceUrl = document.querySelector(features.mark.copyright.selector[2]);
  297. copyright.style.visibility = "hidden";
  298.  
  299. const iconUrl = copyright.getAttribute("src");
  300. const type = iconUrl.substring(iconUrl.lastIndexOf("/") + 1, iconUrl.lastIndexOf("."));
  301. infoBox.innerHTML += `<a href=${sourceUrl}>
  302. <img src=${features.mark.copyright[type]} alt="${type}"
  303. style="width:43px; height:43px; position:absolute; top:7px; left:2px"/>
  304. </a>`;
  305. });
  306.  
  307. errorNock.safeExecute("datetime", () => {
  308. if (!features.mark.datetime.enabled) {
  309. return;
  310. }
  311.  
  312. const postTime = document.querySelector(features.mark.datetime.selector[0]);
  313. const updateTime = document.querySelector(features.mark.datetime.selector[1]) ?? postTime;
  314.  
  315. const regex = /\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}/g;
  316. let rawPostTime = postTime.innerText.match(regex)[0];
  317. let rawUpdateTime = updateTime.innerText.match(regex)[0];
  318.  
  319. [rawPostTime, rawUpdateTime] = [rawPostTime, rawUpdateTime].sort(
  320. (a, b) => new Date(a) - new Date(b)
  321. );
  322.  
  323. const postTimeAgo = calculateTimeAgo(rawPostTime);
  324. const updateTimeAgo = calculateTimeAgo(rawUpdateTime);
  325.  
  326. const formattedPostTime = `发布于 ${rawPostTime}(${postTimeAgo})`;
  327. const formattedUpdateTime = `编辑于 ${rawUpdateTime}(${updateTimeAgo})`;
  328.  
  329. postTime.style.cursor = "pointer";
  330. postTime.style.fontSize = "14px";
  331. postTime.style.textDecoration = "underline";
  332. postTime.innerText = formattedPostTime;
  333. postTime.setAttribute("data-time", rawPostTime);
  334. postTime.addEventListener("click", () => {
  335. const text =
  336. postTime.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime;
  337. postTime.innerHTML = text;
  338. });
  339. });
  340.  
  341. errorNock.safeExecute("readtime", () => {
  342. if (!features.mark.readtime.enabled) {
  343. return;
  344. }
  345.  
  346. const readbox = document.createElement("span");
  347. readbox.style.display = "flex";
  348. readbox.style.alignItems = "center";
  349. const textCount = document.querySelector(features.mark.readtime.selector[0]).innerText.length;
  350. const readtime = textCount / 400;
  351. if (readtime >= 1440) {
  352. const days = Math.floor(readtime / 1440);
  353. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${days} 天</span>`;
  354. }
  355.  
  356. if (readtime >= 60) {
  357. const hours = Math.floor(readtime / 60);
  358. const minutes = Math.floor(readtime % 60);
  359. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${hours} 小时 ${minutes} 分钟</span>`;
  360. }
  361.  
  362. if (readtime >= 1) {
  363. const minutes = Math.round(readtime);
  364. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${minutes} 分钟</span>`;
  365. }
  366.  
  367. if (readtime < 1) {
  368. const seconds = Math.round(readtime * 60);
  369. readbox.innerHTML = `${features.mark.readtime.icon}<span> 预计阅读时长 ${seconds} 秒</span>`;
  370. }
  371. const icon = readbox.querySelector("svg");
  372. icon.style.width = "14px";
  373. icon.style.height = "14px";
  374. icon.style.marginRight = "3px";
  375. readbox.style.fontSize = "14px";
  376. readbox.style.cursor = "pointer";
  377. readbox.style.textDecoration = "underline";
  378. readbox.addEventListener("click", () =>
  379. document.querySelector(features.mark.readtime.selector[2]).scrollIntoView(false)
  380. );
  381. document.querySelector(features.mark.readtime.selector[1]).appendChild(readbox);
  382. });
  383.  
  384. errorNock.safeExecute("source_redirct", () => {
  385. if (!features.source_redirct.enabled) {
  386. return;
  387. }
  388.  
  389. const editStatus = document.querySelector(features.source_redirct.selector[0]);
  390. const flag = editStatus.className !== features.source_redirct.selector[0].slice(1);
  391. if (flag) {
  392. const copyright = document.querySelector(features.source_redirct.selector[1]);
  393. const sourceUrl = document.querySelector(features.source_redirct.selector[2]);
  394. const iconUrl = copyright.getAttribute("src");
  395. const type = iconUrl.substring(iconUrl.lastIndexOf("/") + 1, iconUrl.lastIndexOf("."));
  396. if (type === "reprint") {
  397. const style = document.createElement("style");
  398. style.innerHTML = `
  399. @keyframes fadeIn {
  400. from { opacity: 0; transform: translateX(20px); }
  401. to { opacity: 1; transform: translateX(0); }
  402. }
  403. @keyframes fadeOut {
  404. from { opacity: 1; transform: translateX(0); }
  405. to { opacity: 0; transform: translateX(20px); }
  406. }
  407. `;
  408. document.head.appendChild(style);
  409.  
  410. const countdownTips = document.createElement("div");
  411. Object.assign(countdownTips.style, {
  412. position: "fixed",
  413. top: "10px",
  414. right: "10px",
  415. padding: "15px",
  416. backgroundColor: "rgba(0, 0, 0, 0.8)",
  417. color: "white",
  418. zIndex: "9999",
  419. borderRadius: "8px",
  420. boxShadow: "0 0 10px rgba(0, 0, 0, 0.5)",
  421. fontFamily: "Arial, sans-serif",
  422. animation: "fadeIn 0.3s ease-out",
  423. });
  424. countdownTips.innerHTML = `
  425. <p style="margin: 0; font-size: 14px;">检测到源链接,<span id="countdown">5</span> 秒后跳转。</p>
  426. <button id="cancelButton" style="margin-top: 10px; padding: 5px 10px; background-color: #00a4ff; color: white; border: none; border-radius: 5px; cursor: pointer;">取消</button>
  427. `;
  428. document.body.appendChild(countdownTips);
  429.  
  430. const progressBarContainer = document.createElement("div");
  431. Object.assign(progressBarContainer.style, {
  432. position: "fixed",
  433. top: "0",
  434. left: "0",
  435. width: "100%",
  436. height: "2px",
  437. zIndex: "9999",
  438. });
  439. const progressBar = document.createElement("div");
  440. Object.assign(progressBar.style, {
  441. width: "100%",
  442. height: "100%",
  443. backgroundColor: "#00a4ff",
  444. });
  445. progressBarContainer.appendChild(progressBar);
  446. document.body.appendChild(progressBarContainer);
  447.  
  448. const link = document.createElement("a");
  449. link.href = sourceUrl;
  450. link.style.display = "none";
  451. document.body.appendChild(link);
  452.  
  453. let countdown = 5;
  454. const countdownInterval = setInterval(() => {
  455. countdown -= 0.01;
  456. document.getElementById("countdown").innerText = Math.ceil(countdown);
  457. progressBar.style.width = `${(countdown / 5) * 100}%`;
  458. if (countdown <= 0) {
  459. clearInterval(countdownInterval);
  460. link.click();
  461. }
  462. }, 10);
  463.  
  464. document.getElementById("cancelButton").addEventListener("click", () => {
  465. clearInterval(countdownInterval);
  466. countdownTips.style.animation = "fadeOut 0.3s ease-out";
  467. countdownTips.addEventListener("animationend", () => {
  468. countdownTips.remove();
  469. });
  470. progressBarContainer.remove();
  471. link.remove();
  472. });
  473. }
  474. }
  475. });
  476.  
  477. errorNock.safeExecute("unfixed_comment", () => {
  478. if (!features.unfixed_comment.enabled) {
  479. return;
  480. }
  481.  
  482. const style = document.createElement("style");
  483. style.textContent = `${features.unfixed_comment.selector[0]}
  484. { position: unset !important; }`;
  485. document.head.appendChild(style);
  486. });
  487.  
  488. errorNock.safeExecute("hidden_login", () => {
  489. if (
  490. !features.hidden_login.enabled &&
  491. document.querySelector(features.hidden_login.selector[0])
  492. ) {
  493. return;
  494. }
  495.  
  496. const observer = new MutationObserver(() => {
  497. const loginModal = document.querySelector(features.hidden_login.selector[1]);
  498.  
  499. if (!loginModal) {
  500. return;
  501. }
  502.  
  503. loginModal.querySelector(features.hidden_login.selector[2]).click();
  504. observer.disconnect();
  505. });
  506. observer.observe(document.body, { childList: true, subtree: true });
  507. });
  508.  
  509. errorNock.safeExecute("allow_copy", () => {
  510. if (!features.allow_copy.enabled) {
  511. return;
  512. }
  513.  
  514. const contentView = document.querySelector(features.allow_copy.selector[0]);
  515. contentView.querySelectorAll("*").forEach((content) => {
  516. content.oncopy = (e) => e.stopPropagation();
  517. });
  518.  
  519. const preTags = document.querySelectorAll(features.allow_copy.selector[1]);
  520. preTags.forEach((pre) => (pre.style.userSelect = "auto"));
  521.  
  522. const codeTags = document.querySelectorAll(features.allow_copy.selector[2]);
  523. codeTags.forEach((code) => (code.style.userSelect = "auto"));
  524. });
  525.  
  526. errorNock.safeExecute("unfold_code", () => {
  527. if (!features.unfold_code.enabled) {
  528. return;
  529. }
  530.  
  531. const unfoldCodeBtns = document.querySelectorAll(features.unfold_code.selector[0]);
  532. unfoldCodeBtns.forEach((btn) => btn.click());
  533. });
  534.  
  535. errorNock.safeExecute("allow_copy_with_btn", () => {
  536. if (!features.allow_copy_with_btn.enabled) {
  537. return;
  538. }
  539.  
  540. const codeCopyBtns = document.querySelectorAll(features.allow_copy_with_btn.selector[0]);
  541. codeCopyBtns.forEach((btn) => {
  542. btn.style.display = "none !important";
  543. btn.style.position = "absolute";
  544. btn.style.zIndex = "-1";
  545. });
  546.  
  547. const aiCopyBtns = document.querySelectorAll(features.allow_copy_with_btn.selector[1]);
  548. aiCopyBtns.forEach((btn) => {
  549. btn.innerHTML = "复制";
  550. btn.style.paddingLeft = "8px";
  551. btn.style.backgroundImage = "none";
  552. btn.onclick = (e) => {
  553. e.stopPropagation();
  554. const codeBox = btn.closest("pre").querySelector(features.allow_copy_with_btn.selector[2]);
  555. navigator.clipboard.writeText(codeBox.innerText);
  556. btn.innerHTML = "已复制";
  557. setTimeout(() => (btn.innerHTML = "复制"), 1000);
  558. };
  559. });
  560. });
  561.  
  562. errorNock.safeExecute("hidden_sidebar", () => {
  563. if (!features.hidden_sidebar.enabled) {
  564. return;
  565. }
  566.  
  567. const style = document.createElement("style");
  568. style.textContent = `${features.hidden_sidebar.selector[0]}
  569. { display: none !important; }`;
  570. document.head.appendChild(style);
  571.  
  572. const main = document.querySelector(features.hidden_sidebar.selector[1]);
  573. main.style.display = "flex";
  574. main.style.justifyContent = "center";
  575. });
  576.  
  577. const hiddenStyle = document.createElement("style");
  578. document.head.appendChild(hiddenStyle);
  579.  
  580. Object.entries(features.hidden).forEach(([key, option]) => {
  581. if (option.enabled) {
  582. const cssRule = `${option.selector} { display: none !important; }`;
  583. hiddenStyle.sheet.insertRule(cssRule);
  584. }
  585. });
  586.  
  587. function calculateTimeAgo(datetime) {
  588. const SECOND = 1000;
  589. const MINUTE = 60 * SECOND;
  590. const HOUR = 60 * MINUTE;
  591. const DAY = 24 * HOUR;
  592. const MONTH = 30 * DAY;
  593. const YEAR = 12 * MONTH;
  594.  
  595. const postDate = new Date(datetime);
  596. const currentDate = new Date();
  597. const timeDiff = currentDate - postDate;
  598.  
  599. if (timeDiff >= YEAR) {
  600. return Math.floor(timeDiff / YEAR) + " 年前";
  601. }
  602. if (timeDiff >= MONTH) {
  603. return Math.floor(timeDiff / MONTH) + " 个月前";
  604. }
  605. if (timeDiff >= DAY) {
  606. return Math.floor(timeDiff / DAY) + " 天前";
  607. }
  608. if (timeDiff >= HOUR) {
  609. return Math.floor(timeDiff / HOUR) + " 小时前";
  610. }
  611. if (timeDiff >= MINUTE) {
  612. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  613. }
  614. return Math.floor(timeDiff / SECOND) + " 秒前";
  615. }
  616. })();

QingJ © 2025

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