Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

keyword.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. $(function () {
  2. // 默认搜索引擎记录
  3. var searchTypeStore = {
  4. set: function (type) {
  5. localStorage.setItem('SearchType', type);
  6. },
  7. get: function () {
  8. return localStorage.getItem('SearchType') || 'bing';
  9. },
  10. };
  11. var $searchMethods = $('#search_methods');
  12. var $searchLogo = $('#search_logo');
  13. var initSearchType = searchTypeStore.get();
  14. $searchLogo.addClass(initSearchType).data('type', initSearchType);
  15. var search_types = [
  16. { url: 'https://www.baidu.com/s?wd=', type: 'baidu' },
  17. { url: 'https://www.sogou.com/web?query=', type: 'sogou' },
  18. { url: 'https://cn.bing.com/search?q=', type: 'bing' },
  19. { url: 'https://www.so.com/s?q=', type: 'so' },
  20. { url: 'https://www.google.com/search?q=', type: 'google' },
  21. { url: 'http://www.cilimao.cc/search?word=', type: 'cili' },
  22. { url: 'http://neets.cc/search?key=', type: 'yingyin' },
  23. { url: 'http://www.panduoduo.net/s/name/', type: 'wangpan' },
  24. ];
  25. $searchLogo.on('click', function () {
  26. $searchMethods.fadeIn(100);
  27. });
  28. // 搜索引擎切换
  29. $searchMethods.on('click', 'li', function () {
  30. var type = $(this).data('type');
  31. searchTypeStore.set(type);
  32. $searchLogo.removeClass()
  33. .data('type', type)
  34. .addClass(type + ' search-logo');
  35. $searchMethods.fadeOut(100);
  36. $('#search_keyword').focus();
  37. });
  38. $searchMethods.on('mouseleave', function () {
  39. $searchMethods.fadeOut(100);
  40. });
  41. var EVENT_CLEAR_KEYWORD = 'clearKeyword';
  42. var EVENT_SEARCH = 'search';
  43. // 关键词搜索输入
  44. $('#search_keyword').on('keyup', function (event) {
  45. var keyword = $(this).val();
  46. if(event.which==13){
  47. if($('#search_result .active').length>0){
  48. keyword = $('#search_result .active').eq(0).text();
  49. }
  50. openSearch(keyword)
  51. return;
  52. }
  53. // TODO 上下键选择待选答案
  54. var bl = moveChange(event);
  55. if(bl){
  56. keywordChange(keyword);
  57. }
  58. }).on('blur', function () {
  59. // 推荐结果跳转
  60. $('#search_result').on('click', 'li', function () {
  61. var word = $(this).text();
  62. $('#search_keyword').val(word);
  63. openSearch(word);
  64. $('#search_result').hide();
  65. });
  66. // 解决失焦和点击事件冲突问题
  67. setTimeout(function() {
  68. $('#search_result').hide();
  69. }, 100)
  70. }).on('focus', function () {
  71. var keyword = $(this).val();
  72. keywordChange(keyword);
  73. });
  74. function moveChange(e){
  75. var k = e.keyCode || e.which;
  76. var bl = true;
  77. switch(k){
  78. case 38:
  79. rowMove('top');
  80. bl = false;
  81. break;
  82. case 40:
  83. rowMove('down');
  84. bl = false;
  85. break;
  86. }
  87. return bl;
  88. }
  89. function rowMove(move){
  90. var search_result = $('#search_result');
  91. var hove_li = null;
  92. search_result.find('.result-item').each(function(){
  93. if($(this).hasClass('active')){
  94. hove_li = $(this).index();
  95. }
  96. });
  97. if(move == 'top'){
  98. if(hove_li==null){
  99. hove_li = search_result.find('.result-item').length-1;
  100. }else{
  101. hove_li--;
  102. }
  103. }else if(move == 'down'){
  104. if(hove_li==null){
  105. hove_li = 0;
  106. }else{
  107. hove_li==search_result.find('.result-item').length-1?(hove_li=0):(hove_li++);
  108. }
  109. }
  110. search_result.find('.active').removeClass('active');
  111. search_result.find('.result-item').eq(hove_li).addClass('active');
  112. $('#search_keyword').val(search_result.find('.result-item').eq(hove_li).addClass('active').text());
  113. }
  114. function keywordChange(keyword) {
  115. if (keyword === '') {
  116. $(document).trigger(EVENT_CLEAR_KEYWORD);
  117. } else {
  118. $(document).trigger(EVENT_SEARCH, keyword);
  119. $('#clear_keyword').show();
  120. }
  121. }
  122. // 清空输入框
  123. $('#clear_keyword').on('click', function () {
  124. $('#search_keyword').val('');
  125. $('#search_keyword').focus();
  126. $(document).trigger(EVENT_CLEAR_KEYWORD);
  127. });
  128. // 搜索
  129. $('#search_submit').on('click', function () {
  130. var keyword = $('#search_keyword').val();
  131. var type = getSeachType();
  132. var baseUrl = search_types.find(function (item) {
  133. return item.type === type;
  134. });
  135. if (baseUrl && keyword) {
  136. window.open(baseUrl.url + keyword);
  137. }
  138. });
  139. $(document).on(EVENT_CLEAR_KEYWORD, function () {
  140. $('#clear_keyword').hide();
  141. $('#search_result').hide();
  142. });
  143. $(document).on(EVENT_SEARCH, function (e, keyword) {
  144. getSearchResult(keyword);
  145. });
  146. // 获取搜索引擎类型
  147. function getSeachType() {
  148. return $('#search_logo').data('type');
  149. }
  150. // google 搜索结果
  151. function searchResultGoogle(data) {
  152. var result = data[1];
  153. result = result.map(function (item) {
  154. return item[0];
  155. });
  156. renderSearchResult(result);
  157. }
  158. // 百度 搜索结果
  159. function searchResultBaidu(data) {
  160. if (data === undefined) {
  161. return;
  162. }
  163. var result = data.s;
  164. renderSearchResult(result);
  165. }
  166. // 渲染搜索结果
  167. function renderSearchResult(array) {
  168. var $result = $('#search_result');
  169. $result.empty().hide();
  170. if (!array || array.length <= 9) {
  171. return;
  172. }
  173. for (var i = 4; i < array.length; i++) {
  174. var $li = $('<li class=\'result-item\'></li>');
  175. $li.text(array[i]);
  176. $result.append($li);
  177. }
  178. $result.show();
  179. }
  180. window.searchResultGoogle = searchResultGoogle;
  181. window.searchResultBaidu = searchResultBaidu;
  182. var search_suggest = {
  183. baidu: {
  184. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
  185. data: function (keyword) {
  186. return {
  187. wd: keyword,
  188. cb: 'window.searchResultBaidu',
  189. };
  190. },
  191. },
  192. google: {
  193. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
  194. data: function (keyword) {
  195. return {
  196. wd: keyword,
  197. cb: 'window.searchResultBaidu',
  198. };
  199. },
  200. },
  201. google2: {
  202. url: 'http://suggestqueries.google.com/complete/search',
  203. data: function (keyword) {
  204. return {
  205. q: keyword,
  206. jsonp: 'window.searchResultGoogle',
  207. client: 'youtube',
  208. };
  209. },
  210. },
  211. wangpan: {
  212. url: 'http://unionsug.baidu.com/su',
  213. data: function (keyword) {
  214. return {
  215. wd: keyword,
  216. cb: 'window.searchResultBaidu',
  217. };
  218. },
  219. },
  220. };
  221. function getSearchResult(keyword) {
  222. var searchType = getSeachType();
  223. var suggest = search_suggest[searchType];
  224. if (!suggest) {
  225. suggest = search_suggest.baidu;
  226. }
  227. $.ajax({
  228. url: suggest.url,
  229. dataType: 'jsonp',
  230. data: suggest.data(keyword),
  231. });
  232. }
  233. function openSearch(keyword) {
  234. var type = getSeachType();
  235. var baseUrl = search_types.find(function (item) {
  236. return item.type === type;
  237. });
  238. if (baseUrl && keyword) {
  239. window.open(baseUrl.url + keyword, keyword);
  240. }
  241. }
  242. });