Firefox 2.0とIE7での検索バー

Firefox 2.0とIE7の検索バー(右上にあるやつ)はOpenSearchというXMLに基づいた検索機能を提供している。これについてテストした。

検索バー(Firefox2.0)用XML

Firefox2.0では以下で述べるsuggestion機能があったり、いろんなことができる。

<?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>firefox search test</ShortName>
<Description>search plugin of firefox test</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:image/x-icon;base64,XXXX</Image>
<Url type="text/html" method="post" template="http://foo.example.jp/search-plugin.php">
  <Param name="q" value="{searchTerms}"/>
</Url>
<Url type="application/x-suggestions+json" method="get" template="http://foo.example.jp/json.php">
  <Param name="q" value="{searchTerms}"/>
</Url>
<moz:SearchForm>http://foo.example.jp/</moz:SearchForm>
</OpenSearchDescription>

Image要素に指定するiconはThe data: URI kitchenに16x16のiconファイルを指定してできたBase64の値を指定する。

検索バー(IE7(RC1だけど))用XML

IE7ではあまり機能がない。検索スクリプトもmethod="get"のみみたいだ。

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>IE7 search test</ShortName>
<Description>search plugin of IE7 test</Description>
<Url type="text/html" template="http://foo.example.jp/search-plugin.php?q={searchTerms}" />
</OpenSearchDescription>

検索バーへのインストール(html指定)

htmlのhead中に以下を記述することで、検索バーに表示ができる。

<link rel="search" type="application/opensearchdescription+xml" title="search plugin test" href="http://foo.example.jp/search-plugin.xml">

検索バーへのインストール(ダウンロード)

上記の方法以外に、(?:Java|ECMA)Scriptでダウンロードさせる方法もあるようだ。

<script type="text/javascript">
<!--
var pluginURL = "http://foo.example.jp/";

function openSearch(xmlFile) {
        if((typeof window.external == "object") &&
                (typeof window.external.AddSearchProvider == "function")){
                window.external.AddSearchProvider(pluginURL + xmlFile);
        }
        else alert("このブラウザは、 サポートしていません。");
}
// -->
</script>
....
<a href="javascript:openSearch('search-plugin.xml');">配布</a>

検索用サンプルスクリプト

上記サンプルではクエリに"q"を指定しているのでそれを取得してなんかすればよい。

<?php
$q = htmlspecialchars($_POST["q"] ? $_POST["q"] : $_GET["q"]);
if (!$q) {
        $q = 'no set';
}
....
// 検索結果を出力
?>

候補用サンプルスクリプト(firefox2.0のみ)

firefox2.0では検索バーに入力ごと(入力をやめたあとに指定したスクリプトが起動する。そこでは以下のように、入力した文字列をそれに対応する候補のJSONを出力すればよいみたい(日本語はUTF-8)。

["fir", ["firefox", "Mozilla Firefox"]]

問題点

おなじOpenSearchとはいえ、現状では、Firefox2.0とIE7用の検索用XMLがちがう(IE7がFirefox2.0用のXMLを正しいものと認識してくれない)のでUAを調べてそれぞれのものを指定するようにしないといけないようだ。