Creating OpenSearch plugins for Firefox

  • Revision slug: Creating_OpenSearch_plugins_for_Firefox
  • Revision title: Creating OpenSearch plugins for Firefox
  • Revision id: 377359
  • Created:
  • Creator: THECONSERVATIVE53
  • Is current revision? Да
  • комментировать

Revision Content

{{ fx_minversion_header("2") }}

Firefox 2 supports the OpenSearch description format for search plugins. Plugins that use the OpenSearch description syntax are compatible with IE 7 and Firefox. Because of this, they are the recommended format for use on the web.

Firefox also supports additional search capabilities not included in the OpenSearch description syntax, such as search suggestions and the SearchForm element. This article will focus on creating OpenSearch-compatible search plugins that support these additional Firefox-specific features.

OpenSearch description files can also be advertised as described in Autodiscovery of search plugins, and can be installed programmatically as described in Adding search engines from web pages.

OpenSearch description file

The XML file describing a search engine is actually quite simple, following the basic template below. Sections in bold need to be customized based on the needs of the specific search engine plugin you're writing.

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
                       xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  <ShortName>engineName</ShortName>
  <Description>engineDescription</Description>
  <InputEncoding>inputEncoding</InputEncoding>
  <Image width="16" height="16" type="image/x-icon">data:image/x-icon;base64,imageData</Image>
  <Url type="text/html" method="method" template="searchURL">
    <Param name="paramName1" value="paramValue1"/>
    ...
    <Param name="paramNameN" value="paramValueN"/>
  </Url>
  <Url type="application/x-suggestions+json" template="suggestionURL"/>
  <moz:SearchForm>searchFormURL</moz:SearchForm>
</OpenSearchDescription>
ShortName
A short name for the search engine.
Restrictions: The value must contain 16 or fewer characters of plain text. The value must not contain HTML or other markup.
Description
A brief description of the search engine.
Restrictions: The value must contain 1024 or fewer characters of plain text. The value must not contain HTML or other markup.
InputEncoding
The encoding to use for the data input to the search engine. Example: <InputEncoding>UTF-8</InputEncoding>.
Image
URI to an icon representative of the search engine. When possible, search engines should offer a 16x16 image of type "image/x-icon" and a 64x64 image of type "image/jpeg" or "image/png". The link may also use the data: URI scheme. One useful tool that you can use to construct the data to place here can be found here: The data: URI kitchen.
<Image height="16" width="16" type="image/x-icon">http://example.com/favicon.ico</Image>
  OR
