一键切换搜索

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

目前为 2020-06-14 提交的版本,查看 最新版本

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