ZhiHuNock-Q

BlogNock系列,知乎问答的标识优化

当前为 2024-05-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ZhiHuNock-Q
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.7
  5. // @description BlogNock系列,知乎问答的标识优化
  6. // @author Exisi
  7. // @license MIT License
  8. // @match www.zhihu.com/question/*
  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. question_datetime: {
  20. enabled: GM_getValue("question_datetime", true),
  21. selector: [
  22. "meta[itemprop='dateCreated']",
  23. "meta[itemprop='dateModified']",
  24. ".QuestionHeaderActions>.ShareMenu",
  25. ".QuestionHeader",
  26. ],
  27. 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 256S119 504 256 504 504 393 504 256 393 8 256 8zm92.5 313h0l-20 25a16 16 0 0 1 -22.5 2.5h0l-67-49.7a40 40 0 0 1 -15-31.2V112a16 16 0 0 1 16-16h32a16 16 0 0 1 16 16V256l58 42.5A16 16 0 0 1 348.5 321z"/></svg>`,
  28. },
  29. },
  30. hidden_login: {
  31. enabled: GM_getValue("hidden_login", true),
  32. selector: [
  33. ".AppHeader-profileAvatar",
  34. ".Modal-closeButton",
  35. ".ZDI--Xmark16",
  36. "svg[width='26'][height='8']",
  37. ],
  38. },
  39. expand_answer_content_without_login: {
  40. enabled: GM_getValue("expand_answer_content_without_login", true),
  41. selector: [
  42. ".AppHeader-profileAvatar",
  43. ".RichContent-inner",
  44. ".RichContent-inner img",
  45. ".RichContent",
  46. ],
  47. },
  48. hidden_app_qrcode: {
  49. enabled: GM_getValue("hidden_app_qrcode", true),
  50. selector: [".Card.AppBanner"],
  51. },
  52. hidden_footer: {
  53. enabled: GM_getValue("hidden_footer", true),
  54. selector: ["footer"],
  55. },
  56. hidden: {
  57. answer_ad: {
  58. enabled: GM_getValue("answer_ad", true),
  59. selector: [".List .Pc-word"],
  60. },
  61. sidebar_ad_pic: {
  62. enabled: GM_getValue("sidebar_ad_pic", true),
  63. selector: [".Pc-card.Card"],
  64. },
  65. },
  66. };
  67.  
  68. 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-question-datetime" aria-nock="question_datetime" /> <label for="feature-question-datetime"></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> <div class="setting-item"> <span> 尝试未登录回答自动展开<span style="color: #FF0000;font-size: 12px;">(实现性特性,非全文回答)</span></span> <span> <input type="checkbox" id="feature-expand-answer-content-without-login" aria-nock="expand_answer_content_without_login" /> <label for="feature-expand-answer-content-without-login"></label> </span> </div> <hr /> <div class="setting-item"> <span> 隐藏左侧移动端推荐 </span> <span> <input type="checkbox" id="feature-hidden-app-qrcode" aria-nock="hidden_app_qrcode" /> <label for="feature-hidden-app-qrcode"></label> </span> </div> <div class="setting-item"> <span> 隐藏左侧知乎页脚 </span> <span> <input type="checkbox" id="feature-hidden-footer" aria-nock="hidden_footer" /> <label for="feature-hidden-footer"></label> </span> </div> <div class="setting-item"> <span> 隐藏回答中的广告 </span> <span> <input type="checkbox" id="feature-hidden-answer_ad" aria-nock="answer_ad" /> <label for="feature-hidden-answer_ad"></label> </span> </div> <div class="setting-item"> <span> 隐藏右侧边栏的图片广告 </span> <span> <input type="checkbox" id="feature-hidden-sidebar-ad-pic" aria-nock="sidebar_ad_pic" /> <label for="feature-hidden-sidebar-ad-pic"></label> </span> </div> </div> </div> </div>`;
  69. 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; }`;
  70.  
  71. const dStyle = document.createElement("style");
  72. dStyle.innerHTML = setStyle;
  73. document.head.appendChild(dStyle);
  74. const modal = document.createElement("div");
  75. modal.innerHTML = setModal;
  76. document.body.appendChild(modal);
  77.  
  78. const checkboxList = document.querySelectorAll(".setting-item input");
  79. Array.from(checkboxList).forEach((checkbox) => {
  80. const nock = checkbox.getAttribute("aria-nock");
  81. checkbox.checked = GM_getValue(nock, true);
  82. });
  83.  
  84. const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block");
  85. const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none");
  86.  
  87. document.querySelector(".modal-dialog").addEventListener("click", closeSetting);
  88. document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation());
  89. document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting);
  90. document.querySelector(".modal-body").addEventListener("click", (e) => {
  91. if (e.target.type !== "checkbox") {
  92. return;
  93. }
  94. const nock = e.target.getAttribute("aria-nock");
  95. const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock);
  96. GM_setValue(nock, flag);
  97. });
  98. GM_registerMenuCommand("功能设置", showSetting);
  99.  
  100. if (features.mark.question_datetime.enabled) {
  101. const questionCreatedTime = document.querySelector(features.mark.question_datetime.selector[0]);
  102. const questionModifiedTime = document.querySelector(
  103. features.mark.question_datetime.selector[1]
  104. );
  105. const rawQuestionPostTime = questionCreatedTime.getAttribute("content");
  106. const rawQuestionUpdateTime = questionModifiedTime.getAttribute("content");
  107.  
  108. const questionPostTimeAgo = calculateTimeAgo(rawQuestionPostTime);
  109. const questionUpdateTimeAgo = calculateTimeAgo(rawQuestionUpdateTime);
  110.  
  111. const questionPostTime = new Date(rawQuestionPostTime).toLocaleString().replaceAll("/", "-");
  112. const questionUpdateTime = new Date(rawQuestionUpdateTime)
  113. .toLocaleString()
  114. .replaceAll("/", "-");
  115.  
  116. const questionPostFormattedText = `发布于 ${questionPostTime}(${questionPostTimeAgo})`;
  117. const questionUpdateFormattedText = `编辑于 ${questionUpdateTime}(${questionUpdateTimeAgo})`;
  118.  
  119. const formattedTimeBox = document.createElement("div");
  120. formattedTimeBox.innerHTML = features.mark.question_datetime.icon;
  121. formattedTimeBox.style.color = "#8491a5";
  122. formattedTimeBox.style.marginLeft = "20px";
  123. formattedTimeBox.style.display = "flex";
  124. formattedTimeBox.style.alignItems = "center";
  125. formattedTimeBox.style.fontSize = "14px";
  126. formattedTimeBox.style.cursor = "pointer";
  127. formattedTimeBox.style.textDecoration = "underline";
  128.  
  129. const datetime = document.createElement("span");
  130. datetime.innerText = questionPostFormattedText;
  131. formattedTimeBox.appendChild(datetime);
  132.  
  133. formattedTimeBox.addEventListener("click", () => {
  134. const formattedText =
  135. datetime.innerText == questionPostFormattedText
  136. ? questionUpdateFormattedText
  137. : questionPostFormattedText;
  138.  
  139. datetime.innerText = formattedText;
  140. });
  141.  
  142. const icon = formattedTimeBox.querySelector("svg");
  143. icon.style.width = "15px";
  144. icon.style.height = "15px";
  145. icon.style.marginRight = "5px";
  146.  
  147. const questionHeaderShareAction = document.querySelector(
  148. features.mark.question_datetime.selector[2]
  149. );
  150. questionHeaderShareAction.after(formattedTimeBox);
  151.  
  152. const questionHeader = document.querySelector(features.mark.question_datetime.selector[3]);
  153. questionHeader.addEventListener("click", (e) => {
  154. setTimeout(() => {
  155. const shareAction = document.querySelector(features.mark.question_datetime.selector[2]);
  156. shareAction.after(formattedTimeBox);
  157. }, 50);
  158. });
  159. }
  160.  
  161. if (features.hidden_login.enabled && !document.querySelector(features.hidden_login.selector[0])) {
  162. const createObserver = (selector, callback) => {
  163. const observer = new MutationObserver(() => {
  164. const closeBtn = document.querySelector(selector);
  165.  
  166. if (!closeBtn) {
  167. return;
  168. }
  169.  
  170. callback();
  171. observer.disconnect();
  172. });
  173.  
  174. observer.observe(document.body, { childList: true, subtree: true });
  175. };
  176.  
  177. createObserver(features.hidden_login.selector[1], () => {
  178. document.querySelector(features.hidden_login.selector[1]).click();
  179. });
  180.  
  181. createObserver(features.hidden_login.selector[2], () => {
  182. document.querySelector(
  183. features.hidden_login.selector[2]
  184. ).parentNode.parentNode.parentNode.style.display = "none";
  185. });
  186.  
  187. createObserver(features.hidden_login.selector[3], () => {
  188. document.querySelector(features.hidden_login.selector[3]).closest("div").style.display =
  189. "none";
  190. });
  191. }
  192.  
  193. if (
  194. features.expand_answer_content_without_login.enabled &&
  195. !document.querySelector(features.expand_answer_content_without_login.selector[0])
  196. ) {
  197. const getAnswerLength = () => {
  198. const answerContents = document.querySelectorAll(
  199. features.expand_answer_content_without_login.selector[1]
  200. );
  201. return [answerContents.length, answerContents];
  202. };
  203.  
  204. let [answerLength, answerContents] = getAnswerLength();
  205.  
  206. const expandAnswerContent = (answerContents) => {
  207. answerContents.forEach((content) => {
  208. content.style.maxHeight = "max-content";
  209. content.addEventListener("click", (e) => e.stopPropagation());
  210. });
  211.  
  212. const answerPic = document.querySelectorAll(
  213. features.expand_answer_content_without_login.selector[2]
  214. );
  215. answerPic.forEach((pic) => {
  216. pic.addEventListener("click", (e) => e.stopPropagation());
  217. });
  218.  
  219. const richTextContents = document.querySelectorAll(
  220. features.expand_answer_content_without_login.selector[3]
  221. );
  222. richTextContents.forEach((richText) => {
  223. richText.classList.remove("is-collapsed");
  224. const expandButton = richText.querySelector(".ContentItem-expandButton");
  225.  
  226. if (expandButton) {
  227. expandButton.style.display = "none";
  228. }
  229. });
  230.  
  231. answerLength = answerContents.length;
  232. };
  233.  
  234. expandAnswerContent(answerContents);
  235.  
  236. let timerId = null;
  237. const observer = new MutationObserver(() => {
  238. if (timerId) {
  239. return;
  240. }
  241.  
  242. timerId = setTimeout(() => {
  243. let [dynamicAnswerLength, dynamicAnswerContents] = getAnswerLength();
  244.  
  245. if (answerLength == dynamicAnswerLength) {
  246. return (timerId = null);
  247. }
  248.  
  249. expandAnswerContent(dynamicAnswerContents);
  250. timerId = null;
  251. }, 500);
  252. });
  253.  
  254. observer.observe(document.body, { childList: true, subtree: true });
  255. }
  256.  
  257. if (
  258. features.hidden_app_qrcode.enabled &&
  259. document.querySelector(features.hidden_app_qrcode.selector[0])
  260. ) {
  261. const qrcodeBanner = document.querySelector(features.hidden_app_qrcode.selector[0]);
  262. qrcodeBanner.style.display = "none";
  263. }
  264.  
  265. if (features.hidden_footer.enabled) {
  266. const footer = document.querySelector(features.hidden_footer.selector[0]);
  267. footer.style.display = "none";
  268. }
  269.  
  270. const hiddenStyle = document.createElement("style");
  271. document.head.appendChild(hiddenStyle);
  272.  
  273. Object.entries(features.hidden).forEach(([key, option]) => {
  274. if (option.enabled) {
  275. const cssRule = `${option.selector} { display: none !important; }`;
  276. hiddenStyle.sheet.insertRule(cssRule);
  277. }
  278. });
  279.  
  280. function calculateTimeAgo(datetime) {
  281. const SECOND = 1000;
  282. const MINUTE = 60 * SECOND;
  283. const HOUR = 60 * MINUTE;
  284. const DAY = 24 * HOUR;
  285. const MONTH = 30 * DAY;
  286. const YEAR = 12 * MONTH;
  287.  
  288. const postDate = new Date(datetime);
  289. const currentDate = new Date();
  290. const timeDiff = currentDate - postDate;
  291.  
  292. if (timeDiff >= YEAR) {
  293. return Math.floor(timeDiff / YEAR) + " 年前";
  294. }
  295. if (timeDiff >= MONTH) {
  296. return Math.floor(timeDiff / MONTH) + " 个月前";
  297. }
  298. if (timeDiff >= DAY) {
  299. return Math.floor(timeDiff / DAY) + " 天前";
  300. }
  301. if (timeDiff >= HOUR) {
  302. return Math.floor(timeDiff / HOUR) + " 小时前";
  303. }
  304. if (timeDiff >= MINUTE) {
  305. return Math.floor(timeDiff / MINUTE) + " 分钟前";
  306. }
  307. return Math.floor(timeDiff / SECOND) + " 秒前";
  308. }
  309. })();