Firefox, OpenSearch and Autocomplete

When you start typing a search in Firefox's search box, using the Google search plugin, you get a list of suggested search terms. I thought it would be useful to be able to use this elsewhere, so here's how to do it:

First, create an OpenSearch description file, opensearch.xml:

<OpenSearchDescription xmlns="">
<Description>Example Search Engine</Description>
<Url type="application/x-suggestions+json" method="GET" template=""/>
  <Param name="q" value="{searchTerms}"/>
<Url type="text/html" method="GET" template="http:/">
  <Param name="q" value="{searchTerms}"/>

Put it somewhere online and link to it from the head section of an HTML page:

<link rel="search" title="Example" href="opensearch.xml" type="application/opensearchdescription+xml"/>

Make an autocomplete script, at the application/x-suggestions+json URL described in the OpenSearch description file, that returns a JSON-encoded array of a) the original search term and b) an array of suggested terms:

$terms = array('firefox', 'firefox 3'); // the suggested terms
print json_encode(array($_REQUEST['q'], $terms));

Here's what Google's suggestions output looks like.

Provide an HTML page at the text/html URL described in the OpenSearch description file that will provide the actual search results.

See also: