Search Engine Switcher (Jerry Modified)

More transparent and working better on both dark and light mode; added stackoverflow, devdocs etc; modified from https://greasyfork.org/en/scripts/446492

目前为 2022-12-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Search Engine Switcher (Jerry Modified)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.27-Jerry
  5. // @description More transparent and working better on both dark and light mode; added stackoverflow, devdocs etc; modified from https://greasyfork.org/en/scripts/446492
  6. // @homepage https://greasyfork.org/en/users/28298
  7. // @author https://greasyfork.org/en/users/28298
  8.  
  9. // @match *://www.baidu.com/s*
  10. // @match *://www.baidu.com/baidu*
  11. // @match *://duckduckgo.com/*
  12. // @match *://www.google.com/search*
  13. // @match *://www.google.com.hk/search*
  14. // @match *://scholar.google.com/*
  15. // @match *://translate.google.com/*
  16. // @match *://weixin.sogou.com/weixin*
  17. // @match *://www.bing.com/search*
  18. // @match *://cn.bing.com/search*
  19. // @match *://www.zhihu.com/search*
  20. // @match *://devdocs.io/*
  21. // @match *://stackoverflow.com/*
  22.  
  23.  
  24. // @grant unsafeWindow
  25. // @grant window.onload
  26. // @grant GM_getValue
  27. // @grant GM_setValue
  28. // @run-at document-body
  29.  
  30. // @license MIT
  31. // ==/UserScript==
  32.  
  33. // 搜索网址配置 (youtube, maps, not show, i.e., 1 way)
  34. const urlMapping = [
  35. {
  36. name: "Google",
  37. searchUrl: "https://www.google.com/search?q=",
  38. keyName: "q",
  39. testUrl: /https:\/\/www.google.com\/search.*/,
  40. },
  41. {
  42. name: "GImages",
  43. searchUrl: "https://www.google.com/search?tbm=isch&q=",
  44. keyName: "q",
  45. testUrl: /https:\/\/www.google.com\/search.*/,
  46. },
  47. {
  48. name: "Youtube*",
  49. searchUrl: "https://www.youtube.com/results?search_query=",
  50. keyName: "search_query",
  51. testUrl: /https:\/\/www.youtube.com\/results.*/,
  52. },
  53. {
  54. name: "GScholars",
  55. searchUrl: "https://scholar.google.com/scholar?hl=en&as_sdt=0%2C14&oq=&q=",
  56. keyName: "q",
  57. testUrl: /https:\/\/scholar.google.com\/scholar.*/,
  58. },
  59. {
  60. name: "GMaps*",
  61. searchUrl: "https://maps.google.com/maps?q=",
  62. keyName: "q",
  63. testUrl: /https:\/\/maps.google.com\/maps.*/,
  64. },
  65. {
  66. name: "GTranslate",
  67. searchUrl: "https://translate.google.com/?sl=auto&tl=en&op=translate&text=",
  68. keyName: "text",
  69. testUrl: /https:\/\/translate.google.com\/*/,
  70. },
  71. {
  72. name: "DuckDuckGo",
  73. searchUrl: "https://duckduckgo.com/?q=",
  74. keyName: "q",
  75. testUrl: /https:\/\/duckduckgo.com\/*/,
  76. },
  77. {
  78. name: "Bing",
  79. searchUrl: "https://www.bing.com/search?q=",
  80. keyName: "q",
  81. testUrl: /https:\/\/www.bing.com\/search.*/,
  82. },
  83. {
  84. name: "Baidu",
  85. searchUrl: "https://www.baidu.com/s?wd=",
  86. keyName: "wd",
  87. testUrl: /https:\/\/www.baidu.com\/s.*/,
  88. },
  89. {
  90. name: "Wechat",
  91. searchUrl: "https://weixin.sogou.com/weixin?type=2&s_from=input&query=",
  92. keyName: "query",
  93. testUrl: /https:\/\/weixin.sogou.com\/weixin.*/,
  94. },
  95. {
  96. name: "Zhihu",
  97. searchUrl: "https://www.zhihu.com/search?q=",
  98. keyName: "q",
  99. testUrl: /https:\/\/www.zhihu.com\/search.*/,
  100. },
  101. {
  102. name: "DevDocs",
  103. searchUrl: "https://devdocs.io/#q=",
  104. keyName: "q",
  105. testUrl: /https:\/\/devdocs.io\/*/,
  106. },
  107. {
  108. name: "StackOverflow",
  109. searchUrl: "https://stackoverflow.com/search?q=",
  110. keyName: "q",
  111. testUrl: /https:\/\/stackoverflow.com\/search.*/,
  112. },
  113. ];
  114.  
  115. // JS获取url参数
  116. function getQueryVariable(variable) {
  117. let query = window.location.search.substring(1);
  118. let pairs = query.split("&");
  119. for (let pair of pairs) {
  120. let [key, value] = pair.split("=");
  121. if (key == variable) {
  122. return decodeURIComponent(value);
  123. }
  124. }
  125. return null;
  126. }
  127.  
  128. // 从url中获取搜索关键词
  129. function getKeywords() {
  130. let keywords = "";
  131. for (let item of urlMapping) {
  132. if (item.testUrl.test(window.location.href)) {
  133. keywords = getQueryVariable(item.keyName);
  134. break;
  135. }
  136. }
  137. console.log(keywords);
  138. return keywords;
  139. }
  140.  
  141. // 适配火狐浏览器的百度搜索
  142. const isFirefox = () => {
  143. if (navigator.userAgent.indexOf("Firefox") > 0) {
  144. console.warn("[ Firefox ] 🚀");
  145. for (var i = 0; i < urlMapping.length; i++) {
  146. if (urlMapping[i]['name']=='Baidu') {
  147. break;
  148. }
  149. }
  150. urlMapping[i].searchUrl = "https://www.baidu.com/baidu?wd=";
  151. urlMapping[i].testUrl = /https:\/\/www.baidu.com\/baidu.*/;
  152. } else {
  153. return;
  154. }
  155. };
  156.  
  157. // 适配cn.bing.com的必应域名
  158. const cnBing = {
  159. name: "Bing",
  160. searchUrl: "https://cn.bing.com/search?q=",
  161. keyName: "q",
  162. testUrl: /https:\/\/cn.bing.com\/search.*/,
  163. };
  164.  
  165. // 匹配到cn.bing就修改必应配置对象
  166. if(window.location.hostname === 'cn.bing.com'){
  167. for(let item of urlMapping){
  168. if(item.name === "Bing"){
  169. item = cnBing
  170. }
  171. }
  172. }
  173.  
  174. // 添加节点
  175. function addBox() {
  176. isFirefox();
  177. // 主元素
  178. const div = document.createElement("div");
  179. div.id = "search-app-box";
  180. div.style = `
  181. position: fixed;
  182. top: 140px;
  183. right: 12px;
  184. width: 100px;
  185. background-color: hsla(200, 40%, 96%, .0);
  186. font-size: 12px;
  187. border-radius: 6px;
  188. z-index: 99999;`;
  189. document.body.insertAdjacentElement("afterbegin", div);
  190.  
  191. // 标题
  192. let title = document.createElement("span");
  193. title.innerText = " ";
  194. title.style = `
  195. display: block;
  196. color: hsla(211, 20%, 64%, .4);
  197. text-align: center;
  198. margin-top: 10px;
  199. margin-bottom: 5px;
  200. font-size: 12px;
  201. font-weight: bold;
  202. -webkit-user-select:none;
  203. -moz-user-select:none;
  204. -ms-user-select:none;
  205. user-select:none;`;
  206. div.appendChild(title);
  207.  
  208. // 搜索列表
  209. for (let index in urlMapping) {
  210. let item = urlMapping[index];
  211.  
  212. // 列表样式
  213. let style = `
  214. display: block;
  215. color: hsla(225, 9%, 36%, .8) !important;
  216. padding: 8px;
  217. text-decoration: none;`;
  218. let defaultStyle = style + "color: hsla(211, 20%, 64%, .4) !important;";
  219. let hoverStyle =
  220. style + "background-color: hsla(211, 20%, 64%, .1); color: hsla(211, 100%, 100%, .9) !important;";
  221.  
  222. // 设置搜索引擎链接
  223. let a = document.createElement("a");
  224. a.innerText = item.name;
  225. a.style = defaultStyle;
  226. a.className = "search-engine-a";
  227. a.href = item.searchUrl + getKeywords();
  228.  
  229. // 鼠标移入&移出效果,相当于hover
  230. a.onmouseenter = function () {
  231. this.style = hoverStyle;
  232. };
  233. a.onmouseleave = function () {
  234. this.style = defaultStyle;
  235. };
  236. div.appendChild(a);
  237. }
  238. }
  239.  
  240. (function () {
  241. "use strict";
  242. window.onload = addBox();
  243. })();