<Image height="16" width="16">data:image/x-icon;base64,AAABAAEAEBAAA ... DAAA=</Image>
Firefox caches the icon as a base64 data: URI (search plug-ins are stored in the profile's "searchplugins" folder). http: URIs are changed to data: URIs when this is done.
Url
Describes the URL or URLs to use for the search. The method attribute indicates whether to use a GET or POST request to fetch the result. The template attribute indicates the base URL for the search query.
Note: Internet Explorer 7 does not support POST requests.
There are two URL types Firefox supports:
  • type="text/html" is used to specify the URL for the actual search query itself.
  • type="application/x-suggestions+json" is used to specify the URL to use for fetching search suggestions.
For either type of URL, you can use {searchTerms} to substitute the search terms entered by the user in the search bar. Other supported dynamic search parameters are described in OpenSearch 1.1 parameters.
For search suggestion queries, the specified URL template is used to fetch a suggestion list in JavaScript Object Notation (JSON) format. For details on how to implement search suggestion support on a server, see Supporting search suggestions in search plugins.

Image:SearchSuggestionSample.png

Param
The parameters that need to be passed in along with the search query, as key/value pairs. When specifying values, you can use {searchTerms} to insert the search terms entered by the user in the search bar.
Note: Internet Explorer 7 does not support this element.
SearchForm
The URL to go to to open up the search page at the site for which the plugin is designed to search. This provides a way for Firefox to let the user visit the web site directly.
Note: Since this element is Firefox-specific, and not part of the OpenSearch specification, we use the "moz:" XML namespace prefix in the example above to ensure that other user agents that don't support this element can safely ignore it.

Autodiscovery of search plugins

A web site that offers a search plugin can advertise it so that Firefox users can easily download and install the plugin.

To support autodiscovery, you simply need to add one line to the <head> section of your web page:

<link rel="search" type="application/opensearchdescription+xml" title="searchTitle" href="pluginURL">

Replace the italicized items as explained below:

searchTitle
The name of the search to perform, such as "Search MDC" or "Yahoo! Search". This value should match your plugin file's ShortName.
pluginURL
The URL to the XML search plugin, from which the browser can download it.

If your site offers multiple search plugins, you can support autodiscovery for them all. For example:

<link rel="search" type="application/opensearchdescription+xml" title="MySite: By Author" href="http://www.mysite.com/mysiteauthor.xml">
<link rel="search" type="application/opensearchdescription+xml" title="MySite: By Title" href="http://www.mysite.com/mysitetitle.xml">

This way, your site can offer plugins to search both by author and by title as separate entities.

Supporting automatic updates for OpenSearch plugins

{{ fx_minversion_note("3.5", "This section covers a feature introduced in Firefox 3.5.") }}

Starting in Firefox 3.5, OpenSearch plugins can be updated automatically.  To support this, they need to include an extra Url element of type "application/opensearchdescription+xml".  The rel attribute needs to be "self" and the template attribute needs to be the URL of the OpenSearch document to automatically update to.

For example:

<Url type="application/opensearchdescription+xml"
     rel="self"
     template="http://www.foo.com/mysearchdescription.xml" />
Note: At this time, addons.mozilla.org (AMO) doesn't support automatic updating of OpenSearch plugins. If you want to put your search plugin on AMO, you shouldn't use the auto-updating feature.

Troubleshooting Tips

If there is a mistake in your Search Plugin XML, you could run into errors when adding a discovered plugin in Firefox 2. The error message may not be entirely helpful, however, so the following tips could help you find the problem.

  • Your server should serve OpenSearch plugins using the MIME type application/opensearchdescription+xml.
  • Be sure that your Search Plugin XML is well formed. You can check by loading the file directly into Firefox. Ampersands in the template URL need to be escaped with &amp; and tags need to be closed with a trailing slash or matching end tag.
  • The xmlns attribute is important, without it you could get an error message indicating that "Firefox could not download the search plugin from: (URL)".
  • Note that you must include a text/html URL — search plugins including only Atom or RSS URL types (which is valid, but Firefox doesn't support) will also generate the "could not download the search plugin" error.
  • Remotely fetched favicons must not be larger than 10KB (see {{ Bug(361923) }}).

In addition, the search plugin service provides a logging mechanism that may be of use to plugin developers. Use about:config to set the pref 'browser.search.log' to true. Logging information will appear in Firefox's Error Console (Tools->Error Console) when search plugins are added.

Reference Material

{{ languages( { "es": "es/Creación_de_plugins_OpenSearch_para_Firefox", "de": "de/OpenSearch_Plugin_für_Firefox_erstellen", "ca": "ca/Creació_de_connectors_OpenSearch_per_al_Firefox", "fr": "fr/Création_de_plugins_OpenSearch_pour_Firefox", "ja": "ja/Creating_OpenSearch_plugins_for_Firefox", "pl": "pl/Tworzenie_wtyczek_OpenSearch_dla_Firefoksa", "pt": "pt/Criando_plugins_OpenSearch_para_o_Firefox" } ) }}

Revision Source

<p>{{ fx_minversion_header("2") }}</p>
<p><a href="/en/Firefox_2_for_developers" title="en/Firefox_2_for_developers">Firefox 2</a> supports the <a class="external" href="http://opensearch.org/">OpenSearch</a> description format for search plugins. Plugins that use the <a class="external" href="http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_description_document">OpenSearch description syntax</a> are compatible with IE 7 and Firefox. Because of this, they are the recommended format for use on the web.</p>
<p>Firefox also supports additional search capabilities not included in the <a class="external" href="http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_description_document">OpenSearch description syntax</a>, such as search suggestions and the <code>SearchForm</code> element. This article will focus on creating OpenSearch-compatible search plugins that support these additional Firefox-specific features.</p>
<p>OpenSearch description files can also be advertised as described in <a href="#Autodiscovery_of_search_plugins">Autodiscovery of search plugins</a>, and can be installed programmatically as described in <a href="/en/Adding_search_engines_from_web_pages" title="en/Adding_search_engines_from_web_pages">Adding search engines from web pages</a>.</p>
<h2 id="OpenSearch_description_file">OpenSearch description file</h2>
<p>The XML file describing a search engine is actually quite simple, following the basic template below. Sections in bold need to be customized based on the needs of the specific search engine plugin you're writing.</p>
<pre class="brush: xml">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
                       xmlns:moz="http://www.mozilla.org/2006/browser/search/"&gt;
  &lt;ShortName&gt;<strong>engineName</strong>&lt;/ShortName&gt;
  &lt;Description&gt;<strong>engineDescription</strong>&lt;/Description&gt;
  &lt;InputEncoding&gt;<strong>inputEncoding</strong>&lt;/InputEncoding&gt;
  &lt;Image width="16" height="16" type="image/x-icon"&gt;data:image/x-icon;base64,<strong>imageData</strong>&lt;/Image&gt;
  &lt;Url type="text/html" method="<strong>method</strong>" template="<strong>searchURL</strong>"&gt;
    &lt;Param name="<strong>paramName1</strong>" value="<strong>paramValue1</strong>"/&gt;
    ...
    &lt;Param name="<strong>paramNameN</strong>" value="<strong>paramValueN</strong>"/&gt;
  &lt;/Url&gt;
  &lt;Url type="application/x-suggestions+json" template="<strong>suggestionURL</strong>"/&gt;
  &lt;moz:SearchForm&gt;<strong>searchFormURL</strong>&lt;/moz:SearchForm&gt;
&lt;/OpenSearchDescription&gt;</pre>
<dl>
  <dt>
    <strong>ShortName</strong></dt>
  <dd>
    A short name for the search engine.</dd>
  <dd>
    <strong>Restrictions:</strong> The value must contain 16 or fewer characters of plain text. The value must not contain HTML or other markup.</dd>
</dl>
<dl>
  <dt>
    <strong>Description</strong></dt>
  <dd>
    A brief description of the search engine.</dd>
  <dd>
    <strong>Restrictions:</strong> The value must contain 1024 or fewer characters of plain text. The value must not contain HTML or other markup.</dd>
</dl>
<dl>
  <dt>
    <strong>InputEncoding</strong></dt>
  <dd>
    The encoding to use for the data input to the search engine. Example: <code>&lt;InputEncoding&gt;UTF-8&lt;/InputEncoding&gt;</code>.</dd>
</dl>
<dl>
  <dt>
    <strong>Image</strong></dt>
  <dd>
    URI to an icon representative of the search engine. When possible, search engines should offer a 16x16 image of type "image/x-icon" and a 64x64 image of type "image/jpeg" or "image/png". The link may also use the <a class="external" href="http://en.wikipedia.org/wiki/Data:_URI_scheme">data: URI scheme</a>. One useful tool that you can use to construct the data to place here can be found here: <a class="external" href="http://software.hixie.ch/utilities/cgi/data/data">The data: URI kitchen</a>.
    <pre class="eval">
&lt;Image height="16" width="16" type="image/x-icon"&gt;<a class="external" href="http://example.com/favicon.ico" rel="freelink">http://example.com/favicon.ico</a>&lt;/Image&gt;
&nbsp;&nbsp;OR
&lt;Image height="16" width="16"&gt;data:image/x-icon;base64,AAABAAEAEBAAA ... DAAA=&lt;/Image&gt;
</pre>
    Firefox caches the icon as a base64 data: URI (search plug-ins are stored in the profile's "searchplugins" folder). http: URIs are changed to data: URIs when this is done.</dd>
</dl>
<dl>
  <dt>
    <strong>Url</strong></dt>
  <dd>
    Describes the URL or URLs to use for the search. The <code>method</code> attribute indicates whether to use a <code>GET</code> or <code>POST</code> request to fetch the result. The <code>template</code> attribute indicates the base URL for the search query.</dd>
  <dd>
    <div class="note">
      <strong>Note:</strong> Internet Explorer 7 does not support <code>POST</code> requests.</div>
  </dd>
</dl>
<dl>
  <dd>
    There are two URL types Firefox supports:</dd>
</dl>
<ul>
  <li><code>type="text/html"</code> is used to specify the URL for the actual search query itself.</li>
  <li><code>type="application/x-suggestions+json"</code> is used to specify the URL to use for fetching search suggestions.</li>
</ul>
<dl>
  <dd>
    For either type of URL, you can use <code>{searchTerms}</code> to substitute the search terms entered by the user in the search bar. Other supported dynamic search parameters are described in <a class="external" href="http://www.opensearch.org/Specifications/OpenSearch/1.1/Draft_3#OpenSearch_1.1_parameters">OpenSearch 1.1 parameters</a>.</dd>
</dl>
<dl>
  <dd>
    For search suggestion queries, the specified URL template is used to fetch a suggestion list in JavaScript Object Notation (JSON) format. For details on how to implement search suggestion support on a server, see <a href="/en/Supporting_search_suggestions_in_search_plugins" title="en/Supporting_search_suggestions_in_search_plugins">Supporting search suggestions in search plugins</a>.</dd>
</dl>
<p><img alt="Image:SearchSuggestionSample.png" class="internal" src="/@api/deki/files/358/=SearchSuggestionSample.png" /></p>
<dl>
  <dt>
    <strong>Param</strong></dt>
  <dd>
    The parameters that need to be passed in along with the search query, as key/value pairs. When specifying values, you can use <code>{searchTerms}</code> to insert the search terms entered by the user in the search bar.</dd>
  <dd>
    <div class="note">
      <strong>Note:</strong> Internet Explorer 7 does not support this element.</div>
  </dd>
</dl>
<dl>
  <dt>
    <strong>SearchForm</strong></dt>
  <dd>
    The URL to go to to open up the search page at the site for which the plugin is designed to search. This provides a way for Firefox to let the user visit the web site directly.</dd>
  <dd>
    <div class="note">
      <strong>Note:</strong> Since this element is Firefox-specific, and not part of the OpenSearch specification, we use the "<code>moz:</code>" XML namespace prefix in the example above to ensure that other user agents that don't support this element can safely ignore it.</div>
  </dd>
</dl>
<h2 id="Autodiscovery_of_search_plugins">Autodiscovery of search plugins</h2>
<p>A web site that offers a search plugin can advertise it so that Firefox users can easily download and install the plugin.</p>
<p>To support autodiscovery, you simply need to add one line to the <code>&lt;head&gt;</code> section of your web page:</p>
<pre class="eval">
&lt;link rel="search" type="application/opensearchdescription+xml" title="<em>searchTitle</em>" href="<em>pluginURL</em>"&gt;
</pre>
<p>Replace the italicized items as explained below:</p>
<dl>
  <dt>
    <strong>searchTitle</strong></dt>
  <dd>
    The name of the search to perform, such as "Search MDC" or "Yahoo! Search". This value should match your plugin file's ShortName.</dd>
</dl>
<dl>
  <dt>
    <strong>pluginURL</strong></dt>
  <dd>
    The URL to the XML search plugin, from which the browser can download it.</dd>
</dl>
<p>If your site offers multiple search plugins, you can support autodiscovery for them all. For example:</p>
<pre class="eval">
&lt;link rel="search" type="application/opensearchdescription+xml" title="MySite: By Author" href="<a class="external" href="http://www.mysite.com/mysiteauthor.xml" rel="freelink">http://www.mysite.com/mysiteauthor.xml</a>"&gt;
&lt;link rel="search" type="application/opensearchdescription+xml" title="MySite: By Title" href="<a class="external" href="http://www.mysite.com/mysitetitle.xml" rel="freelink">http://www.mysite.com/mysitetitle.xml</a>"&gt;
</pre>
<p>This way, your site can offer plugins to search both by author and by title as separate entities.</p>
<h2 id="Supporting_automatic_updates_for_OpenSearch_plugins">Supporting automatic updates for OpenSearch plugins</h2>
<p>{{ fx_minversion_note("3.5", "This section covers a feature introduced in Firefox 3.5.") }}</p>
<p>Starting in Firefox 3.5, OpenSearch plugins can be updated automatically. &nbsp;To support this, they need to include an extra <code>Url</code> element of type "<code>application/opensearchdescription+xml</code>".&nbsp; The <code>rel</code> attribute needs to be "<code>self</code>"&nbsp;and the template attribute needs to be the URL&nbsp;of the OpenSearch document to automatically update to.</p>
<p>For example:</p>
<pre>
&lt;Url type="application/opensearchdescription+xml"
&nbsp;&nbsp;&nbsp;&nbsp; rel="self"
&nbsp;&nbsp;&nbsp;&nbsp; template="http://www.foo.com/mysearchdescription.xml" /&gt;
</pre>
<div class="note">
  <strong>Note:</strong> At this time, <a class="external" href="http://addons.mozilla.org" title="http://addons.mozilla.org/">addons.mozilla.org</a> (AMO)&nbsp;doesn't support automatic updating of OpenSearch plugins. If you want to put your search plugin on AMO, you shouldn't use the auto-updating feature.</div>
<h2 id="Troubleshooting_Tips">Troubleshooting Tips</h2>
<p>If there is a mistake in your Search Plugin XML, you could run into errors when adding a discovered plugin in Firefox 2. The error message may not be entirely helpful, however, so the following tips could help you find the problem.</p>
<ul>
  <li>Your server should serve OpenSearch plugins using the MIME type <code>application/opensearchdescription+xml</code>.</li>
  <li>Be sure that your Search Plugin XML is well formed. You can check by loading the file directly into Firefox. Ampersands in the template URL need to be escaped with &amp;amp; and tags need to be closed with a trailing slash or matching end tag.</li>
  <li>The <code>xmlns</code> attribute is important, without it you could get an error message indicating that "Firefox could not download the search plugin from: (URL)".</li>
  <li>Note that you <strong>must</strong> include a <code>text/html</code> URL — search plugins including only Atom or <a href="/en/RSS" title="en/RSS">RSS</a> URL types (which is valid, but Firefox doesn't support) will also generate the "could not download the search plugin" error.</li>
  <li>Remotely fetched favicons must not be larger than 10KB (see {{ Bug(361923) }}).</li>
</ul>
<p>In addition, the search plugin service provides a logging mechanism that may be of use to plugin developers. Use <em>about:config</em> to set the pref '<code>browser.search.log</code>' to <code>true</code>. Logging information will appear in Firefox's <a href="/en/Error_Console" title="en/Error_Console">Error Console</a> (Tools-&gt;Error Console) when search plugins are added.</p>
<h2 id="Reference_Material">Reference Material</h2>
<ul>
  <li><a class="external" href="http://opensearch.org/">OpenSearch Documentation</a>, <a class="external" href="http://www.opensearch.org/Specifications/OpenSearch/Extensions/Parameter/1.0">OpenSearch Documentation about the Url and Param element</a></li>
  <li>Technorati.com has a <a class="external" href="http://technorati.com/osd.xml">working osd.xml</a></li>
  <li>More on Autodiscovery difficulties at bugzilla {{ Bug(340208) }}</li>
  <li><a class="external" href="http://en.wikipedia.org/wiki/Data:_URI_scheme"><code>data:</code> URI scheme</a></li>
  <li><a class="external" href="http://www.searchplugins.net">searchplugins.net</a> - create OpenSearch plugins for use with Firefox 2. <a class="external" href="http://www.searchplugins.net/pluginlist.aspx">List of generated search plugins</a></li>
  <li><a class="external" href="http://keijisaito.info/ready2search/e/">Ready2Search</a> - create OpenSearch plugins. <a class="external" href="http://keijisaito.info/arc/search/en_make_plugin.htm">Customized Search through Ready2Search</a></li>
</ul>
<p>{{ languages( { "es": "es/Creación_de_plugins_OpenSearch_para_Firefox", "de": "de/OpenSearch_Plugin_für_Firefox_erstellen", "ca": "ca/Creació_de_connectors_OpenSearch_per_al_Firefox", "fr": "fr/Création_de_plugins_OpenSearch_pour_Firefox", "ja": "ja/Creating_OpenSearch_plugins_for_Firefox", "pl": "pl/Tworzenie_wtyczek_OpenSearch_dla_Firefoksa", "pt": "pt/Criando_plugins_OpenSearch_para_o_Firefox" } ) }}</p>
Revert to this revision