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="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Example</ShortName>
<Description>Example Search Engine</Description>
<InputEncoding>UTF-8</InputEncoding>
<OutputEncoding>UTF-8</OutputEncoding>
<Url type="application/x-suggestions+json" method="GET" template="http://example.com/autocomplete"/>
  <Param name="q" value="{searchTerms}"/>
</Url>
<Url type="text/html" method="GET" template="http:/example.com/search">
  <Param name="q" value="{searchTerms}"/>
</Url>
</OpenSearchDescription>

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:

<?php
$terms = array('firefox', 'firefox 3'); // the suggested terms
header('Content-Type:application/json');
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: