X - 优化推文按钮

可以自由显示/隐藏,推文上的按钮,包括,回覆、转推、喜欢、观看次数、书签、分享等按钮,并且有中英两种功能语言可以切换

目前为 2025-04-15 提交的版本,查看 最新版本

  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.3
  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: {
  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-TW': {
  67. reply: '回覆',
  68. retweet: '轉推',
  69. bookmark: '書籤',
  70. views: '觀看次數',
  71. share: '分享',
  72. like: '喜歡',
  73. language: '語言'
  74. }
  75. };
  76.  
  77. function t() {
  78. const { language } = config.get();
  79. return i18n[language] || i18n.EN;
  80. }
  81.  
  82. // === 樣式管理 ===
  83. const style = {
  84. element: null,
  85. rules: new Map([
  86. ['hideReply', '[data-testid="reply"] { display: none !important; }'],
  87. ['hideRetweet', '[data-testid="retweet"] { display: none !important; }'],
  88. ['hideBookmark', '[data-testid="bookmark"] { display: none !important; }'],
  89. ['hideViews', 'a[href*="/analytics"] { display: none !important; }'],
  90. ['hideShare', 'button[aria-label="Share Post"],button[aria-label="分享貼文"],button[aria-label="分享"],button[aria-label="Compartir publicación"] { display: none !important; }'],
  91. ['hideLike', '[data-testid="like"], [data-testid="unlike"] { display: none !important; }']
  92. ]),
  93. init() {
  94. if (!document.getElementById('x-btn-hider-styles')) {
  95. this.element = document.createElement('style');
  96. this.element.id = 'x-btn-hider-styles';
  97. document.head.appendChild(this.element);
  98. } else {
  99. this.element = document.getElementById('x-btn-hider-styles');
  100. }
  101. this.update();
  102. },
  103. update() {
  104. const currentConfig = config.get();
  105. const activeRules = Array.from(this.rules.entries())
  106. .filter(([key]) => currentConfig[key])
  107. .map(([, rule]) => rule);
  108. this.element.textContent = activeRules.join('\n');
  109. }
  110. };
  111.  
  112. // === 選單系統 ===
  113. const menu = {
  114. cmds: [],
  115. build() {
  116. this.cmds = [];
  117. const currentConfig = config.get();
  118. const items = [
  119. { key: 'hideReply', label: t().reply },
  120. { key: 'hideRetweet', label: t().retweet },
  121. { key: 'hideBookmark', label: t().bookmark },
  122. { key: 'hideViews', label: t().views },
  123. { key: 'hideShare', label: t().share },
  124. { key: 'hideLike', label: t().like }
  125. ];
  126. items.forEach(({ key, label }) => {
  127. const status = currentConfig[key] ? '✅' : '❌';
  128. this.cmds.push(GM_registerMenuCommand(
  129. `${label} ${status}`,
  130. () => {
  131. config.update(key, !config.get()[key]);
  132. location.reload(); // 直接刷新頁面
  133. }
  134. ));
  135. });
  136. // 語言切換
  137. let langStatus = '';
  138. if (currentConfig.language === 'EN') {
  139. langStatus = 'EN';
  140. } else {
  141. langStatus = '中文';
  142. }
  143. this.cmds.push(GM_registerMenuCommand(
  144. `${t().language}: ${langStatus}`,
  145. () => {
  146. config.update('language', currentConfig.language === 'EN' ? 'ZH-TW' : 'EN');
  147. location.reload(); // 直接刷新頁面
  148. }
  149. ));
  150. }
  151. };
  152.  
  153. // === 防抖工具函數 ===
  154. function debounce(func, delay) {
  155. let timer;
  156. return (...args) => {
  157. clearTimeout(timer);
  158. timer = setTimeout(() => func(...args), delay);
  159. };
  160. }
  161.  
  162. const debouncedStyleUpdate = debounce(() => style.update(), OPT.debounceTime);
  163.  
  164. // === 初始化流程 ===
  165. (function init() {
  166. style.init();
  167. menu.build();
  168. const observer = new MutationObserver(mutations => {
  169. if (mutations.some(m => m.addedNodes.length > 0)) {
  170. debouncedStyleUpdate();
  171. }
  172. });
  173. observer.observe(document.body, OPT.observerConfig);
  174. })();
  175. })();