2017-08-08 24 views
-4

あなたが思いついた可能性のあるjsコードを共有してください。再利用できるようにしてください。ありがとうgreasemonkey/tampermonkeyを使用したGoogle検索結果ページのキーボードショートカット?

+3

質問がありますか、問題が発生していますか、コードを共有しようとしていますか?前者の場合、あなたの遭遇している問題/エラーの詳細と、それを修正しようとしたことをあなたの投稿に更新してください。後者の場合、Stack Overflowは適切な場所ではなく、代わりにGitHubなどを調べます。 – TheJim01

答えて

0

以下は、いくつかのGoogle検索結果ページのショートカットの動作コードです。画像を開きます、 シフトはV +:ビデオ、 ... ... を...開きます

あなたが表示されます

シフト+私はそれとLMKのおかげで即興で気軽にしてください検索結果のハイパーリンク(リンク)の隣のリンク番号 キーボードの番号を押すと、そのリンクが新しいタブで開きます。

このjsコードを実行するにはgreasemonkeyまたはtampermonkeyが必要です。

私は開発者の一人は、このことができます願っていWay to Create Keyboard Shortcuts for Google Search Results?

に書いたものを活用することで、それを少し改善しました。

おかげで、

Abhi。

// ==UserScript== 
// @name   Google digits 
// @include  https://www.google.tld/* 
// @run-at  document-start 
// @author  Abhinay Gadikoppula 
// @match  https://stackoverflow.com/questions/39456608/way-to-create-keyboard-shortcuts-for-google-search-results 
// @grant  none 
// ==/UserScript== 
// only work on search pages with #q= &q= ?q= 
if (location.href.match(/[#&?]q=/)) { 

    var links = []; 
    var menu_links = []; 
    var menu_links_map = []; 

    window.onload = function() { 

     // get all header menu links 
     menu_links = document.getElementById("hdtb-msb").querySelectorAll('a.q.qs'); 
     menu_links.forEach(function(link, index) { 
      menu_links_map[link.innerHTML.charAt(0)] = link; 
     }); 

     // get all results in an array 
     links = document.querySelectorAll('h3.r a'); 
     links.forEach(function(link, index) { 
      link.innerHTML = link.innerHTML + ' (' + (index + 1) + ')'; 
     }); 
    }; 

    window.addEventListener('keydown', function(e) { 

     if (e.shiftKey && e.keyCode >= 65 && e.keyCode <= 90) { 
      e.preventDefault(); 
      char = String.fromCharCode(e.keyCode); 
      //keycodes 65-90 are alphabets a-z 
      if (char in menu_links_map) { 
       menu_links_map[char].click(); 
      } 
     } else if (e.keyCode >= 49 && e.keyCode <= 57 && 
      // don't intercept if a modifier key is held 
      !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey && 
      // don't intercept 1-9 in the search input 
      e.target.localName != 'input') { 
      //keycodes for 1-9 are 49-57 but replacing them with 1-9 
      var digit = e.keyCode - 48; 
      // arrays are 0-based 
      var link = links[digit - 1]; 
      if (link) { 
       // go to the linked URL 
       window.open(link.href, '_blank'); 
       // prevent site from seeing this keyboard event 
       e.preventDefault(); 
       e.stopPropagation(); 
       e.stopImmediatePropagation(); 
      } 
     } else if (e.keyCode == 191) { 
      // event '/' keycode 191 
      e.preventDefault(); 
      input = document.getElementById("lst-ib"); 
      val = input.value; //store the value of the element 
      input.value = ''; //clear the value of the element 
      input.focus(); //set focus on the input field 
      input.value = val; //set that value back. 
     } 
    }, true); // true means we capture the event before it's "bubbled" down 
} 
関連する問題