Greasy Fork 支持简体中文。

Instant Search Switcher

Changes search engine from one site to another without deleting search query.

目前為 2024-12-24 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Instant Search Switcher
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.4
  5. // @description Changes search engine from one site to another without deleting search query.
  6. // @author Faisal Bhuiyan
  7. // @match *://*.bing.com/*search*
  8. // @match *://*.bing.com/chat*
  9. // @match https://www.google.com/search*
  10. // @match https://www.qwant.com/*
  11. // @match *://yandex.com/*search*
  12. // @match *://search.brave.com/*search*
  13. // @match *://duckduckgo.com/*
  14. // @license MIT
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. const searchEngines = [
  22. {
  23. name: 'Google',
  24. url: 'https://www.google.com/search',
  25. param: 'q'
  26. },
  27. {
  28. name: 'Bing',
  29. url: 'https://www.bing.com/search',
  30. param: 'q'
  31. },
  32. {
  33. name: 'Copilot',
  34. url: 'https://www.bing.com/chat',
  35. param: 'q',
  36. additionalParams: '&sendquery=1&FORM=SCCODX'
  37. },
  38. {
  39. name: 'Qwant',
  40. url: 'https://www.qwant.com/',
  41. param: 'q'
  42. },
  43. {
  44. name: 'Brave',
  45. url: 'https://search.brave.com/search',
  46. param: 'q'
  47. },
  48. {
  49. name: 'Yandex',
  50. url: 'https://yandex.com/search',
  51. param: 'text'
  52. },
  53. {
  54. name: 'Perplexity',
  55. url: 'https://www.perplexity.ai/search',
  56. param: 'q'
  57. },
  58. {
  59. name: 'Phind',
  60. url: 'https://www.phind.com/search',
  61. param: 'q'
  62. },
  63. {
  64. name: 'Morphic',
  65. url: 'https://morphic.sh/search',
  66. param: 'q'
  67. },
  68. {
  69. name: 'DuckDuckGo',
  70. url: 'https://duckduckgo.com/',
  71. param: 'q'
  72. }
  73. ];
  74.  
  75. // Create the floating select box
  76. const selectBox = document.createElement('select');
  77. selectBox.style.position = 'fixed';
  78. selectBox.style.top = '20px';
  79. selectBox.style.right = '5rem';
  80. selectBox.style.zIndex = '9999';
  81. selectBox.style.fontSize = '16px';
  82. selectBox.style.padding = '5px';
  83. selectBox.style.borderRadius = '4px';
  84. selectBox.style.backgroundColor = '#fff';
  85. selectBox.style.color = '#000'; // Force black text
  86. selectBox.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.1)';
  87.  
  88. // Add CSS to ensure text remains visible in both light and dark themes
  89. const style = document.createElement('style');
  90. style.textContent = `
  91. .search-switcher-select {
  92. color: #000 !important;
  93. -webkit-text-fill-color: #000 !important;
  94. }
  95. .search-switcher-select option {
  96. background-color: #fff !important;
  97. color: #000 !important;
  98. -webkit-text-fill-color: #000 !important;
  99. }
  100. `;
  101. document.head.appendChild(style);
  102.  
  103. selectBox.className = 'search-switcher-select';
  104.  
  105. // Add an empty option as the first element
  106. const emptyOption = document.createElement('option');
  107. emptyOption.value = '';
  108. emptyOption.textContent = 'Select';
  109. emptyOption.disabled = true;
  110. emptyOption.selected = true;
  111. selectBox.appendChild(emptyOption);
  112.  
  113. // Add search engines to the select box
  114. searchEngines.forEach(engine => {
  115. const option = document.createElement('option');
  116. option.value = engine.url;
  117. option.textContent = engine.name;
  118. selectBox.appendChild(option);
  119. });
  120.  
  121. // Append the select box to the body
  122. document.body.appendChild(selectBox);
  123.  
  124. // Detect changes to the select box
  125. selectBox.addEventListener('change', () => {
  126. const selectedEngine = searchEngines[selectBox.selectedIndex - 1];
  127. // Try to get query parameter from either 'q' or 'text' depending on current search engine
  128. const currentQuery = new URLSearchParams(window.location.search).get('q') ||
  129. new URLSearchParams(window.location.search).get('text');
  130.  
  131. if (currentQuery && selectedEngine) {
  132. const additionalParams = selectedEngine.additionalParams || '';
  133. window.location.href = `${selectedEngine.url}?${selectedEngine.param}=${encodeURIComponent(currentQuery)}${additionalParams}`;
  134. }
  135. });
  136. })();