Ok, so this is the only bookmarklet I recall making that early in the morning, but it’s still useful if you have a browser setup at all similar to mine. When triggered, this bookmarklet finds all input elements on the page, sorts out the text/search types, runs them through a few regular expressions to find the first one that seems a likely candidate to be the main search field and then focuses it. It’s not perfect, but it works pretty well across the board (and without loading any libraries or external dependencies).

SearchFocus

If you want to test it, drag the “SearchFocus” button above to your bookmark bar. If you have any trouble with that (for example, if Chrome blocks the script and it shows up as javascript:void()), just create a new bookmark named “SearchFocus” in your bookmarks bar and paste the code below (raw version) in as the url. Then, go to any web page with a search field on it and click the bookmarklet. If all goes well it should find the search field and focus it, ready for you to type your search.

searchFocus.bookmarklet.jsraw
javascript:var%20inputs=document.getElementsByTagName('input'),firstSearch=false,textinputs=[],i,t;for(i=0;i<inputs.length;i++)if(((inputs[i].type==='text')||(inputs[i].type==='search'))&&inputs.disabled!==true)textinputs.push(inputs[i]);for(t=0;t<textinputs.length;t++)if((/search/i).test(textinputs[t].className)||(/(^[sq]$|search|query)/i).test(textinputs[t].id)||(/^(q(uery)?|s|.*?search.*)$/).test(textinputs[t].name)){firstSearch=textinputs[t];break;}if(!firstSearch)textinputs[0].focus();else%20firstSearch.focus();

Triggering the bookmarklet

For this to be truly useful, you need a way to fire the bookmarklet without clicking it. Methods for doing this vary from browser to browser. Note that with keyboard solutions some sites have issues due to keyboard trapping. Twitter, for example, intercepts certain keys for its own navigation.

In Safari

In Safari you can do this just by putting it in your bookmarks bar in one of the first nine positions, then using #, where # is the position of the bookmark (1-9). You can also fire it via Quix, and I’ve added it to my custom Quix file with the shortcut “sf”. Other options include Gleebox or custom injected scripts/extensions.

In Chrome

For Chrome, you’ll need a keyboard Extension. I currently recommend Keyboard-fu, but I just recently started using Chrome more heavily than Safari1 and am still exploring the extensions (Josh Dick recommends Shortcut Manager2). I have it set up in Chrome as a bookmark called “SearchFocus”, and fire it via Keyboard-fu using s (which happens to not conflict with Vimium). The Keyboard-fu command for firing the bookmark (you’d have to modify the path based on your bookmark location):

fu.openBookmarkByPath('/Bookmarks Bar/Bookmarklets/SearchFocus', target='here')

In Firefox, IE, Opera and other browsers I don’t like

For Firefox, well, you’ll have to tell me. I haven’t used Firefox for anything other than browser testing for years now, so you’re on your own with that ugly beast3. I’m not opening Parallels right now to test in IE, either, so if you try it, let me know if it works there or not. Opera can go Seriously, why would you do that? REDACTED.

What’s that? Camino? I have no time for your sick sense of humor.

Developers

There’s an un-boomarkified version of the code in the Gist, if you’re curious or want to expand the filtering criteria a little. Let me know if you improve on it.

  1. I know some people are going to ask me why I ditched Safari. It’s primarily because I installed a development version and was unhappy with it, then ran into enough frustration with downgrading that I just gave up for now. When it stables out, I might go back. I’m getting really used to Chrome, though…

  2. With Shortcut Manager you can just paste the code into the bottom box of a new shortcut.

  3. If you know a great way to do it, I will edit this post with your suggestion. Firefox will still be ugly, though.