当我在 Google Chrome 的 Omnibox 输入某些 URL 时,会在其中看到这样的消息:“按 TAB 键在 $URL 中搜索”。例如,有一些俄罗斯网站如 habrahabr.ru 或 yandex.ru。按下 TAB 后,您将能够在该网站中进行搜索,而不是在您的搜索引擎中进行搜索。
如何使我的网站可以实现这个功能?也许,我需要在我的网站页面中编写一些特殊代码吗?
通常情况下,Chrome 通过用户偏好设置(通过 chrome://settings/searchEngines
)来处理这个问题。
然而,如果您想特别为自己的用户实现此功能,您需要在您的网站上添加 OSD (Open Search Description)。
如何在个人网站中使用 Google Chrome 的 Omnibox [TAB] 功能?
然后您需要将这个 XML 文件添加到您网站的根目录,并在您的 <head>
标签中链接到它:
<link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml" />
现在,访问你的网页的用户会自动将你网站的搜索信息放置到 Chrome 内部设置的 chrome://settings/searchEngines
中。
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>Your website name (shorter = better)</ShortName>
<Description>
Description about your website search here
</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon">your site favicon</Image>
<Url type="text/html" method="get" template="http://www.yoursite.com/search/?query={searchTerms}"/>
</OpenSearchDescription>
重要的部分是<url>
项。{searchTerms}
将被替换为用户在万能搜索栏中搜索的内容。
这里有一个指向OpenSearch获取更多信息的链接。
@element119 提供的答案非常好,但这里提供了稍微调整过的代码,以支持搜索建议和 Mozilla Support。
请按照以下步骤为您的网站实现 Omnibox 支持。
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<script/>
<ShortName>Site Name</ShortName>
<Description>Site Description (eg: Search sitename)</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon">Favicon url</Image>
<Url type="application/x-suggestions+json" method="GET" template="http://suggestqueries.google.com/complete/search?output=firefox&q={searchTerms}" />
<Url type="text/html" method="GET" template="http://yoursite.com/?s={searchTerms}" />
<SearchForm>http://yoursite.com/</SearchForm>
</OpenSearchDescription>
将search.xml上传到您网站的根目录。
将以下meta标签添加到您网站的<head>
标签中
<link rel="search" href="http://www.yoursite.com/search.xml" type="application/opensearchdescription+xml" title="You site name"/>
请务必将域名网址替换为您的域名。
<SearchForm>
还是 <moz:SearchForm>
?我在 OpenSearch 文档中似乎找不到 SearchForm
,而我在网上找到的所有其他资源都使用 <moz:SearchForm>
。 - Niels R.谷歌浏览器不再支持自动发现特定网站搜索
@googlechrome 推特账号:
我们已经做出了更改,新的网站搜索快捷方式不再自动添加到Chrome搜索栏中。我们这样做是为了避免将人们可能没有使用的网站搜索建议弄乱它。
此回复最初在评论中提供(由Olivier Lalonde提供),但我重新发布它以便更容易找到(例如,当我最初寻找答案时,我没看到这条评论)。