NoMouseGoogle

Shortcut for Google search results. j/k to move focus, enter/l/h to open in current/new/background tab, n/p to go to next/previous page.

当前为 2023-09-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name NoMouseGoogle
  3. // @namespace com.gmail.fujifruity.greasemonkey
  4. // @version 1.16
  5. // @description Shortcut for Google search results. j/k to move focus, enter/l/h to open in current/new/background tab, n/p to go to next/previous page.
  6. // @author fujifruity
  7. // @include https://www.google.com/search*
  8. // @include https://www.google.co.*/search*
  9. // @grant GM.openInTab
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. {
  14. const tag = "noMouseGoogleCurrentItem"
  15. const itemQuery = "#res div[data-hveid][data-ved][lang], #botstuff div[data-hveid][data-ved][lang], #rso video-voyager>div"
  16. const findItems = () => [...document.querySelectorAll(itemQuery)]
  17. .filter(e => e.offsetParent != null /* is visible */)
  18. const findCurrentItem = items => items.find(e => e.hasAttribute(tag))
  19. const moveCursor = step => {
  20. const items = findItems()
  21. const currentItem = findCurrentItem(items)
  22. const r = currentItem.getBoundingClientRect();
  23. const inScreen = (0 < r.top && r.top < window.innerHeight
  24. || 0 < r.bottom && r.bottom < window.innerHeight)
  25. if (!inScreen) {
  26. const dist = e => {
  27. const r = e.getBoundingClientRect()
  28. return Math.abs(window.innerHeight - (r.top + r.bottom))
  29. }
  30. const nearestItem = items.reduce((acc, e) => dist(acc) < dist(e) ? acc : e)
  31. select(nearestItem, currentItem)
  32. return
  33. }
  34. const nextIdx = (items.indexOf(currentItem) + step + items.length) % items.length
  35. select(items[nextIdx], currentItem)
  36. }
  37. const highlight = e => {
  38. const isDarkTheme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
  39. e.style.backgroundColor = isDarkTheme ? '#2a2a2a' : 'WhiteSmoke'
  40. }
  41. const select = (item, currentItem) => {
  42. // Deselect current item.
  43. if (currentItem) {
  44. currentItem.style.backgroundColor = null
  45. currentItem.removeAttribute(tag)
  46. }
  47. // Select the item.
  48. item.setAttribute(tag, '')
  49. highlight(item)
  50. item.scrollIntoView({ behavior: "smooth", block: "center" })
  51. console.log('select', item)
  52. }
  53.  
  54. const currentItemHref = () => findCurrentItem(findItems()).querySelector('a').href
  55. const openInNewTab = inBackground => GM.openInTab(currentItemHref(), inBackground)
  56. const openInThisTab = () => window.open(currentItemHref(), "_self")
  57.  
  58. // Select the first item without scrolling.
  59. const items = findItems()
  60. items[0].setAttribute(tag, '')
  61. highlight(items[0])
  62.  
  63. window.addEventListener('keydown', event => {
  64. if (["INPUT", "TEXTAREA"].includes(event.target.tagName) || event.ctrlKey || event.altKey || event.metaKey) return
  65. if (event.key == 'j') moveCursor(+1)
  66. if (event.key == 'k') moveCursor(-1)
  67. if (event.key == 'l') openInNewTab(false)
  68. if (event.key == 'h') openInNewTab(true)
  69. if (event.key == 'Enter') openInThisTab()
  70. if (event.key == 'n') document.querySelector('#pnnext')?.click()
  71. if (event.key == 'p') document.querySelector('#pnprev')?.click()
  72. })
  73.  
  74. }