I was wondering if anybody knows a way to create keyboard shortcuts for google search results. For instance, I'm looking for a way open up the first google search result with the number 1, on my keyboard, and for each successive entry to be opened with the next number.
I've searched high and low for ways to do this, but I haven't gotten much closer. What are ways/extensions/languages I can use to carry out this feature?
At the very least, I was wondering if anybody can point me in the direction of resources or ways to program this. I have the Tampermonkey extension downloaded as a Chrome extension, but I haven't been able to create or find the appropriate JS code to do what I want. An example of what I'm looking for can be found here: http://googlesystem.blogspot.com/2007/02/keyboard-shortcuts-for-google-search.html. Unfortunately, the scripts and links found there are dead and incredibly old (from 2007).
This idea got me interested, so here's a basic implementation for Tampermonkey that works on all Google domains via the special .tld
domain available to userscripts.
// ==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
}
The challenge was to process events before the page does, so I've used a capturing listener on the top of the bubble chain, the window
object, and used @run-at: document-start
metakey to register the handler before the page registered its own.