X - 優化推文按鈕

可以自由顯示/隱藏,推文上的按鈕,包括,回覆、轉推、喜歡、觀看次數、書籤、分享等按鈕,並且有中英兩種功能語言可以切換

目前為 2025-04-12 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name X - Optimized Tweet Buttons
  3. // @name:zh-TW X - 優化推文按鈕
  4. // @name:zh-CN X - 优化推文按钮
  5. // @namespace http://tampermonkey.net/
  6. // @version 5.1
  7. // @description You can freely show or hide the buttons on a tweet, including Reply, Retweet, Like, View Count, Bookmark, and Share. The interface supports switching between Chinese and English.
  8. // @description:zh-TW 可以自由顯示/隱藏,推文上的按鈕,包括,回覆、轉推、喜歡、觀看次數、書籤、分享等按鈕,並且有中英兩種功能語言可以切換
  9. // @description:zh-CN 可以自由显示/隐藏,推文上的按钮,包括,回覆、转推、喜欢、观看次数、书签、分享等按钮,并且有中英两种功能语言可以切换
  10. // @author chatgpt
  11. // @match https://twitter.com/*
  12. // @match https://x.com/*
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @license MIT
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. // === 性能優化核心 ===
  23. const OPT = {
  24. debounceTime: 500, // 防抖間隔
  25. observerConfig: { // 監控配置:設定 subtree 為 true,以確保監控所有新增節點
  26. childList: true,
  27. subtree: true,
  28. attributes: false,
  29. characterData: false
  30. }
  31. };
  32.  
  33. // === 配置系統 ===
  34. const CONFIG_KEY = 'XButtonSettings';
  35. const defaults = {
  36. hideReply: true,
  37. hideRetweet: true,
  38. hideBookmark: true,
  39. hideViews: true,
  40. hideShare: true,
  41. hideLike: false,
  42. language: 'EN' // 預設英文
  43. };
  44.  
  45. const config = {
  46. get() {
  47. return { ...defaults, ...GM_getValue(CONFIG_KEY, {}) };
  48. },
  49. update(key, value) {
  50. const current = this.get();
  51. GM_setValue(CONFIG_KEY, { ...current, [key]: value });
  52. }
  53. };
  54.  
  55. // === 多語言系統 ===
  56. const i18n = {
  57. EN: {
  58. reply: 'Reply',
  59. retweet: 'Retweet',
  60. bookmark: 'Bookmark',
  61. views: 'View count',
  62. share: 'Share',
  63. like: 'Like',
  64. language: 'Language'
  65. },
  66. ZH: {
  67. reply: '回覆',
  68. retweet: '轉推',
  69. bookmark: '書籤',
  70. views: '觀看次數',
  71. share: '分享',
  72. like: '喜歡',
  73. language: '語言'
  74. }
  75. };
  76.  
  77. // 每次調用時根據最新配置返回對應語言字串
  78. const t = () => {
  79. const { language } = config.get();
  80. return i18n[language] || i18n.EN;
  81. };
  82.  
  83. // === 樣式管理 ===
  84. const style = {
  85. element: null,
  86. rules: new Map([
  87. ['hideReply', '[data-testid="reply"] { display: none !important; }'],
  88. ['hideRetweet', '[data-testid="retweet"] { display: none !important; }'],
  89. ['hideBookmark', '[data-testid="bookmark"] { display: none !important; }'],
  90. ['hideViews', 'a[href*="/analytics"] { display: none !important; }'],
  91. ['hideShare', 'button[aria-label="分享貼文"]:not(:has(svg g.download)) { display: none !important; }'],
  92. ['hideLike', '[data-testid="like"], [data-testid="unlike"] { display: none !important; }']
  93. ]),
  94. init() {
  95. this.element = document.createElement('style');
  96. this.element.id = 'x-btn-hider-styles';
  97. document.head.appendChild(this.element);
  98. this.update();
  99. },
  100. update() {
  101. // 取得當前配置,並套用生效的 CSS 規則
  102. const currentConfig = config.get();
  103. const activeRules = Array.from(this.rules.entries())
  104. .filter(([key]) => currentConfig[key])
  105. .map(([, rule]) => rule);
  106. this.element.textContent = activeRules.join('\n');
  107. }
  108. };
  109.  
  110. // === 選單系統 (帶防抖功能) ===
  111. const menu = {
  112. cmds: [],
  113. build() {
  114. // 清除舊選單(假如 GM_unregisterMenuCommand 可用)
  115. menu.cmds.forEach(id => {
  116. if (typeof GM_unregisterMenuCommand === 'function') {
  117. GM_unregisterMenuCommand(id);
  118. }
  119. });
  120. menu.cmds = [];
  121.  
  122. // 使用配置的緩存,避免多次調用 config.get()
  123. const currentConfig = config.get();
  124. const items = [
  125. { key: 'hideReply', label: t().reply },
  126. { key: 'hideRetweet', label: t().retweet },
  127. { key: 'hideBookmark', label: t().bookmark },
  128. { key: 'hideViews', label: t().views },
  129. { key: 'hideShare', label: t().share },
  130. { key: 'hideLike', label: t().like }
  131. ];
  132.  
  133. items.forEach(({ key, label }) => {
  134. const status = currentConfig[key] ? '✅' : '❌';
  135. menu.cmds.push(GM_registerMenuCommand(
  136. `${label} ${status}`,
  137. () => {
  138. // 更新對應設定,然後防抖後重載頁面
  139. config.update(key, !config.get()[key]);
  140. debouncedReload();
  141. }
  142. ));
  143. });
  144.  
  145. // 語言切換
  146. const langStatus = currentConfig.language === 'EN' ? 'EN' : 'ZH';
  147. menu.cmds.push(GM_registerMenuCommand(
  148. `${t().language}: ${langStatus}`,
  149. () => {
  150. config.update('language', config.get().language === 'EN' ? 'ZH' : 'EN');
  151. debouncedReload();
  152. }
  153. ));
  154. }
  155. };
  156.  
  157. // === 防抖工具函數 ===
  158. const debounce = (func, delay) => {
  159. let timer;
  160. return (...args) => {
  161. clearTimeout(timer);
  162. timer = setTimeout(() => func(...args), delay);
  163. };
  164. };
  165.  
  166. const debouncedReload = debounce(() => location.reload(), 300);
  167. const debouncedStyleUpdate = debounce(() => style.update(), OPT.debounceTime);
  168.  
  169. // === 初始化流程 ===
  170. (function init() {
  171. // 初始化樣式管理
  172. style.init();
  173. // 建立選單
  174. menu.build();
  175. // 初始化 MutationObserver,當 DOM 發生新增時更新樣式
  176. const observer = new MutationObserver(mutations => {
  177. if (mutations.some(m => m.addedNodes.length > 0)) {
  178. debouncedStyleUpdate();
  179. }
  180. });
  181. observer.observe(document.body, OPT.observerConfig);
  182. })();
  183. })();