logo

searx

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

vim_hotkeys.js (9743B)


  1. $(document).ready(function() {
  2. highlightResult('top')();
  3. $('.result').on('click', function() {
  4. highlightResult($(this))();
  5. });
  6. var vimKeys = {
  7. 27: {
  8. key: 'Escape',
  9. fun: removeFocus,
  10. des: 'remove focus from the focused input',
  11. cat: 'Control'
  12. },
  13. 73: {
  14. key: 'i',
  15. fun: searchInputFocus,
  16. des: 'focus on the search input',
  17. cat: 'Control'
  18. },
  19. 66: {
  20. key: 'b',
  21. fun: scrollPage(-window.innerHeight),
  22. des: 'scroll one page up',
  23. cat: 'Navigation'
  24. },
  25. 70: {
  26. key: 'f',
  27. fun: scrollPage(window.innerHeight),
  28. des: 'scroll one page down',
  29. cat: 'Navigation'
  30. },
  31. 85: {
  32. key: 'u',
  33. fun: scrollPage(-window.innerHeight / 2),
  34. des: 'scroll half a page up',
  35. cat: 'Navigation'
  36. },
  37. 68: {
  38. key: 'd',
  39. fun: scrollPage(window.innerHeight / 2),
  40. des: 'scroll half a page down',
  41. cat: 'Navigation'
  42. },
  43. 71: {
  44. key: 'g',
  45. fun: scrollPageTo(-document.body.scrollHeight, 'top'),
  46. des: 'scroll to the top of the page',
  47. cat: 'Navigation'
  48. },
  49. 86: {
  50. key: 'v',
  51. fun: scrollPageTo(document.body.scrollHeight, 'bottom'),
  52. des: 'scroll to the bottom of the page',
  53. cat: 'Navigation'
  54. },
  55. 75: {
  56. key: 'k',
  57. fun: highlightResult('up'),
  58. des: 'select previous search result',
  59. cat: 'Results'
  60. },
  61. 74: {
  62. key: 'j',
  63. fun: highlightResult('down'),
  64. des: 'select next search result',
  65. cat: 'Results'
  66. },
  67. 80: {
  68. key: 'p',
  69. fun: pageButtonClick(0),
  70. des: 'go to previous page',
  71. cat: 'Results'
  72. },
  73. 78: {
  74. key: 'n',
  75. fun: pageButtonClick(1),
  76. des: 'go to next page',
  77. cat: 'Results'
  78. },
  79. 79: {
  80. key: 'o',
  81. fun: openResult(false),
  82. des: 'open search result',
  83. cat: 'Results'
  84. },
  85. 84: {
  86. key: 't',
  87. fun: openResult(true),
  88. des: 'open the result in a new tab',
  89. cat: 'Results'
  90. },
  91. 82: {
  92. key: 'r',
  93. fun: reloadPage,
  94. des: 'reload page from the server',
  95. cat: 'Control'
  96. },
  97. 72: {
  98. key: 'h',
  99. fun: toggleHelp,
  100. des: 'toggle help window',
  101. cat: 'Other'
  102. }
  103. };
  104. $(document).keydown(function(e) {
  105. // check for modifiers so we don't break browser's hotkeys
  106. if (vimKeys.hasOwnProperty(e.keyCode)
  107. && !e.ctrlKey
  108. && !e.altKey
  109. && !e.shiftKey
  110. && !e.metaKey)
  111. {
  112. if (e.keyCode === 27) {
  113. if (e.target.tagName.toLowerCase() === 'input') {
  114. vimKeys[e.keyCode].fun();
  115. }
  116. } else {
  117. if (e.target === document.body) {
  118. e.preventDefault();
  119. vimKeys[e.keyCode].fun();
  120. }
  121. }
  122. }
  123. });
  124. function highlightResult(which) {
  125. return function() {
  126. var current = $('.result[data-vim-selected]');
  127. if (current.length === 0) {
  128. current = $('.result:first');
  129. if (current.length === 0) {
  130. return;
  131. }
  132. }
  133. var next;
  134. if (typeof which !== 'string') {
  135. next = which;
  136. } else {
  137. switch (which) {
  138. case 'visible':
  139. var top = $(window).scrollTop();
  140. var bot = top + $(window).height();
  141. var results = $('.result');
  142. for (var i = 0; i < results.length; i++) {
  143. next = $(results[i]);
  144. var etop = next.offset().top;
  145. var ebot = etop + next.height();
  146. if ((ebot <= bot) && (etop > top)) {
  147. break;
  148. }
  149. }
  150. break;
  151. case 'down':
  152. next = current.next('.result');
  153. if (next.length === 0) {
  154. next = $('.result:first');
  155. }
  156. break;
  157. case 'up':
  158. next = current.prev('.result');
  159. if (next.length === 0) {
  160. next = $('.result:last');
  161. }
  162. break;
  163. case 'bottom':
  164. next = $('.result:last');
  165. break;
  166. case 'top':
  167. default:
  168. next = $('.result:first');
  169. }
  170. }
  171. if (next) {
  172. current.removeAttr('data-vim-selected').removeClass('well well-sm');
  173. next.attr('data-vim-selected', 'true').addClass('well well-sm');
  174. scrollPageToSelected();
  175. }
  176. }
  177. }
  178. function reloadPage() {
  179. document.location.reload(false);
  180. }
  181. function removeFocus() {
  182. if (document.activeElement) {
  183. document.activeElement.blur();
  184. }
  185. }
  186. function pageButtonClick(num) {
  187. return function() {
  188. var buttons = $('div#pagination button[type="submit"]');
  189. if (buttons.length !== 2) {
  190. console.log('page navigation with this theme is not supported');
  191. return;
  192. }
  193. if (num >= 0 && num < buttons.length) {
  194. buttons[num].click();
  195. } else {
  196. console.log('pageButtonClick(): invalid argument');
  197. }
  198. }
  199. }
  200. function scrollPageToSelected() {
  201. var sel = $('.result[data-vim-selected]');
  202. if (sel.length !== 1) {
  203. return;
  204. }
  205. var wnd = $(window);
  206. var wtop = wnd.scrollTop();
  207. var etop = sel.offset().top;
  208. var offset = 30;
  209. if (wtop > etop) {
  210. wnd.scrollTop(etop - offset);
  211. } else {
  212. var ebot = etop + sel.height();
  213. var wbot = wtop + wnd.height();
  214. if (wbot < ebot) {
  215. wnd.scrollTop(ebot - wnd.height() + offset);
  216. }
  217. }
  218. }
  219. function scrollPage(amount) {
  220. return function() {
  221. window.scrollBy(0, amount);
  222. highlightResult('visible')();
  223. }
  224. }
  225. function scrollPageTo(position, nav) {
  226. return function() {
  227. window.scrollTo(0, position);
  228. highlightResult(nav)();
  229. }
  230. }
  231. function searchInputFocus() {
  232. $('input#q').focus();
  233. }
  234. function openResult(newTab) {
  235. return function() {
  236. var link = $('.result[data-vim-selected] .result_header a');
  237. if (link.length) {
  238. var url = link.attr('href');
  239. if (newTab) {
  240. window.open(url);
  241. } else {
  242. window.location.href = url;
  243. }
  244. }
  245. };
  246. }
  247. function toggleHelp() {
  248. var helpPanel = $('#vim-hotkeys-help');
  249. if (helpPanel.length) {
  250. helpPanel.toggleClass('hidden');
  251. return;
  252. }
  253. var categories = {};
  254. for (var k in vimKeys) {
  255. var key = vimKeys[k];
  256. categories[key.cat] = categories[key.cat] || [];
  257. categories[key.cat].push(key);
  258. }
  259. var sorted = Object.keys(categories).sort(function(a, b) {
  260. return categories[b].length - categories[a].length;
  261. });
  262. if (sorted.length === 0) {
  263. return;
  264. }
  265. var html = '<div id="vim-hotkeys-help" class="well vim-hotkeys-help">';
  266. html += '<div class="container-fluid">';
  267. html += '<div class="row">';
  268. html += '<div class="col-sm-12">';
  269. html += '<h3>How to navigate searx with Vim-like hotkeys</h3>';
  270. html += '</div>'; // col-sm-12
  271. html += '</div>'; // row
  272. for (var i = 0; i < sorted.length; i++) {
  273. var cat = categories[sorted[i]];
  274. var lastCategory = i === (sorted.length - 1);
  275. var first = i % 2 === 0;
  276. if (first) {
  277. html += '<div class="row dflex">';
  278. }
  279. html += '<div class="col-sm-' + (first && lastCategory ? 12 : 6) + ' dflex">';
  280. html += '<div class="panel panel-default iflex">';
  281. html += '<div class="panel-heading">' + cat[0].cat + '</div>';
  282. html += '<div class="panel-body">';
  283. html += '<ul class="list-unstyled">';
  284. for (var cj in cat) {
  285. html += '<li><kbd>' + cat[cj].key + '</kbd> ' + cat[cj].des + '</li>';
  286. }
  287. html += '</ul>';
  288. html += '</div>'; // panel-body
  289. html += '</div>'; // panel
  290. html += '</div>'; // col-sm-*
  291. if (!first || lastCategory) {
  292. html += '</div>'; // row
  293. }
  294. }
  295. html += '</div>'; // container-fluid
  296. html += '</div>'; // vim-hotkeys-help
  297. $('body').append(html);
  298. }
  299. });