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 :P )

Share :
  • Reddit
  • del.icio.us
  • Digg
  • StumbleUpon
  • DZone
  • Ping.fm
  • Sphinn
Related posts :

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

Pages: [1] 2 » Show All

  1. 1
    Premasagar says:

    Brilliant. Just what I was looking for.
    Yes – I was also pretty surprised that this wasn’t a common plugin.

    You might want to add it to the Mycroft search engine plugin directory.

  2. 2
    White Shadow says:

    Thanks :)
    I don’t think it would be accepted in the directory though, because this is a different kind of search plugin.

Pages: [1] 2 » Show All

Leave a Reply