Textbox Focus with Greasemonkey and Javascript

I use IMDB at work a lot, and finally got tired of having to manually click the search box every time I went back to that tab to perform a query. So with a little searching and tweaking, I came up with a Greasemonkey script that not only gives focus to the main search box, but returns focus to it when I hit the ESC key.  Here’s the code for those interested:

function focusFirst() {
	document.getElementsByTagName('input')[0].focus();
}
function focusKey(ev) {
    if (ev.keyCode == 27)
    // 27 is the keycode for esc
    focusFirst();
}
document.addEventListener("keyup", focusKey, false);
window.addEventListener('load', focusFirst, false);

I’m sure it can be done differently, and more efficiently, so constructive criticism is welcome.

Advertisements

2 thoughts on “Textbox Focus with Greasemonkey and Javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s