A Handy Firefox Hack (Or Several?)
July 9, 2010
When I’m browsing a website, sometimes I find myself needing to search for a specific topic on the site. Some sites have their own search boxes, but, in my experience, such homegrown site searches tend to be disappointing. (They can, however, often be improved using a Google Site Search.)
Historically, I’ve typically resorted to opening a new tab and typing “g site:www.example.com query”. The ‘g’ triggers my Firefox custom keyword search for Google, and the ‘site’ operator confines a search to specific domain. Clearly, with a long domain, this can result in a lot of typing, so it’s always something I do reluctantly.1
My first solution was just to beef up my custom keyword arsenal a bit, and map ‘gs’ to a Google search preloaded with the ‘site’ operator. This was better, but, again, most of my searches are targeted to the site I’m currently browsing. Typing the domain again, especially when it was already in the address bar, was a far cry from ideal. Unfortunately, the custom keyword syntax is pretty basic, and I couldn’t seem to find a way to grab that domain information.
Luckily, I hit upon the idea of using a JavaScript pseudo-URL. This would let me map a Firefox custom keyword search to a JavaScript snippet, and thereby pretty much do anything I wanted!
The snippet I used is:
javascript: var host = document.location.href; if (host.indexOf("http://") == 0) {host = host.substr(7);} else if (host.indexOf("https://") == 0) {host = host.substr(8);}; if (host.indexOf("/") != 0) {host = host.substr(0, host.indexOf("/"));}; document.location = "https://www.google.com/search?hl=en&q=site:"+host+" "+"%s";
I just mapped it to ‘gsl’ for Google Site Local. It is wonderful.
- I have the typical developer’s hatred of unnecessary typing, to the point where much of my computer use can be characterized as an endless tug-of-war, where only the strongest urges actually make it out of my finger tips. Mousing — or worse, the dreaded transition from keyboard to mouse — I can’t even talk about. ↩
