一键切换搜索

在常用的搜索引擎页面中添加互相切换的按钮。

  1. // ==UserScript==
  2. // @name Search Switcher
  3. // @name:zh-CN 一键切换搜索
  4. // @name:zh-TW 壹鍵切換搜索
  5. // @description Add links to each other in search engines. Including multiple search modes.
  6. // @description:zh-CN 在常用的搜索引擎页面中添加互相切换的按钮。
  7. // @description:zh-TW 在常用的搜索引擎頁面中添加互相切換的按鈕。
  8. // @author XanderWang
  9. // @icon https://i.loli.net/2020/05/29/DxSmHAy2o53FdUY.png
  10. // @license GPL-3.0
  11. // @include https://www.baidu.com/*
  12. // @include *.so.com/*
  13. // @include *.bing.com/*
  14. // @include *.soku.com/*
  15. // @include *.sogou.com/*
  16. // @include /^https?://[a-z]+\.google\.[a-z,\.]+/.+$/
  17. // @run-at document_body
  18. // @date 05/29/2020
  19. // @modified 07/31/2024
  20. // @version 1.0.7
  21. // @namespace https://blog.xanderwang.site
  22. // ==/UserScript==
  23.  
  24. (function () {
  25. //指定代码在严格条件下执行
  26. 'use strict';
  27. const sites = [
  28. {
  29. name: "百度",
  30. host: "baidu.com",
  31. link: "https://www.baidu.com/s",
  32. key: "wd",
  33. hide: false,
  34. },
  35. {
  36. name: "必应",
  37. host: "bing.com",
  38. link: "https://bing.com/search",
  39. key: "q",
  40. hide: false,
  41. },
  42. {
  43. name: "谷歌",
  44. host: "google.com",
  45. link: "https://www.google.com.hk/search",
  46. key: "q",
  47. hide: false,
  48. },
  49. {
  50. name: "谷歌镜像",
  51. host: "google.fuckcloudnative.io",
  52. link: "https://google.fuckcloudnative.io/search",
  53. key: "q",
  54. hide: false,
  55. },
  56. {
  57. name: "搜搜",
  58. host: "so.com",
  59. link: "https://www.so.com/s",
  60. key: "q",
  61. hide: false,
  62. },
  63. {
  64. name: "搜狗",
  65. host: "sogou.com",
  66. link: "https://www.sogou.com/web",
  67. key: "query",
  68. hide: false,
  69. },
  70. ];
  71.  
  72. const css = `
  73. .search-warpper {
  74. position: fixed;
  75. left: 0;
  76. top: 0;
  77. }
  78.  
  79. .search-switcher {
  80. position: fixed;
  81. opacity: 0.2;
  82. top: 80px;
  83. right: calc(100% - 10px);
  84. z-index: 9999999;
  85. transition: all 400ms;
  86. }
  87.  
  88. .search-switcher:hover {
  89. top: 80px;
  90. left: 0px;
  91. right:auto;
  92. opacity: 1;
  93. border-radius: 0 20px;
  94. }
  95.  
  96. .search-switcher .search-list {
  97. display: flex;
  98. flex-direction: column;
  99. align-items: center;
  100. justify-content: center;
  101. box-sizing:border-box;
  102. background-color: #000;
  103. border-radius: 0px 10px 10px 0px;
  104. color: #fff;
  105. padding: 10px;
  106. box-shadow: 5px 5px 5px #777;
  107. }
  108.  
  109. .search-switcher .search-list a {
  110. color: #0cf;
  111. height: 25px;
  112. line-height: 25px;
  113. }
  114.  
  115. .search-switcher .search-list a.mirror {
  116. font-weight: bold;
  117. }
  118. `;
  119.  
  120. function setup() {
  121. console.log("setup location:", location.href);
  122. let current_site;
  123. for (let site of sites) {
  124. if (location.host.includes(site.host)) {
  125. current_site = site;
  126. }
  127. }
  128. let site_list = sites.filter(
  129. ({ host, hide }) => !location.hostname.includes(host) && !hide
  130. );
  131. // console.log("site_list:", site_list);
  132. let query = new URLSearchParams(location.search).get(current_site.key || "q");
  133. console.log("current_site:", current_site, ",query:", query);
  134. if (query == null) {
  135. return;
  136. }
  137. let body = document.body;
  138. if (body == undefined) {
  139. return;
  140. }
  141. let switcherParentId = "search-switcher-parent";
  142. let switcherParent = document.getElementById(switcherParentId);
  143. if (switcherParent == undefined) {
  144. // 样式
  145. const style = document.createElement("style");
  146. style.innerHTML = css;
  147. body.appendChild(style);
  148. // 生成切换框
  149. switcherParent = document.createElement("div");
  150. switcherParent.setAttribute("id", switcherParentId);
  151. console.log("body.appendChild:", switcherParent);
  152. body.appendChild(switcherParent);
  153. }
  154. const siteTag = ({ link, name, host, mirror, key }) => {
  155. let className = "";
  156. let text = name;
  157. let href = `${link}?${key}=${query}`;
  158. console.log("href:", href);
  159. return `<a href='${href}' target='_blank' >${text}</a>`;
  160. };
  161. const tags = site_list
  162. .filter(({ hidden }) => !hidden)
  163. .map(siteTag)
  164. .join("");
  165.  
  166. switcherParent.innerHTML = `
  167. <div id='search-switcher' class='search-switcher'>
  168. <div id='search-list' class="search-list">${tags}</div>
  169. </div>
  170. `;
  171. console.log("switcherParent:", switcherParent);
  172. }
  173.  
  174. // 监听 pushState 和 replaceState 方法
  175. const originalPushState = history.pushState;
  176. const originalReplaceState = history.replaceState;
  177.  
  178. history.pushState = function () {
  179. originalPushState.apply(this, arguments);
  180. window.dispatchEvent(new Event('urlChange'));
  181. };
  182.  
  183. history.replaceState = function () {
  184. originalReplaceState.apply(this, arguments);
  185. window.dispatchEvent(new Event('urlChange'));
  186. };
  187.  
  188. // 监听 popstate 事件(用于处理浏览器后退和前进)
  189. window.addEventListener('popstate', () => {
  190. window.dispatchEvent(new Event('urlChange'));
  191. });
  192.  
  193. // 自定义的 URL 变化事件处理函数
  194. const handleUrlChange = () => {
  195. console.log('URL changed to: ', window.location.href);
  196. // 这里可以添加你希望执行的代码
  197. setTimeout(function () {
  198. setup();
  199. }, 2000)
  200. };
  201.  
  202. // 监听自定义的 urlChange 事件
  203. window.addEventListener('urlChange', handleUrlChange);
  204.  
  205. // 初始加载时也触发一次
  206. handleUrlChange();
  207. })();
  208. // end userScript

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址