搜索引擎一键切换 soTab

在常用的搜索引擎页面中添加互相切换的按钮,包括图片、视频、知道、学术搜索。

当前为 2016-06-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 搜索引擎一键切换 soTab
  3. // @namespace http://hzy.pw
  4. // @description 在常用的搜索引擎页面中添加互相切换的按钮,包括图片、视频、知道、学术搜索。
  5. // @author Moshel
  6. // @homepageURL http://hzy.pw/p/1849
  7. // @license Apache License 2.0
  8. // @supportURL https://github.com/h2y/sotab
  9. // @icon http://q.qlogo.cn/qqapp/100229475/F1260A6CECA521F6BE517A08C4294D8A/100
  10.  
  11. // @include *.baidu.com/*
  12. // @exclude *.baidu.com/link?*
  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 10/30/2015
  23. // @modified 06/06/2016
  24. // @version 1.3.1.9
  25. // ==/UserScript==
  26.  
  27.  
  28. var href0 = "";
  29. ! function ajax_fixer() {
  30. var href = location.href;
  31. if (href0 != href) {
  32. var oldDOM = document.getElementById('soTab');
  33. if (oldDOM) {
  34. oldDOM.parentNode.removeChild(oldDOM);
  35. }
  36. soTab_init();
  37. href0 = href;
  38. }
  39. setTimeout(ajax_fixer, 2222);
  40. }();
  41.  
  42.  
  43. function soTab_init() {
  44. //console.log("soTab开始加载");
  45.  
  46. if (top != window) {
  47. console.log("soTab! not top window");
  48. return;
  49. }
  50.  
  51. //判断搜索引擎,将仅使用hostname适配
  52. var site = ["baidu", "bing", "so.com", "", "zhihu", "google", "", "soku", "sogou"],
  53. siteName = ["百度", "必应", "好搜", "ALLSO", "知乎", "谷歌", "清澄漫语", "搜库", "搜狗"],
  54. siteID = -1;
  55. for (var i = 0; i < site.length; i++) {
  56. if (site[i] && location.hostname.indexOf(site[i]) >= 0) {
  57. siteID = i;
  58. break;
  59. }
  60. }
  61. if (siteID == -1) {
  62. console.log("soTab can't match site.");
  63. return;
  64. }
  65.  
  66. //判断搜索类型,使用href适配
  67. var kind = [];
  68. switch (siteID) {
  69. case 0:
  70. kind = ["www.baidu", "image.baidu", "zhidao.baidu.com/search", "v.baidu", "xueshu.baidu.com/s"];
  71. break;
  72. case 1: //bing
  73. kind = [".com/search", ".com/images", ".com/knows/search", ".com/videos", "/academic/search"];
  74. break;
  75. case 2:
  76. kind = ["www.so", "image.so", "wenda.so.com/search", "video.so"];
  77. break;
  78. case 4: //zhihu
  79. kind = ["", "", ".com/search"];
  80. break;
  81. case 5: //google
  82. kind = ["", "tbm=isch", "", "tbm=vid", "scholar.google"];
  83. break;
  84. case 7:
  85. //kind[3] = "soku";
  86. break;
  87. case 8: //sogou
  88. kind = [[
  89. "/web?", "weixin.sogou", "english.sogou"
  90. ], "pic.sogou", [
  91. "interation=196636", "mingyi.sogou", "wenwen.sogou", ".com/zhihu"
  92. ], "v.sogou", ".com/xueshu"];
  93. break;
  94. }
  95. //0:normal 1:pic 2:zhidao 3:video 4:xueshu
  96. var kindID = -1;
  97. for (i = 0; i < kind.length; i++) {
  98. if (!kind[i])
  99. continue;
  100. else if (Array.isArray(kind[i])) { //数组形式
  101. for (var j=0; j<kind[i].length; j++ )
  102. if(location.href.indexOf(kind[i][j]) >= 0) {
  103. kindID = i;
  104. break;
  105. }
  106. if(kindID!=-1)
  107. break;
  108. }
  109. else if (location.href.indexOf(kind[i]) >= 0) {
  110. kindID = i;
  111. break;
  112. }
  113. }
  114. //谷歌特殊处理
  115. if (siteID == 5 && kindID == -1) {
  116. if (location.href.indexOf('q=') >= 0)
  117. kindID = 0;
  118. }
  119. if (kindID == -1) {
  120. console.log("soTab! no kind found");
  121. return;
  122. }
  123.  
  124. //console.log("soTab loaded: " + siteID + "." + kindID);
  125.  
  126. //初始化搜索路径
  127. //"百度", "必应", "好搜", "ALLSO", "知乎", "谷歌", "清澄漫语", "搜库", "搜狗"
  128. var link = []; //link[siteID]
  129. if (kindID == 0) { //normal
  130. link = ["//www.baidu.com/s?wd=",
  131. "//cn.bing.com/search?q=",
  132. "//www.so.com/s?q=",
  133. "http://h2y.github.io/allso/?so=",
  134. "",
  135. "https://www.google.com/search?q=",
  136. "", "",
  137. "https://www.sogou.com/web?query="
  138. ];
  139. } else if (kindID == 1) { //pic
  140. link = ["//image.baidu.com/search/index?tn=baiduimage&word=",
  141. "//cn.bing.com/images/search?q=",
  142. "//image.so.com/i?q=",
  143. "", "",
  144. "https://www.google.com/search?tbm=isch&q="
  145. ];
  146. } else if (kindID == 2) { //zhidao
  147. link = ["//zhidao.baidu.com/search?word=",
  148. "//cn.bing.com/knows/search?q=",
  149. "", "",
  150. "//www.zhihu.com/search?q=",
  151. "", "", "",
  152. "http://www.sogou.com/sogou?interation=196636&query="
  153. ];
  154. } else if (kindID == 3) { //video
  155. link = ["//v.baidu.com/v?ie=utf-8&word=",
  156. "",
  157. "//video.so.com/v?q=",
  158. "", "",
  159. "https://www.google.com/search?tbm=vid&q=",
  160. "http://hzy.pw/dm/?s=",
  161. "//www.soku.com/v?keyword="
  162. ];
  163. } else if (kindID == 4) { //xueshu
  164. link = ["http://xueshu.baidu.com/s?wd=",
  165. "https://cn.bing.com/academic/search?q=",
  166. "", "", "",
  167. "https://scholar.google.com/scholar?q="
  168. ];
  169. }
  170.  
  171. //获取搜索词(get通用)
  172. var key;
  173. if (siteID == 0)
  174. key = (location.search.indexOf("wd=") >= 0) ? "wd" : "word";
  175. /*else if (siteID == 7)
  176. key = "keyword";*/
  177. else if (siteID == 8)
  178. key = (location.search.indexOf("query=") >= 0) ? "query" : 'w';
  179. else
  180. key = "q";
  181. var tmp = location.href.split(key + "=", 2);
  182. if (tmp.length <= 1) {
  183. console.log("soTab! no keyword found");
  184. return;
  185. }
  186. var tmp2 = tmp[1];
  187. tmp = tmp2.split("&", 2);
  188. key = tmp[0];
  189.  
  190. //console.log(key);
  191.  
  192. //加载css
  193. var dom = document.createElement('style'),
  194. dom_body = document.getElementsByTagName("body")[0];
  195. dom.innerHTML = '.soTab{position:fixed;background-color:#000;opacity:.3;border-radius:40px;color:#fff;padding:15px 20px;bottom:100px;height:40px;left:-320px;width:300px;z-index:9999999;transition:all 400ms}.soTab:hover{left:5px;opacity:1;border-radius:10px;box-shadow:5px -5px 10px #777}.soTab p{margin:0}p.soTab_title{font-weight:bold;margin-bottom:3px}.soTab a{color:#0cf;margin-right:1rem}';
  196. dom_body.appendChild(dom);
  197.  
  198. //生成切换框
  199. dom = document.createElement('div');
  200. dom.id = "soTab";
  201. var str = "<p class='soTab_title'>soTab 一键切换引擎:</p><p>";
  202. for (i = 0; i < link.length; i++) {
  203. if (i != siteID && link[i]) {
  204. str += "<a href='" + link[i] + key + "' target='_blank'>" + siteName[i] + "</a>";
  205. }
  206. }
  207. dom.innerHTML = str + '</p>';
  208. dom.className = "soTab soTab_site" + siteID + " soTab_kind" + kindID;
  209. dom_body.appendChild(dom);
  210.  
  211. //console.log("soTab all run!");
  212.  
  213. }