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. 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. 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.

  3. ithinc says:

    Great! It would be more appreciated if it could be a GreaseMonkey script.

  4. ithinc says:

    I’m looking for another search plugin to open the “selected text” directly as an url. It seems a search plugin could not satisfy either.

  5. Transcontinental says:

    This is a very nice extension. I’ve been looking for this for some time now, because the only point I missed was the site search, and I didn’t want to install Google Toolbar for that sole purpose! Thanks a lot, works great!

  6. White Shadow says:

    I don’t know about Firefox, but in Opera you could select some text and press Ctrl-C followed by Ctrl-D to do that…

  7. Transcontinental says:

    @White Shadow, one might be searching a site for occurrences rather than for redundancies ; I mean, finding the text first to search where else it appears in the site is a half solution!

  8. ithinc says:

    A bug is found. Once you open a second window, the “Search Current Site” will encounter an error.

  9. Ty says:

    I uninstalled and reinstalled the extension and for some reason the icon does not show anymore in the extension manager but does in the search bar. It was there the first time. any ideas?

  10. bbbobbbo says:

    MAN, you made my day!!! Yoooohoooo, cannot believe it’s true! An absolutely indispensable extension. It is one whole year that i’ve been looking for a search engine like this, and found none (so far, that is :)))

    As someone suggested above you should definitely consider adding this one to http://mycroft.mozdev.org/ – that was the first place i looked and was disappointed that i found nothing, so maybe this will prevent other people from wasting their time elsewhere.

  11. White Shadow says:

    Thank you πŸ™‚
    As I’ve mentioned previously, this plugin has a different format than other search plugins, so it probably wouldn’t be accepted in that directory.

  12. ithinc says:

    Inspired by your article, I have completed a CustomButtons button (link). It is a button, besides, it adds a menuitem in the contextarea context menu, and it supports drag&drop search. Overriding of getSubmission will lead to some problem when more than one window are opened.

  13. White Shadow says:

    Interesting. By the way, I fixed the link in your first comment.

  14. vaitus says:

    Thanks! I’ve been looking for this ever since I’ve made the switch to firefox 3. Installing the google toolbar or the web search pro plugin just for that would have been too much.

  15. Transcontinental says:

    As White Shadow, the author, mentions, the very specificity of this extension requiring the assistance of an executable to be installed makes it impossible to have this extension appear as other traditional Firefox extensions. It wouldn’t be much of a problem if it weren’t the consequence of bringing ‘Search The Current Side’ to few, making those few, myself anyway, feel as a privileged person!

    Well, we searched, and we found!

    I think it’s good that we take all opportunities – chats, forums, blogs – to spread the good word… and the link!

    As for me, enjoying this little gem every day of the year!

  16. Transcontinental says:

    Erratum : I didn’t mean an executable, but an install for a search engine of a special form…. gosh, I’m no geekie am I ?! Well, I don’t know how to cook but I sure do like that cookin’ !

  17. Abhishek says:

    Dude I can give you away my girlfriend for this. You are a genius, and this addon can be immensely popular if given even slight exposure. This was something that I had been looking for ages. Why dont you post this on the official addons list. Trust me there are several others looking for it. Thanks man you rock. God Bless You.

  18. White Shadow says:

    Interesting πŸ˜‰
    We’ll see about the extension list.

Leave a Reply