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!
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.
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 😛 )