Skip to content

Instantly share code, notes, and snippets.

@thefloodshark
Created April 2, 2018 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefloodshark/01947fa884d6b54820424febafe9f48d to your computer and use it in GitHub Desktop.
Save thefloodshark/01947fa884d6b54820424febafe9f48d to your computer and use it in GitHub Desktop.
Tampermonkey Userscript - Keybind Numbered Google Results Select
// ==UserScript==
// @name Google digits
// @include https://www.google.tld/*
// @run-at document-start
// ==/UserScript==
// only work on search pages with #q= &q= ?q=
if (location.href.match(/[#&?]q=/)) {
window.addEventListener('keydown', function(e) {
var digit = e.keyCode - 48;
// 48 is the code for '0'
if (digit >= 1 && digit <= 9 &&
// 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')
{
// get all results in an array
var links = document.querySelectorAll('h3.r a');
// arrays are 0-based
var link = links[digit - 1];
if (link) {
// go to the linked URL
location.href = link.href;
// prevent site from seeing this keyboard event
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
}
}
}, true); // true means we capture the event before it's "bubbled" down
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment