logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

searx_search.js (3440B)


  1. /**
  2. * searx is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU Affero General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * searx is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Affero General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License
  13. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  14. *
  15. * (C) 2017 by Alexandre Flament, <alex@al-f.net>
  16. */
  17. (function(w, d, searx) {
  18. 'use strict';
  19. var firstFocus = true, qinput_id = "q", qinput;
  20. function placeCursorAtEnd(element) {
  21. if (element.setSelectionRange) {
  22. var len = element.value.length;
  23. element.setSelectionRange(len, len);
  24. }
  25. }
  26. function submitIfQuery() {
  27. if (qinput.value.length > 0) {
  28. var search = document.getElementById('search');
  29. setTimeout(search.submit.bind(search), 0);
  30. }
  31. }
  32. function createClearButton(qinput) {
  33. var cs = document.getElementById('clear_search');
  34. var updateClearButton = function() {
  35. if (qinput.value.length === 0) {
  36. cs.classList.add("empty");
  37. } else {
  38. cs.classList.remove("empty");
  39. }
  40. };
  41. // update status, event listener
  42. updateClearButton();
  43. cs.addEventListener('click', function() {
  44. qinput.value='';
  45. qinput.focus();
  46. updateClearButton();
  47. });
  48. qinput.addEventListener('keyup', updateClearButton, false);
  49. }
  50. searx.ready(function() {
  51. qinput = d.getElementById(qinput_id);
  52. function placeCursorAtEndOnce(e) {
  53. if (firstFocus) {
  54. placeCursorAtEnd(qinput);
  55. firstFocus = false;
  56. } else {
  57. // e.preventDefault();
  58. }
  59. }
  60. if (qinput !== null) {
  61. // clear button
  62. createClearButton(qinput);
  63. // autocompleter
  64. if (searx.autocompleter) {
  65. searx.autocomplete = AutoComplete.call(w, {
  66. Url: "./autocompleter",
  67. EmptyMessage: searx.noItemFound,
  68. HttpMethod: searx.method,
  69. MinChars: 4,
  70. Delay: 300,
  71. }, "#" + qinput_id);
  72. // hack, see : https://github.com/autocompletejs/autocomplete.js/issues/37
  73. w.addEventListener('resize', function() {
  74. var event = new CustomEvent("position");
  75. qinput.dispatchEvent(event);
  76. });
  77. }
  78. qinput.addEventListener('focus', placeCursorAtEndOnce, false);
  79. qinput.focus();
  80. }
  81. // vanilla js version of search_on_category_select.js
  82. if (qinput !== null && searx.search_on_category_select) {
  83. d.querySelector('.help').className='invisible';
  84. searx.on('#categories input', 'change', function(e) {
  85. var i, categories = d.querySelectorAll('#categories input[type="checkbox"]');
  86. for(i=0; i<categories.length; i++) {
  87. if (categories[i] !== this && categories[i].checked) {
  88. categories[i].click();
  89. }
  90. }
  91. if (! this.checked) {
  92. this.click();
  93. }
  94. submitIfQuery();
  95. return false;
  96. });
  97. searx.on(d.getElementById('time_range'), 'change', submitIfQuery);
  98. searx.on(d.getElementById('language'), 'change', submitIfQuery);
  99. }
  100. });
  101. })(window, document, window.searx);