Install a service handler for URI scheme from webpage(从网页安装 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+
前缀开头的自定义方案(标准方案除外:mailto
、mms
、nntp
、rtsp
和 webcal
).因此,如果您想像 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 方案的服务处理程序
基础教程推荐
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01