Search The Current Site – A Firefox Extension

There are lots of search engine plugins for Firefox, and it’s easy to make a simple search plugin for your own site. That’s why it came as a bit of a surprise that I couldn’t find any search engine plugins for searching the currently open site. So I made one.

Let me clarify – I don’t mean searching my site. I’m talking about searching whatever website is displayed in the currently active Firefox tab. For example, let’s say you browse to example.com and discover their on-site search features suck/are nonexistent. With this extension you can just select “Current site” from the search bar, type in your query and hit Enter to search the entire site for the keywords you entered.

The Firefox extension that I have created will add a new search engine to the Search Bar. This search engine is called “Current site” and it will search the entire website that you’re on for whatever you enter in the search box. It uses Google to perform the actual search.

The exact behaviour of this extension depends on what you’ve got open in the current tab :

  • A normal webpage – search the current site via Google.
  • Google – search the last site that you searched with this extension.
  • Blank tab – do a normal Google search (not constrained to a site).

This extension is experimental, so use at your own risk!

Technical Notes

This extension doesn’t use the site: command. Instead it appends the sitesearch argument to the Google search URL. The effect is the same, but sitesearch doesn’t show up in the input box on Google’s page, and any further queries you make from that page are still constrained to the website’s domain.

Creating the extension was pretty hard. For one, it really had to be an extension, not a standard search plugin, because the search engine plugin format doesn’t have a way to get information about the currently open site. I needed to write some JavaScript to get the current URL, extract the domain name and append it to the query string. That was easy enough. The real trouble began when I started trying to figure out how to attach that code to a search engine object.

Turns out there is a nsIBrowserSearchService service that manages these things. You can programmatically add a new search engine and then retrieve it as a nsISearchEngine. This interface has a method – getSubmission() – that Firefox calls when it needs to determine the URL to open when something is searched. The method returns a nsISearchSubmission object. I had to override the aforementioned method to insert an additional argument in the URL.

Oh, did I mention that none of this is really documented anywhere? Sure, there are some terse interface docs on a third party site (see below), that can be useful if you know what to look for. I didn’t, so I had to analyze the source code. Arrgh.

Some links that should get you started (or confuse you completely 😛 )

Related posts :

44 Responses to “Search The Current Site – A Firefox Extension”

  1. IMGrant says:

    Is this still being developed? It seems like the ideal implementation, but I currently have problems with it (FF3.6) – seems like it works at first, but after I open more than one window it stops working. The error console shows ‘too much recursion’ errors.

  2. White Shadow says:

    This is not being actively developed, no. Maybe if I’ll look into it if get a free moment sometime.

  3. windozer says:

    Just want to confirm this works perfectly with Firefox 4.0 b3pre; I just downloaded it, dragged & dropped into FF window to install, and tested it. BAM!!

    I have been using this plugin for so many years now (Since FF 1 or 2) : ) I remember searching on google relentlessly to finally discover this. Thanks White Shadow.

  4. […] This Site” Bookmarklet For FireFox & Opera Some time ago I made a Firefox extension that would let you easily search any site you had open. The addon, while not hugely popular, has […]

Leave a Reply