十三搜索平台切换 / Search Engine Switcher

在搜索引擎左侧显示一个快速切换列表,节省「另开搜索引擎」和「输入关键词」的动作和时间,提高搜索效率。修改自原作者的基础上,添加了即刻、小红书、知识星球等平台。

  1. // ==UserScript==
  2. // @name 十三搜索平台切换 / Search Engine Switcher
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.26
  5. // @description 在搜索引擎左侧显示一个快速切换列表,节省「另开搜索引擎」和「输入关键词」的动作和时间,提高搜索效率。修改自原作者的基础上,添加了即刻、小红书、知识星球等平台。
  6. // @author https://twitter.com/rockucn
  7.  
  8. // @match *://www.baidu.com/s*
  9. // @match *://www.baidu.com/baidu*
  10. // @match *://www.google.com/search*
  11. // @match *://www.google.com.hk/search*
  12. // @match *://weixin.sogou.com/weixin*
  13. // @match *://www.bing.com/search*
  14. // @match *://cn.bing.com/search*
  15. // @match *://www.zhihu.com/search*
  16. // @match *://search.cnki.com.cn/Search/Result*
  17. // @match *://web.okjike.com/search*
  18. // @match *://www.xiaohongshu.com/search_result*
  19. // @match *://scys.com/search*
  20. // @grant unsafeWindow
  21. // @grant window.onload
  22. // @grant GM_getValue
  23. // @grant GM_setValue
  24. // @run-at document-body
  25. // @license MIT
  26. // ==/UserScript==
  27.  
  28. // 搜索网址配置
  29. const urlMapping = [
  30. {
  31. name: "Google",
  32. searchUrl: "https://www.google.com/search?q=",
  33. keyName: "q",
  34. testUrl: /https:\/\/www.google.com\/search.*/,
  35. },
  36. {
  37. name: "百度",
  38. searchUrl: "https://www.baidu.com/s?wd=",
  39. keyName: "wd",
  40. testUrl: /https:\/\/www.baidu.com\/s.*/,
  41. },
  42. {
  43. name: "即刻",
  44. searchUrl: "https://web.okjike.com/search?keyword=",
  45. keyName: "keyword",
  46. testUrl: /https:\/\/web.okjike.com\/search.*/,
  47. },
  48. {
  49. name: "小红书",
  50. searchUrl: "https://www.xiaohongshu.com/search_result?keyword=",
  51. keyName: "keyword",
  52. testUrl: /https:\/\/www.xiaohongshu.com\/search_result.*/,
  53. },
  54. {
  55. name: "知识星球",
  56. searchUrl: "https://scys.com/search?query=",
  57. keyName: "query",
  58. testUrl: /https:\/\/scys.com\/search.*/,
  59. },
  60. {
  61. name: "知乎",
  62. searchUrl: "https://www.zhihu.com/search?q=",
  63. keyName: "q",
  64. testUrl: /https:\/\/www.zhihu.com\/search.*/,
  65. },
  66. ];
  67.  
  68. // JS获取url参数
  69. function getQueryVariable(variable) {
  70. let query = window.location.search.substring(1);
  71. let pairs = query.split("&");
  72. for (let pair of pairs) {
  73. let [key, value] = pair.split("=");
  74. if (key == variable) {
  75. return decodeURIComponent(value);
  76. }
  77. }
  78. return null;
  79. }
  80.  
  81. // 从url中获取搜索关键词
  82. function getKeywords() {
  83. let keywords = "";
  84. for (let item of urlMapping) {
  85. if (item.testUrl.test(window.location.href)) {
  86. keywords = getQueryVariable(item.keyName);
  87. break;
  88. }
  89. }
  90. console.log(keywords);
  91. return keywords;
  92. }
  93.  
  94. // 适配火狐浏览器的百度搜索
  95. const isFirefox = () => {
  96. if (navigator.userAgent.indexOf("Firefox") > 0) {
  97. console.warn("[ Firefox ] 🚀");
  98. urlMapping[0].searchUrl = "https://www.baidu.com/baidu?wd=";
  99. urlMapping[0].testUrl = /https:\/\/www.baidu.com\/baidu.*/;
  100. } else {
  101. return;
  102. }
  103. };
  104.  
  105. // 适配cn.bing.com的必应域名
  106. const cnBing = {
  107. name: "Bing",
  108. searchUrl: "https://cn.bing.com/search?q=",
  109. keyName: "q",
  110. testUrl: /https:\/\/cn.bing.com\/search.*/,
  111. };
  112.  
  113. // 匹配到cn.bing就修改必应配置对象
  114. if(window.location.hostname === 'cn.bing.com'){
  115. for(let item of urlMapping){
  116. if(item.name === "Bing"){
  117. item = cnBing
  118. }
  119. }
  120. }
  121.  
  122. // 添加节点
  123. function addBox() {
  124. isFirefox();
  125. // 主元素
  126. const div = document.createElement("div");
  127. div.id = "search-app-box";
  128. div.style = `
  129. position: fixed;
  130. top: 140px;
  131. left: 12px;
  132. width: 88px;
  133. background-color: hsla(200, 40%, 96%, .8);
  134. font-size: 12px;
  135. border-radius: 6px;
  136. z-index: 99999;`;
  137. document.body.insertAdjacentElement("afterbegin", div);
  138.  
  139. // 标题
  140. let title = document.createElement("span");
  141. title.innerText = "搜索引擎";
  142. title.style = `
  143. display: block;
  144. color: hsla(211, 60%, 35%, .8);
  145. text-align: center;
  146. margin-top: 10px;
  147. margin-bottom: 5px;
  148. font-size: 12px;
  149. font-weight: bold;
  150. -webkit-user-select:none;
  151. -moz-user-select:none;
  152. -ms-user-select:none;
  153. user-select:none;`;
  154. div.appendChild(title);
  155.  
  156. // 搜索列表
  157. for (let index in urlMapping) {
  158. let item = urlMapping[index];
  159.  
  160. // 列表样式
  161. let style = `
  162. display: block;
  163. color: hsla(211, 60%, 35%, .8) !important;
  164. padding: 8px;
  165. text-decoration: none;`;
  166. let defaultStyle = style + "color: hsla(211, 60%, 35%, .8) !important;";
  167. let hoverStyle =
  168. style + "background-color: hsla(211, 60%, 35%, .1);";
  169.  
  170. // 设置搜索引擎链接
  171. let a = document.createElement("a");
  172. a.innerText = item.name;
  173. a.style = defaultStyle;
  174. a.className = "search-engine-a";
  175. a.href = item.searchUrl + getKeywords();
  176.  
  177. // 鼠标移入&移出效果,相当于hover
  178. a.onmouseenter = function () {
  179. this.style = hoverStyle;
  180. };
  181. a.onmouseleave = function () {
  182. this.style = defaultStyle;
  183. };
  184. div.appendChild(a);
  185. }
  186. }
  187.  
  188. (function () {
  189. "use strict";
  190. window.onload = addBox();
  191. })();

QingJ © 2025

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