Google digits

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

目前为 2020-03-12 提交的版本,查看 最新版本

  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.1.4
  6. // @author wOxxOm
  7. // @namespace wOxxOm.scripts
  8. // @license MIT License
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (document.head || document.documentElement).appendChild(document.createElement('style')).textContent = `
  14. body {
  15. counter-reset: digitnav;
  16. }
  17. #rso > div > :not([data-ved]) a h3::before {
  18. counter-increment: digitnav;
  19. content: counter(digitnav);
  20. position: absolute;
  21. margin-left: -1em;
  22. opacity: .5;
  23. }
  24. `;
  25.  
  26. window.addEventListener('keydown', function(e) {
  27. var digit = e.keyCode - 48;
  28. if (digit >= 1 && digit <= 9 &&
  29. location.href.match(/[#&?]q=/) &&
  30. !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey &&
  31. e.target.localName != 'input') {
  32. var el = document.querySelectorAll('a h3')[digit - 1];
  33. var link = el && el.closest('a');
  34. if (link) {
  35. el.style.backgroundColor = 'yellow';
  36. location.href = link.href;
  37. e.preventDefault();
  38. e.stopPropagation();
  39. e.stopImmediatePropagation();
  40. }
  41. }
  42. }, true);