从网页安装 URI 方案的服务处理程序

Install a service handler for URI scheme from webpage(从网页安装 URI 方案的服务处理程序)

本文介绍了从网页安装 URI 方案的服务处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Chrome 访问 Google Mail 或 Google Calendar 时,地址栏中会出现一个小图标,允许为 URI 方案安装自定义服务处理程序(图中用红色方块标记).

When accessing Google Mail or Google Calendar from Chrome, small icon appears in addressbar allowing to install custom service handler for URI scheme (marked with red square in picture).

图标的工具提示是:此页面要安装服务处理程序.当我点击图标并允许 Google Mail 处理 mailto: 链接时,所有 mailto: 链接都会在 Chrome 中打开.

Tooltip for icon is: This page wants to install a service handler. When I click icon and allow Google Mail to handle mailto: links, all mailto: links are opening in Chrome.

是否可以像 Google Mail 一样创建能够为我的自定义 URI 方案安装自定义处理程序的网页?

Is it possible to create webpage that will be able to install custom handler for my custom URI scheme just like Google Mail do?

推荐答案

对于 Chrome (13+)、Firefox (3.0+) 和 Opera (11.60+),可以使用以下方法将 Web 应用程序注册为自定义 URI 方案的服务处理程序JavaScript API:

For Chrome (13+), Firefox (3.0+) and Opera (11.60+) it is possible to register web application as service handler for custom URI scheme using JavaScript API:

window.navigator.registerProtocolHandler(protocol, uri, title);

  • protocol 是网站希望处理的协议,指定为字符串.
  • uri 是作为字符串的处理程序的 URI.您可以包含%s"以指示在何处插入要处理的文档的转义 URI.
  • title 是以字符串形式呈现给用户的处理程序的标题.
    • protocol is the protocol the site wishes to handle, specified as a string.
    • uri is the URI to the handler as a string. You can include "%s" to indicate where to insert the escaped URI of the document to be handled.
    • title is the title of the handler presented to the user as a string.
    • 特别是对于 Chrome 有一个限制,不允许使用不以 web+ 前缀开头的自定义方案(标准方案除外:mailtommsnntprtspwebcal).因此,如果您想像 GMail 一样将您的 Web 应用程序注册为服务处理程序,您应该编写如下内容:

      Specifically for Chrome there is a limitation that does not allow to use custom schemes that don't start with web+ prefix (except standard ones: mailto, mms, nntp, rtsp and webcal). So if you want to register your web app as service handler as GMail do, you should write something like this:

      navigator.registerProtocolHandler("mailto", "https://www.example.com/?uri=%s", "Example Mail");
      

      navigator.registerProtocolHandler("web+myscheme", "https://www.example.com/?uri=%s", "My Cool App");
      

      注意 URI 模式,它必须包含 %s ,它将替换为用户点击的链接的实际 URI.例如:

      Pay attention at URI pattern, it have to contain %s which will be replaced with actual URI of the link user clicks. For example:

      <a href="web+myscheme:some+data">Open in "My Cool App"</a>
      

      将触发 GET 请求到 http://www.example.com/?uri=web%2Bmyscheme%3Asome%20data

      这里有一些有用的链接:

      Here are some useful links:

      • 标准 http://www.whatwg.org/specs/web-apps/current-work/#custom-handlers
      • MDN https://developer.mozilla.org/en-US/docs/DOM/navigator.registerProtocolHandler
      • HTML5ROCKS http://updates.html5rocks.com/2011/06/Registering-自定义协议处理程序

      这篇关于从网页安装 URI 方案的服务处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从网页安装 URI 方案的服务处理程序

基础教程推荐