Greasy Fork is available in English.

Google digits

Press 1-9 on Google search page to open the corresponding link

  1. // ==UserScript==
  2. // @name Google digits
  3. // @description Press 1-9 on Google search page to open the corresponding link
  4. // @include https://www.google.tld/*
  5. // @version 1.3.0
  6. // @author wOxxOm
  7. // @namespace wOxxOm.scripts
  8. // @license MIT License
  9. // @run-at document-start
  10. // @grant GM_openInTab
  11. // @icon https://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png
  12. // @noframes
  13. // ==/UserScript==
  14.  
  15. const SEL = '#rso a h3';
  16. const SEL2 = '.related-question-pair [aria-expanded][tabindex]';
  17. const SELHIDE = '.related-question-pair a h3';
  18.  
  19. document.documentElement.appendChild(document.createElement('style')).textContent = /*css*/ `
  20. #rso {
  21. counter-reset: digitnav digitnav2;
  22. }
  23. ${SEL}::before {
  24. counter-increment: digitnav;
  25. content: counter(digitnav) ":";
  26. margin-right: .25ex;
  27. opacity: .5;
  28. }
  29. ${SEL2}::before {
  30. counter-increment: digitnav2;
  31. content: "⇧" counter(digitnav2) ":";
  32. margin-right: .5ex;
  33. opacity: .75;
  34. }
  35. ${SEL}:hover::before {
  36. opacity: 1;
  37. }
  38. ${SELHIDE}::before {
  39. counter-increment: none !important;
  40. }
  41. `;
  42.  
  43. addEventListener('keydown', function onKeyDown(e) {
  44. const k = e.which;
  45. const digit =
  46. k >= 48 && k <= 57 ? k - 48 :
  47. k >= 96 && k <= 105 ? k - 96 :
  48. -1;
  49. let el, a, b;
  50. if (digit >= 0 &&
  51. location.href.match(/[#&?]q=/) &&
  52. !e.metaKey && !e.ctrlKey &&
  53. (el = e.target.localName) !== 'input' && (el !== 'textarea')) {
  54. const is2 = e.shiftKey;
  55. const elems = [...document.querySelectorAll(is2 ? SEL2 : SEL)]
  56. .filter(is2 ? Boolean : el => !el.matches(SELHIDE));
  57. el = elems[digit ? digit - 1 : 9];
  58. a = el && (el.closest('a') || is2 && el);
  59. if (!a) return;
  60. e.stopPropagation();
  61. el.style.backgroundColor = el.style.backgroundColor ? '' : 'yellow';
  62. a.focus();
  63. if (a.scrollIntoViewIfNeeded) a.scrollIntoViewIfNeeded();
  64. else if ((b = a.getBoundingClientRect()) && (b.y < 0 || b.bottom > innerHeight))
  65. a.scrollIntoView({block: 'center'});
  66. if (is2) a.click();
  67. else if (e.altKey) GM_openInTab(a.href);
  68. else location = a.href;
  69. }
  70. }, true);