Twitter Advanced Search Button Twitter 高级搜索按钮

Adds an Advanced Search button next to Twitter's search box. Makes it easier to access Twitter's powerful advanced search features.

目前为 2024-12-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter Advanced Search Button Twitter 高级搜索按钮
  3. // @name:zh-CN Twitter高级搜索按钮
  4. // @name:ja Twitter高度な検索ボタン
  5. // @namespace http://tampermonkey.net/
  6. // @version 0.2
  7. // @description Adds an Advanced Search button next to Twitter's search box. Makes it easier to access Twitter's powerful advanced search features.
  8. // @description:zh-CN 在Twitter搜索框旁边添加高级搜索按钮,方便快速访问Twitter强大的高级搜索功能。
  9. // @description:ja Twitterの検索ボックスの横に高度な検索ボタンを追加し、Twitterの強力な検索機能に素早くアクセスできます。
  10. // @author Your name
  11. // @match https://x.com/*
  12. // @match https://twitter.com/*
  13. // @grant none
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. // Language settings
  21. const LANGUAGES = {
  22. 'zh-CN': '高级搜索',
  23. 'ja': '高度な検索',
  24. 'en': 'Search Advanced'
  25. };
  26.  
  27. // Get user's browser language
  28. function getUserLanguage() {
  29. const lang = navigator.language || navigator.userLanguage;
  30. const shortLang = lang.split('-')[0];
  31. // Check for exact match first
  32. if (LANGUAGES[lang]) {
  33. return LANGUAGES[lang];
  34. }
  35. // Then check for language without region
  36. if (LANGUAGES[shortLang]) {
  37. return LANGUAGES[shortLang];
  38. }
  39. // Default to English
  40. return LANGUAGES['en'];
  41. }
  42.  
  43. // Function to add the advanced search button
  44. function addAdvancedSearchButton() {
  45. // Find the search box container
  46. const searchBox = document.querySelector("#react-root > div > div > div.css-175oi2r.r-1f2l425.r-13qz1uu.r-417010.r-18u37iz > main > div > div > div > div > div > div.css-175oi2r.r-aqfbo4.r-gtdqiz.r-1gn8etr.r-1g40b8q > div.css-175oi2r.r-1e5uvyk.r-6026j > div.css-175oi2r.r-136ojw6 > div > div > div > div > div.css-175oi2r.r-1pz39u2.r-1777fci.r-15ysp7h.r-obd0qt.r-s8bhmr");
  47. if (searchBox && !document.getElementById('advanced-search-btn')) {
  48. // Create the button
  49. const button = document.createElement('button');
  50. button.id = 'advanced-search-btn';
  51. button.textContent = getUserLanguage();
  52. button.style.cssText = `
  53. margin-left: 12px;
  54. padding: 0 16px;
  55. height: 32px;
  56. min-height: 32px;
  57. border-radius: 16px;
  58. background-color: rgb(15, 20, 25);
  59. color: rgb(255, 255, 255);
  60. border: 1px solid rgb(15, 20, 25);
  61. font-weight: 700;
  62. cursor: pointer;
  63. font-size: 14px;
  64. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  65. transition-property: background-color, box-shadow;
  66. transition-duration: 0.2s;
  67. line-height: 32px;
  68. display: inline-flex;
  69. align-items: center;
  70. justify-content: center;
  71. user-select: none;
  72. white-space: nowrap;
  73. `;
  74.  
  75. // Add hover effect
  76. button.onmouseover = function() {
  77. this.style.backgroundColor = 'rgb(39, 44, 48)';
  78. };
  79. button.onmouseout = function() {
  80. this.style.backgroundColor = 'rgb(15, 20, 25)';
  81. };
  82.  
  83. // Add click event
  84. button.addEventListener('click', function() {
  85. window.location.href = 'https://x.com/search-advanced';
  86. });
  87.  
  88. // Insert the button after the search box
  89. searchBox.parentNode.insertBefore(button, searchBox.nextSibling);
  90. return true;
  91. }
  92. return false;
  93. }
  94.  
  95. // Function to check if we should add the button
  96. function shouldAddButton() {
  97. const url = window.location.href;
  98. return url.includes('/explore') || url.includes('/search-advanced');
  99. }
  100.  
  101. // Function to continuously check for the search box
  102. function startChecking() {
  103. if (shouldAddButton()) {
  104. // Initial check
  105. if (!addAdvancedSearchButton()) {
  106. // If not found, set up periodic checking
  107. const checkInterval = setInterval(() => {
  108. if (addAdvancedSearchButton() || !shouldAddButton()) {
  109. clearInterval(checkInterval);
  110. }
  111. }, 1000);
  112. }
  113. }
  114. }
  115.  
  116. // Start checking when script loads
  117. startChecking();
  118.  
  119. // Create a MutationObserver to watch for DOM changes
  120. const observer = new MutationObserver((mutations) => {
  121. if (shouldAddButton() && !document.getElementById('advanced-search-btn')) {
  122. startChecking();
  123. }
  124. });
  125.  
  126. // Start observing the document with the configured parameters
  127. observer.observe(document.body, {
  128. childList: true,
  129. subtree: true,
  130. attributes: true,
  131. characterData: true
  132. });
  133.  
  134. // Also check when history events occur (back/forward navigation)
  135. window.addEventListener('popstate', function() {
  136. if (shouldAddButton()) {
  137. startChecking();
  138. }
  139. });
  140.  
  141. // Check when the page visibility changes (tab switching)
  142. document.addEventListener('visibilitychange', function() {
  143. if (!document.hidden && shouldAddButton()) {
  144. startChecking();
  145. }
  146. });
  147.  
  148. // Handle URL changes
  149. let lastUrl = location.href;
  150. new MutationObserver(() => {
  151. const url = location.href;
  152. if (url !== lastUrl) {
  153. lastUrl = url;
  154. if (shouldAddButton()) {
  155. startChecking();
  156. }
  157. }
  158. }).observe(document, {subtree: true, childList: true});
  159. })();