Google Search hotkeys

Quick-select from google search using numbers 1-9. Does not work on sub-results. Adds numbers for reference. Also does NOT yet work on complex results such as yt vids/twitter posts.

当前为 2021-12-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google Search hotkeys
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Quick-select from google search using numbers 1-9. Does not work on sub-results. Adds numbers for reference. Also does NOT yet work on complex results such as yt vids/twitter posts.
  6. // @author You
  7. // @include /(https?:(www\.)?\/\/)?google\.com\/search\?.*
  8. // @run-at document-start
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. let cycleno=0;
  18. let cycle=setInterval(update,100)
  19. let search_dict = {}
  20. let queue=[]
  21. setup_listeners()
  22.  
  23. function update(){
  24. //var startTime = performance.now()
  25. if(cycleno==10){
  26. clearInterval(cycle)
  27. (()=>{
  28. if(queue.length>0){
  29. search_dict[queue[0]].click()
  30. }
  31. })()
  32. }
  33. cycleno+=1
  34. let linkno=1;
  35. let search_results = document.querySelectorAll('.g')
  36. let gtags=[]
  37. for(let i=0; i<search_results.length; ++i){
  38. if(search_results[i].className=='g'){
  39. gtags.push(search_results[i])
  40. }
  41. }
  42. let htags=[]
  43. let atags=[]
  44. for(let i=0; i<gtags.length; ++i){
  45. let htag=gtags[i].getElementsByTagName('h3')
  46. if(htag.length>0){
  47. htags.push(htag[0])
  48. atags.push(htag[0].parentElement)
  49. }
  50. }
  51.  
  52. let headers=[]
  53. for(let i=0; i<atags.length; ++i){
  54. if(atags[i].parentNode.children.length>1){
  55. let svgtag=atags[i].parentNode.getElementsByTagName('svg')
  56. if(svgtag.length>0){
  57. headers.push(atags[i])
  58. }
  59. }
  60. }
  61. let hrefs=[]
  62. let headers2=[]
  63. for(const header of headers){
  64. if(hrefs.includes(header.getAttribute('href'))){
  65.  
  66. }
  67. else{
  68. hrefs.push(header.getAttribute('href'))
  69. headers2.push(header)
  70. }
  71. }
  72. for(const tag of headers2){
  73. let cites=tag.getElementsByTagName('cite');
  74. if(cites.length==1){
  75. if(!cites[0].textContent.startsWith(linkno)){
  76. cites[0].textContent=linkno+' '+cites[0].textContent
  77. }
  78. linkno++
  79. }
  80. }
  81.  
  82. headers2.forEach((key,i)=>{search_dict[i+1]=key})
  83. //var endTime = performance.now()
  84. //console.log(`Call to doSomething took ${endTime - startTime} milliseconds`)
  85. }
  86.  
  87. function setup_listeners(){
  88. for(let i=1; i<10; ++i){
  89. if(document.querySelector('body')){
  90. console.log('body found')
  91. document.body.addEventListener('keydown',(e)=>{
  92. if(e.key==i && document.activeElement.tagName!='INPUT'){
  93. let index=parseInt(i)
  94. if(search_dict.hasOwnProperty(index)){
  95. search_dict[index].click()
  96.  
  97. }
  98. else {
  99. queue.push(index)
  100. }
  101. }
  102. })
  103. }
  104. else {
  105. console.log('body not found')
  106. setTimeout(setup_listeners,100)
  107. }
  108. }
  109. }
  110. // Your code here...
  111. })();