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. Abhishek says:

    Sometimes this extension simply wont work. Any solution????

  2. White Shadow says:

    No idea. I’m not that familiar with FF plugin development, so I don’t even have an idea where to start looking for the bug.

  3. Transcontinental says:

    I never had any problems using this little marvel with Firefox, that’s all I can say, besides the smartness of these jewels, that is Firefox and this extension!

  4. itssri says:

    Hi,

    I do quite a bit of web content research and I had been searching for something like this for ages; the closest I could get to was using the Yahoo toolbar, but it sucks! and many times, I installed it whenever I was desperate and uninstalled it as soon as it frustrated me. Now, you have made my day.

    This is the exact phrase I used on google today – “firefox extension to search in current website” – and your site was listed second. I wonder how I missed it earlier?

    Another plugin/extension that is wanting is:
    “Ctrl+Click” on anything on a browsed page = parse a link from that anything and open in another tab

    If you could do it, you are sure to be deluged with …

    Hey! BTW, has Abhishek sent you at least a picture of his girlfriend πŸ˜‰

    ItsSri

  5. White Shadow says:

    Heh, thanks πŸ™‚

    Can you elaborate on that “anything on a browsed page” comment? A plaintext link would be one possibility; what else?

    Edit : this addon might be similar to what you’re looking for.

  6. itssri says:

    Hi,

    Thanks a lot.

    Super DragAndGo is almost what I needed, although instead of drag, “Ctrl-Click” might have more universal appeal (MS Office hangover, u c).

    And yes, I did mean plain text link, rather a contiguous block of text – isn’t the extra action of selecting that, a drag on our mouse entangled wrists (or the touch-pad chafed fingertips), especially when you are researching thousands of sites.

    Regards,

    ItsSri

  7. emarell says:

    Hallelujah. At last I’ve found you.

    Why this obviously desirable adaptation of a publicly accessible Google feature has been so elusive for Firefox 2, then 3, users is beyond me. Many thanks to White Shadow.

    P.S. Would love to see a Firefox 3 toolbar button for it – that opens a pop-up site search input field for this search alone. The button offered above by ithinc isn’t for Firefox 3 as far as I can tell. Could we have one that does not require additional software (the way ithinc’s requires Custom Buttons 2)?

  8. White Shadow says:

    πŸ™‚

    I did some quick research and it appears that it isn’t possible to create a custom button for FF without packaging it as an extension, or using a third-party button manager addon (like Custom Buttons 2).

  9. nettrotter says:

    such a good extension. I like it very much.

  10. nettrotter says:

    Maybe you guy would like to try a bookmarklet. It doenst use any third-party plugin. Just put this bookmarklet in you bookmark toolbar. When you click it, it will pop up window for keyword input to search. the other way is that after you highlight some keyword in the page, click the bookmarklet and it will do search the current site from that keyword via google.

    javascript:q%20=%20%22%22%20+%20(window.getSelection%20?%20window.getSelection()%20:%20document.getSelection%20?%20document.getSelection()%20:%20document.selection.createRange().text);%20if%20(!q)%20q%20=%20prompt(%22%E8%AF%B7%E8%BE%93%E5%85%A5%E5%85%B3%E9%94%AE%E8%AF%8D:%22,%20%22%22);%20if%20(q!=null)%20{var%20qlocation=%22%20%22;qlocation=(%22http://www.google.com/search?num=100&hl=zh-CN&newwindow=1&q=site:%22%20+%20escape(location.hostname)+%22+%22+q+%22%22);window.open(qlocation);}%20void%200

  11. White Shadow says:

    @nettrotter – That looks good, I’m sure some users will find it useful.

  12. Mark says:

    Thanks very much for this, I’ve been looking all morning for something that will give me a google site search from the firefox search bar.

  13. John Macpherson says:

    On the new version Firefox i get an error – not compatible with version 3.5.6.

    Hopefully it can be sorted as this looks like a great extension.

  14. White Shadow says:

    Weird, works fine with 3.5.7 here. Try the slightly modified version I just uploaded.

  15. John Macpherson says:

    Thanks for looking, much appreciated, would love to say it worked but:

    Now getting an error “invalid file hash -261”

  16. White Shadow says:

    No idea why that happens, but you could just download the .xpi and install it manually (drag & drop to FF should work).

  17. Mark says:

    I realised after a couple of days that after you have opened/closed multiple windows, this stops working (clicking on the search button does nothing). But the Mozilla plugin site now has the ‘Search Site’ plugin (https://addons.mozilla.org/en-US/firefox/addon/14381) working for 3.5.x which does the same job.

  18. log121 says:

    Here you have to install new engine, quite unnecessary if you need to perform search once. There’s easier way – “Current Site Search” FireFox addon on mozilla site

  19. emarell says:

    It’s been a while since my previous post here, but a just weeks ago I got this Firefox (now I have v3.5.7) add-on: “Search Site 1.6, by DW-dev” Won’t work on Firefox versions older than v3.

    Search Site 1.6 puts its own clickable, green icon inside the search field of the multi-engine Search Bar. If you click this green icon instead of the normal magnifying glass, you’ll be searching the currently displayed site for whatever text you placed in the Search Bar’s field – using whichever engine you’ve scrolled to.

    The same green icon will appear in the context menu of the displayed page. Here it is usable after you’ve highlighted some in-page text to search for.

    Sounds complicated – but really it isn’t. Check out the Firefox Add-on page for more clarity https://addons.mozilla.org/en-US/firefox/addon/14381

Leave a Reply