惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
博客园 - 叶小钗
F
Fortinet All Blogs
GbyAI
GbyAI
Martin Fowler
Martin Fowler
博客园 - 聂微东
I
InfoQ
B
Blog
IT之家
IT之家
美团技术团队
L
LangChain Blog
小众软件
小众软件
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
Last Week in AI
Last Week in AI
A
About on SuperTechFans
博客园 - Franky
P
Proofpoint News Feed
罗磊的独立博客
月光博客
月光博客
V
Visual Studio Blog
MyScale Blog
MyScale Blog

博客园 - biggates

Linux Screen 超简单用法 ldconfig gdb前端Nemiver 在Ubuntu中使用autofs自动加载Virtual Box数据空间 -lm 必须放在 gcc 命令的最后 常用WinXP小工具集合 VritualBox 数据空间使用手记 在 Google Buzz 中使用格式 x64 WAMP + Oracle 环境搭建 查看Oracle Dump文件的结构 解决 Vista 下 IceGrid 调试时的监视错误 - biggates 使用163镜像在线安装CentOS 令人痛苦的SyncML同步协议错误 C#中从WSDL文件生成Web 服务 有图有真相,百度屏蔽网站行为仍在继续 [转]CSS Hacks 浏览器功能一览:IE曾经垃圾,现在垃圾,将来仍然很垃圾 学习笔记 使用 Java 正则表达式解析 VCard
三步实现 OpenSearch 功能
biggates · 2009-02-01 · via 博客园 - biggates

在网站上添加了 OpenSearch 功能以后,IE 7.0 和 Firefox 2.0 以上的浏览器(好吧,我承认在 Opera 和 Chrome 里没找到这个功能,但是 IE 8.0 RC 和 Firefox 3.05 是绝对没有问题的)就能够在自带的搜索栏里面添加这个网站的搜索功能了。

要添加 OpenSearch 功能,一共分三步:

第一步:实现 OpenSearch 功能;

第二步:编写 OpenSearch 描述文件;

第三步:添加对 OpenSearch 描述文件的引用。

那么,下来就开始吧:

第一步:实现 OpenSearch 功能。

这一个最简单,如果原来就有搜索,只需要使之支持 GET 参数传递就行了。

假设现在在网站上搜索 "abcd" ,实际的页面地址是 http://example.com/?q=abcd

第二步:编写 OpenSearch 描述文件。

只需在网站上添加一个 xml 文件即可,内容如下:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
3   <ShortName>Web Search</ShortName>
4   <Description>Use Example.com to search the Web.</Description>
5   <Tags>example web</Tags>
6   <Contact>admin@example.com</Contact>
7   <Url type="text/html" 
8        template="http://example.com/?q={searchTerms}"/>
9 </OpenSearchDescription>

为了便于描述,将这个文件命名为 opensearch.xml 放置在网站根目录下。

第三步:添加对 OpenSearch 描述文件的引用。

这一步也很简单,只需要在网站的模板的 <head> 标记中添加一句:

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

就行了。

更多:

通过修改 OpenSearch 描述文件,可以提供更多设置。

Firefox 支持使用 OpenSearch Suggestions 插件 来进行逐字提示搜索的功能。要实现这个功能,只需根据搜索参数返回按格式编写的 JSON 数据,然后在描述文件中添加相应的 Url 参数即可。

IE 支持(其实 Firefox 也支持)使用 OpenSearch Referrer 插件 为搜索添加 Referrer 参数。

参考资料:

OpenSearch 1.1 协议

OpenSearch 主页