Created
April 4, 2011 06:57
-
-
Save jackysee/901226 to your computer and use it in GitHub Desktop.
googlesearchnavigation.user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @id GoogleJK | |
// @name GoogleJK | |
// @namespace http://jacky.seezone.net/googlejk | |
// @description use j,k to jump to different google search result focus | |
// @include http*://www.google.com*/search?* | |
// ==/UserScript== | |
GM_addStyle("li.g.active{border-left:3px solid #AAA; padding-left: 5px;}"); | |
function findSibling(dir /*previous,next*/, li){ | |
var target = li[dir+'Sibling']; | |
if(target.tagName && target.tagName.toLowerCase() == 'li'){ | |
return target; | |
} | |
return null; | |
} | |
function jump(dir){ | |
var li = document.querySelector("li.g.active"); | |
if(li) li.className = li.className.replace(/ active /, ''); | |
var target = !li | |
? document.querySelector("li.g") | |
: findSibling(dir, li) || li; | |
target.className = target.className + ' active '; | |
var a = target.getElementsByTagName('a')[0]; | |
if(a) a.focus(); | |
} | |
function navKey(ev){ | |
if(ev.altKey || ev.ctrlKey || ev.shiftKey || /input|textarea/.test(ev.target.tagName.toLowerCase())) return; | |
var key = String.fromCharCode(ev.keyCode ||ev.which); | |
if(key == 'j' || key == 'k'){ | |
jump(key == 'j'?'next':'previous'); | |
} | |
} | |
document.addEventListener('keypress', navKey, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment