Greasy Fork 还支持 简体中文。

Google digits

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

目前為 2021-05-17 提交的版本,檢視 最新版本

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