Linux.po

对 linux.do 的增强脚本

  1. // ==UserScript==
  2. // @name Linux.po
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.8
  5. // @description 对 linux.do 的增强脚本
  6. // @author PRO-2684
  7. // @match https://linux.do/*
  8. // @run-at document-start
  9. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAABOxJREFUWEfNV0tslFUU/s7/nmmHmWE6NRUqKZXER9OIuNFA3Ei6w6CUWiPGjRiaSLtoWZi4MnFBu2gxgYgLjRgqtNHIrsGFBqIbAdPUR4LQYIXGTseZ6WPmfx+9f+dvphRIDWPxzm7+e8/3nXPPOfc7hLssZiYAKhHZ4RZm3gtgD4Am27Z3EpEsvjGzp2naRQCTAM4R0ZcVZzQADhHxnaAEyKrFzALYKRtPADjuOE7n2NgYLly4iPHxcVy6dEVAl88SduzYjtbWVuzatRNtbW1QVXUYQBcR5ct2lm1WAq4iwMxa6DUzD7qu2z04OIRjxz7A1NQNF4BfUxOTa2pqZKKl48yMxcVFb3Fx3gMgNTZuUQ4ffhs9Pd1QFGXoHxI95X3LtkMSKwgws05EFjNvBDAxMjLacOhQF7LZjF1XV6/GYjESoJ7nwff9FZGTJAmyLAdk5ufneXZ2xkml0tqJE8fR3r5vGkALEf0VYqwiEHpeLBY3RyKRqb6+IxgY6DfT6YeMWCwGx3EC42tZgqSqqoIIMpk/zd7ePqO//yhKpVJjNBr9ozLKQQTCOy97nu3oeAVnz56xmpqa9Tt5uxYSYk8YlcnJa9b+/R36mTOfi79T5UgEOUEi28MMZeZbfX1HGgYG+q3m5m26ZVlrxbrnPl3Xce3aVau3t0/v7z86TUQPlx0nQSBIDJFwo6Oj3e3t7WZz8zbDtperryokNE0TJMyRkVFj376Xg8QMsMNSy2Qyufr6ehE4RCKRVUl2vyzEdZRKJVFEmJmZQTqdTooSDQjYtn365s2bnRMTE3Y8Htc8z0e5wu4Xd/m8yF9ZllAoFOyWlhZt06ZNw5qmvRoQyOVybBgGDMMQaX7H5lQ1JgCbpkmmaSKZTBLlcrm9RPQFM7ue5ylVBLqrKVmWXSJSmPklKhQKHzPzG+ImAIi+vR4rwCKiTyifz38D4HkAoo0Gj8s6rBDrW0FA9Pf1Ar7dN08QWFt//Y/C8r8g8MCv4MEm4QMvw7ARAeyC16cRgWQXKDcikdz5fI4hGWBlfVoxuSbBN5FIJJeenPy8e5rMG53y3GWb1aQGXim3qlaBJIGcvO1t2K6xsWU4EVPKjxFzgnKZXHysfukpkqoGudKQ6H8ACm0z4GRaPEV5+olZe5LInmUejPz2WXf0+wMmElEDfnUFCSQNyBfN4rOnjNKjrw3VEfUI7BWSLOvxrdrLbzVov560EI/q8KsjySDpQKFo2Y8d1Bee/nA6JVdIsrIiCgRiocAb3SiyG77bDeX3ry3EI7ooDrCI3b+VCQyIwYkUoFCy3Ede0OeeOw+liFQ8HsjzJVEaXlKoDbNF3gwVU7U/HoT2y0cmamFAiQK+GJTWmpwSIKmAWwQWYNqPv2ksPHUScNCYitJqWR6SuMqsbyOygkjUYsK4fqohOv46YMJGBCpkgwKtJiJye6WQtOSx0F6eySjBgQGt2PopzK0HppUFtAjPQ4wQ856jWZZ5UFrwuo3J92Bcfx9YdFwQfEEl+FUuDx4ceGBIqFEVc+s7MJvehV8rD6XWOppVXMfyIJljTvjAcWnB7VQzX0HNnoc8dwly4YeV+PFn4G3YASe1G076Rfi1yrAEdIlSq8yz2wv8rpklBpafAVWUaHholnkv+dgjWWiC5+6sEDIeZOWir2OSJZyrqxjPRak9cY/x/G9pa4SNlgBs7AAAAABJRU5ErkJggg==
  10. // @license gpl-3.0
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @grant GM_deleteValue
  14. // @grant GM_registerMenuCommand
  15. // @grant GM_unregisterMenuCommand
  16. // @grant GM_addValueChangeListener
  17. // @require https://github.com/PRO-2684/GM_config/releases/download/v1.2.1/config.min.js#md5=525526b8f0b6b8606cedf08c651163c2
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22. const { name, version } = GM_info.script;
  23. const idPrefix = "linux-po-";
  24. const configDesc = {
  25. $default: {
  26. autoClose: false,
  27. },
  28. appearance: {
  29. name: "🎨 外观",
  30. title: "外观",
  31. type: "folder",
  32. items: {
  33. sidebarManager: {
  34. name: "⬅️ 侧栏管理",
  35. title: "允许你隐藏侧栏中的各个部分",
  36. type: "folder",
  37. items: {
  38. $default: {
  39. value: false,
  40. input: "current",
  41. processor: "not",
  42. title: (prop, value, desc) => desc.name,
  43. formatter: (prop, value, desc) => `${desc.name}: ${value ? "🫥" : "👀"}`,
  44. },
  45. customCategories: { name: "自定义板块" },
  46. externalLinks: { name: "外部链接" },
  47. categories: { name: "类别" },
  48. tags: { name: "标签" },
  49. messages: { name: "消息" },
  50. channels: { name: "频道" },
  51. directMessages: { name: "直接消息" },
  52. chat: { name: "聊天" },
  53. bottomMenu: { name: "底部菜单" },
  54. },
  55. },
  56. postManager: {
  57. name: "📝 帖子管理",
  58. title: "允许你隐藏帖子的各个部分",
  59. type: "folder",
  60. items: {
  61. $default: {
  62. value: false,
  63. input: "current",
  64. processor: "not",
  65. title: (prop, value, desc) => desc.name,
  66. formatter: (prop, value, desc) => `${desc.name}: ${value ? "🫥" : "👀"}`,
  67. },
  68. secondaryName: { name: "次要名称" },
  69. userTitle: { name: "头衔" },
  70. userStatus: { name: "自定义状态" },
  71. posterIcon: {
  72. name: "🍰",
  73. title: "加入社区纪念日以及生日图标 (discourse-cakeday)",
  74. },
  75. postNotice: {
  76. name: "帖子通知",
  77. title: "新用户首次发言、回归用户等标识",
  78. },
  79. flair: {
  80. name: "资质",
  81. title: "展示在头像右下角",
  82. },
  83. },
  84. },
  85. },
  86. },
  87. accessibility: {
  88. name: "♿ 辅助功能",
  89. title: "辅助功能",
  90. type: "folder",
  91. items: {
  92. largerClickArea: {
  93. name: "👆 增大点击区域",
  94. title: "增大帖子列表中各帖子的可点击区域 (仅支持左键)",
  95. type: "bool",
  96. value: true,
  97. },
  98. showPostsFloor: {
  99. name: "🔢 显示楼层",
  100. title: "在帖子中显示楼层",
  101. type: "bool",
  102. value: false,
  103. },
  104. atBeforeUsername: {
  105. name: "👤 @用户名",
  106. title: "在用户名前添加 @ 符号",
  107. type: "bool",
  108. value: false,
  109. },
  110. },
  111. },
  112. };
  113. const config = new GM_config(configDesc);
  114.  
  115. // Helper function for css
  116. function injectCSS(id, css) {
  117. const style = document.head.appendChild(document.createElement("style"));
  118. style.id = idPrefix + id;
  119. style.textContent = css;
  120. return style;
  121. }
  122. function cssHelper(id, enable) {
  123. const current = document.getElementById(idPrefix + id);
  124. if (current) {
  125. current.disabled = !enable;
  126. } else if (enable) {
  127. injectCSS(id, dynamicStyles[id]);
  128. }
  129. }
  130. /**
  131. * Generates CSS for hiding given sidebar section.
  132. */
  133. function hideSidebarSection(section) {
  134. return `#d-sidebar > .sidebar-sections div.sidebar-section[data-section-name="${section}"] { display: none; }`;
  135. }
  136. /**
  137. * Generates CSS for hiding given post section.
  138. */
  139. function hidePostSection(section) {
  140. return `.post-stream > .topic-post > article .names > .${section} { display: none; }`;
  141. }
  142.  
  143. // Dynamic styles
  144. const dynamicStyles = {
  145. "appearance.sidebarManager.customCategories": hideSidebarSection("community"),
  146. "appearance.sidebarManager.externalLinks": hideSidebarSection("外部链接"),
  147. "appearance.sidebarManager.categories": hideSidebarSection("categories"),
  148. "appearance.sidebarManager.tags": hideSidebarSection("tags"),
  149. "appearance.sidebarManager.messages": hideSidebarSection("messages"),
  150. "appearance.sidebarManager.channels": hideSidebarSection("chat-channels"),
  151. "appearance.sidebarManager.directMessages": hideSidebarSection("chat-dms"),
  152. "appearance.sidebarManager.chat": "#d-sidebar > button[data-key='chat'] { display: none; }",
  153. "appearance.sidebarManager.bottomMenu": "#d-sidebar > div.sidebar-footer-wrapper { display: none; }",
  154. "appearance.postManager.secondaryName": hidePostSection("second"),
  155. "appearance.postManager.userTitle": hidePostSection("user-title"),
  156. "appearance.postManager.userStatus": hidePostSection("user-status-message-wrap"),
  157. "appearance.postManager.posterIcon": hidePostSection("poster-icon"),
  158. "appearance.postManager.flair": ".topic-avatar > .post-avatar > .avatar-flair { display: none; }",
  159. "appearance.postManager.postNotice": ".post-stream > .topic-post > article .post-notice { display: none; }",
  160. "accessibility.largerClickArea": ".topic-list-item > .main-link { cursor: pointer; }",
  161. "accessibility.showPostsFloor": `.post-stream > .topic-post > article[id^='post_'] {
  162. &::after {
  163. content: attr(id) '#'; color: var(--primary-med-or-secondary-med);
  164. position: absolute; right: 0; top: calc(0.8em + 1px);
  165. text-indent: -2.5em; overflow: hidden; /* Dirty trick to hide leading "post_" */
  166. }
  167. .embedded-posts > .reply .post-link-arrow > a.post-info::after {
  168. content: attr(href) '#'; display: inline-flex;
  169. text-indent: -7.9em; overflow: hidden; /* Dirty trick to hide leading "/t/topic/\\d{6}/" */
  170. }
  171. }
  172. .timeline-container > .topic-timeline > .timeline-scrollarea-wrapper > .timeline-date-wrapper > .now-date[href^='/t/topic/']::after {
  173. content: attr(href) '#'; display: inline-flex; margin-left: 0.2em;
  174. text-indent: -7.9em; overflow: hidden; /* Dirty trick to hide leading "/t/topic/\\d{6}/" */
  175. }`,
  176. "accessibility.atBeforeUsername": `
  177. span.username > a::before { content: "@"; }
  178. div.username::before { content: "@"; }
  179. `,
  180. };
  181. for (const prop in dynamicStyles) {
  182. cssHelper(prop, config.get(prop));
  183. }
  184.  
  185. // Accessibility
  186. // Larger click area
  187. let largerClickAreaEnabled = false;
  188. /**
  189. * Handles the click event when larger click area is enabled.
  190. * @param {MouseEvent} e
  191. */
  192. function largerClickAreaHandler(e) {
  193. if (e.defaultPrevented || !e.isTrusted) return;
  194. const mainLink = e.target.closest(".topic-list-item > .main-link");
  195. if (mainLink) {
  196. e.preventDefault();
  197. const title = mainLink.querySelector(".title.raw-link.raw-topic-link");
  198. title?.click();
  199. }
  200. }
  201. /**
  202. * Enables or disables the larger click area feature.
  203. * @param {boolean} enable
  204. */
  205. function largerClickArea(enable) {
  206. if (enable && !largerClickAreaEnabled) {
  207. document.body.addEventListener("click", largerClickAreaHandler);
  208. largerClickAreaEnabled = true;
  209. } else if (!enable && largerClickAreaEnabled) {
  210. document.body.removeEventListener("click", largerClickAreaHandler);
  211. largerClickAreaEnabled = false;
  212. }
  213. }
  214.  
  215. // Callbacks
  216. const callbacks = {
  217. "accessibility.largerClickArea": largerClickArea,
  218. };
  219. for (const [prop, callback] of Object.entries(callbacks)) {
  220. callback(config.get(prop));
  221. }
  222. config.addEventListener("set", e => {
  223. if (e.detail.prop in dynamicStyles) {
  224. cssHelper(e.detail.prop, e.detail.after);
  225. }
  226. if (e.detail.prop in callbacks) {
  227. callbacks[e.detail.prop](e.detail.after);
  228. }
  229. });
  230.  
  231. // https://linux.do/emojis.json
  232. })();
  233.