Skip to content

Instantly share code, notes, and snippets.

@Soft
Created May 15, 2010 23:33
Show Gist options
  • Save Soft/402510 to your computer and use it in GitHub Desktop.
Save Soft/402510 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Google shortcuts
// @include http://www.google.*/search*
// @include https://encrypted.google.com/search*
// @description Adds useful shortcuts to Google search results page
// ==/UserScript==
(function(){
var results = document.querySelectorAll("#ires>ol>li.g.w0");
var activated = false;
var indexStyles = "background: #1111cc;" +
"font: 10px verdana, arial, sans;" +
"width: 12px; height: 12px;" +
"display: block; text-align: center;" +
"color: white; float: left;" +
"margin-right: 5px;" +
"-webkit-border-radius: 6px;";
activateShortcuts = function() {
if (!activated) {
activated = true;
for (var i = 0; i < results.length && i < 9; i++) {
var index = document.createElement("span");
index.setAttribute("style", indexStyles);
index.setAttribute("class", "index");
index.innerHTML = i + 1;
var header = results[i].firstChild;
header.insertBefore(index, header.firstChild);
}
}
};
disableShortcuts = function() {
if (activated) {
activated = false;
var elems = document.querySelectorAll("span.index");
for (var i = 0; i < elems.length; i++) {
elems[i].parentNode.removeChild(elems[i]);
}
}
};
openLink = function(index) {
window.location = results[index].querySelector("h3.r>a.l").href;
};
document.addEventListener("keydown", function(event) {
if (document.activeElement.value) return;
if (event.keyCode == 79) { // o
if (!activated) {
activateShortcuts();
} else {
disableShortcuts();
}
} else if (event.keyCode >= 49 && event.keyCode <= 57 &&
(event.keyCode - 49) < results.length) { // 1-9
openLink(event.keyCode - 49);
} else {
disableShortcuts();
}
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment