Google digits

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

当前为 2023-05-22 提交的版本,查看 最新版本

  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.2.4
  6. // @author wOxxOm
  7. // @namespace wOxxOm.scripts
  8. // @license MIT License
  9. // @run-at document-start
  10. // @grant none
  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 SEL10 = '#rso > :last-child a h3';
  17. const SELHIDE = '#rso [aria-hidden="true"] a h3';
  18.  
  19. document.documentElement.appendChild(document.createElement('style')).textContent = /*css*/ `
  20. body {
  21. counter-reset: digitnav;
  22. }
  23. ${SEL}::before {
  24. counter-increment: digitnav;
  25. content: counter(digitnav) ":";
  26. position: absolute;
  27. margin-left: -1em;
  28. opacity: .5;
  29. z-index: 127;
  30. }
  31. ${SEL10}::before {
  32. content: "0:";
  33. }
  34. ${SEL}:hover::before {
  35. opacity: 1;
  36. }
  37. ${SELHIDE}::before {
  38. counter-increment: none !important;
  39. }
  40. `;
  41.  
  42. addEventListener('keydown', function onKeyDown(e) {
  43. const k = e.which;
  44. const digit =
  45. k >= 48 && k <= 57 ? k - 48 :
  46. k >= 96 && k <= 105 ? k - 96 :
  47. -1;
  48. let el;
  49. if (digit >= 0 &&
  50. location.href.match(/[#&?]q=/) &&
  51. !e.metaKey && !e.ctrlKey &&
  52. (el = e.target.localName) !== 'input' && (el !== 'textarea')) {
  53. const elems = [...document.querySelectorAll(SEL)].filter(el => !el.matches(SELHIDE));
  54. el = elems[digit ? digit - 1 : 9];
  55. const link = el && el.closest('a');
  56. if (!link) return;
  57. removeEventListener('keydown', onKeyDown, true);
  58. e.stopPropagation();
  59. el.style.backgroundColor = 'yellow';
  60. link.focus();
  61. link.dispatchEvent(new MouseEvent('click',
  62. e.shiftKey && {shiftKey: true} ||
  63. e.altKey && {ctrlKey: true} ||
  64. {}));
  65. }
  66. }, true);