浏览器的搜索框如何工作?

时间:2009-06-28 05:20:52

标签: google-custom-search

我需要准确了解浏览器上的搜索框是如何工作的。我想用数学中的自定义搜索引擎替换搜索内容,例如维基百科和谷歌。您可以在iGoogle中看到它们。所以:

  

如何将Google CSE添加到   浏览器的搜索框?

4 个答案:

答案 0 :(得分:6)

您可以为您的网站创建所谓的“搜索提供商”。您的网站上应该有一个搜索页面,它会在您的网址中接受搜索关键字作为查询字符串,例如

  http://www.example.com/search?q=meaning+of+life

这也适用于Google自定义搜索。

您必须创建一个特殊的XML文件(例如,将其称为SearchProvider.xml)并将其放在您的Web服务器上:

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
   <ShortName>Example Search Provider</ShortName>
   <Description>Finds answers to the most important question of the universe</Description>
   <InputEncoding>UTF-8</InputEncoding>
   <Url type="text/html" template=" http://www.example.com/search?q={searchTerms}"/>
</OpenSearchDescription>

然后,您需要在页面的标题部分插入一个特殊的链接标记:

 <link title="Example Search Provider" rel="search"
     type="application/opensearchdescription+xml"
     href="http://www.example.com/SearchProvider.xml" />

您还可以插入指向您网页的链接,以便您的用户将搜索提供商添加到浏览器中:

<a href="#"
   onclick="javascript:window.external.AddSearchProvider('http://www.example.com/SearchProvider.xml');">
Example Search Provider</a>

答案 1 :(得分:2)

浏览器搜索框采用名为OpenSearch的技术实现。请参阅:http://www.opensearch.org/(网站目前已关闭?)

Mozilla有一个很好的页面,解释了如何为他们的浏览器实现这一点:https://developer.mozilla.org/en/Creating_OpenSearch_plugins_for_Firefox虽然那里有一些特定于Mozilla的细节,但该页面可以作为跨浏览器实现的良好起点。 / p>

在搜索框中添加自动完成功能有点棘手。首先按照Mozilla的描述添加自动完成查询URL。然后,您必须在服务器上制作符合各种浏览器所期望的响应。

了解Google为其支持的不同浏览器返回的内容:

* Firefox: http://suggestqueries.google.com/complete/search?client=firefox&hl=en-US&q=xmarks
      o Content-Type: text/javascript
      o Response body: ["xmarks",["xmarksthaspot","xmarksthescot","foxmarks safari","xmark.com","gmarks firefox","x marks foxmarks","xmarksthespot","xmarks ie","foxmarks addon","foxmarks for ie"]] 
* Safari: http://suggestqueries.google.com/complete/search?client=safari&hl=en-US&q=xmarks
      o Content-Type: application/json
      o Response body: ["xmarks",[["xmarksthaspot","18,400 results","0"],["xmarksthescot","196,000 results","1"],["foxmarks safari","148,000 results","2s"],["xmark.com","336,000 results","3s"],["gmarks firefox","50,700 results","4s"],["x marks foxmarks","13,500 results","5s"],["xmarksthespot","20,500 results","6"],["xmarks ie","96,400 results","7"],["foxmarks addon","210,000 results","8s"],["foxmarks for ie","191,000 results","9s"]]]
* Others: http://suggestqueries.google.com/complete/search?client=ie&hl=en-US&q=xmarks
      o Content-Type: text/javascript
      o Response body: I'm not sure it's relevant. It's essentially the exact same format as Safari above, but it's wrapped by a JavaScript call to window.google.ac.h(). I'm not 100% certain, but that looks like the callback to their HTML-page version of auto-completion and suggests to me that they don't really support opensearch auto-completion in anything but Firefox and Safari.

答案 2 :(得分:0)

它可能取决于浏览器到浏览器,但使用Firefox,它很容易定制:请参阅搜索栏以及如何轻松地将自定义搜索引擎添加到Firefox搜索栏。

答案 3 :(得分:0)

Microsoft提供了一个向IE添加自定义搜索提供程序的工具,Add to Search Bar扩展允许您对Firefox执行相同操作。