一键切换搜索/镜像站

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

  1. // ==UserScript==
  2. // @name Site 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.  
  9. // @author l1xnan
  10. // @namespace http://hzy.pw
  11. // @homepageURL http://hzy.pw/p/1849
  12. // @supportURL https://github.com/h2y/link-fix
  13. // @icon http://q.qlogo.cn/qqapp/100229475/F1260A6CECA521F6BE517A08C4294D8A/100
  14. // @license GPL-3.0
  15. // @include https://github.com/*
  16. // @include https://github.com.cnpmjs.org/*
  17. // @include *.baidu.com/*
  18. // @exclude *.baidu.com/link?*
  19. // @include *.so.com/*
  20. // @include *.bing.com/*
  21. // @include *.zhihu.com/search?*
  22. // @include *.soku.com/*
  23. // @include *.sogou.com/*
  24. // @include /^https?://[a-z]+\.google\.[a-z,\.]+/.+$/
  25. // @grant none
  26. // @run-at document_end
  27.  
  28. // @date 02/16/2020
  29. // @modified 02/16/2020
  30. // @version 1.0.0
  31. // ==/UserScript==
  32.  
  33. {
  34. const sites = [
  35. {
  36. name: '百度',
  37. host: 'baidu.com',
  38. link: 'https://www.baidu.com/s',
  39. key: 'wd',
  40. },
  41. {
  42. name: '必应',
  43. host: 'bing.com',
  44. link: 'https://bing.com/search',
  45. key: 'q',
  46. },
  47. {
  48. name: '谷歌',
  49. host: 'www.google.com',
  50. link: 'https://www.google.com/search',
  51. key: 'q',
  52. mirror: 'google.fuckcloudnative.io',
  53. },
  54. {
  55. name: 'Github',
  56. host: 'github.com',
  57. link: 'https://github.com/search',
  58. key: 'q',
  59. mirror: 'github.com.cnpmjs.org',
  60. },
  61. // {
  62. // name: '搜搜',
  63. // host: 'so.com',
  64. // link: 'https://www.so.com/s?q=',
  65. // key: 'q',
  66. // hidden: true,
  67. // },
  68. // {
  69. // name: '搜狗',
  70. // host: 'sogou.com',
  71. // link: 'https://www.sogou.com/web?query=',
  72. // key: 'query',
  73. // hidden: true,
  74. // },
  75. ];
  76.  
  77. // 镜像站
  78. const mirrors = {
  79. 'github.com': 'https://github.com.cnpmjs.org/search?q=',
  80. 'google.com': 'https://google.fuckcloudnative.io/search?q=',
  81. };
  82. const css = `
  83. .search-switcher {
  84. position: fixed;
  85. box-sizing:border-box;
  86. background-color: #000;
  87. opacity: 0.3;
  88. border-radius: 40px;
  89. color: #fff;
  90. padding: 15px 20px;
  91. bottom: 100px;
  92. left: -280px;
  93. width: 300px;
  94. z-index: 9999999;
  95. transition: all 400ms;
  96. }
  97. .search-switcher:hover {
  98. left: 5px;
  99. opacity: 1;
  100. border-radius: 10px;
  101. box-shadow: 5px -5px 10px #777;
  102. }
  103. .search-switcher p {
  104. margin: 0;
  105. }
  106. p.search-switcher-title {
  107. font-weight: bold;
  108. margin-bottom: 5px;
  109. }
  110. .search-switcher a {
  111. color: #0cf;
  112. margin-right: 20px;
  113. }
  114. .search-switcher a.mirror {
  115. font-weight: bold;
  116. }
  117. `;
  118.  
  119. function setup() {
  120. let site;
  121. let isMirror;
  122. for (let s of sites) {
  123. if (location.host.includes(s.host)) {
  124. site = s;
  125. }
  126. if (location.href.includes(s.mirror)) {
  127. site = s;
  128. isMirror = true;
  129. }
  130. }
  131. sites.filter(({ host }) => location.hostname.includes(host))[0];
  132. let mirror = sites.filter(({ mirror }) =>
  133. location.hostname.includes(mirror),
  134. )[0];
  135.  
  136. let query = new URLSearchParams(location.search).get(site.key || 'q');
  137. console.log(site, query, isMirror);
  138. const body = document.getElementsByTagName('body')[0];
  139.  
  140. // 样式
  141. const style = document.createElement('style');
  142. style.innerHTML = css;
  143. body.appendChild(style);
  144.  
  145. // 生成切换框
  146. const content = document.createElement('div');
  147. const aTag = ({ link, name, host, mirror, key }) => {
  148. let className = '';
  149. let text = name;
  150. let href = `${link}?${key}=${query}`;
  151. if (mirror && name === site.name) {
  152. href = location.href.replace(host, mirror);
  153. text = '镜像';
  154. className = 'mirror';
  155. if (isMirror) {
  156. text = '原站';
  157. href = location.href.replace(mirror, host);
  158. }
  159. }
  160. return `<a href='${href}' target='_blank' class=${className}>${text}</a>`;
  161. };
  162. const tags = sites
  163. .filter(({ hidden }) => !hidden)
  164. .map(aTag)
  165. .join('');
  166.  
  167. content.innerHTML = `
  168. <div id='search-switcher' class='search-switcher'>
  169. <p class='search-switcher-title'>一键切换引擎:</p>
  170. <p>${tags}</p>
  171. </div>`;
  172. body.appendChild(content);
  173. }
  174.  
  175. let href0 = '';
  176.  
  177. !(function init() {
  178. var href = location.href;
  179. if (href0 != href) {
  180. var oldDOM = document.getElementById('search-switcher');
  181. if (oldDOM) {
  182. oldDOM.parentNode.removeChild(oldDOM);
  183. }
  184. setup();
  185. href0 = href;
  186. }
  187. setTimeout(init, 2222);
  188. })();
  189. }
  190. //end userScript

QingJ © 2025

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