从网页安装URI方案的服务处理程序 [英] Install a service handler for URI scheme from webpage
问题描述
通过Chrome访问Google Mail或Google日历时,地址栏中会出现一个小图标,允许为URI方案安装自定义服务处理程序(用红色方块标记)。
Tooltip for图标是:此页面要安装服务处理程序
。当我点击图标并允许Google Mail处理 mailto:
链接时,所有 mailto:
链接都在Chrome中打开。 / p>
是否有可能创建网页,以便像我的Google Mail那样为自定义URI方案安装自定义处理程序?
window.navigator.registerProtocolHandler(protocol,uri,title);
-
协议
是网站希望处理的协议,指定为一个字符串。 -
uri
是处理程序的URI字符串。您可以添加%s来指明要插入处理文档的转义URI。 作为字符串呈现给用户的处理程序。
特别针对Chrome,有一个限制,不允许使用自定义方案不要以 web +
前缀开头(标准的除外: mailto
, mms
, nntp
, rtsp
和 webcal
) 。因此,如果您想要将您的Web应用程序注册为GMail的服务处理程序,则应该这样写:
navigator.registerProtocolHandler (mailto,https://www.example.com/?uri=%s,示例邮件);
或
navigator.registerProtocolHandler(web + myscheme,https://www.example.com/?uri=%s,我的酷应用程序);
注意URI模式,它必须包含%s
它将被链接用户点击的实际URI取代。例如:
< a href =web + myscheme:some + data>在我的酷应用程序 < / A>
会触发 GET
请求至 http://www.example.com/?uri=web%2Bmyscheme%3Asome%20data
以下是一些有用的链接:
- 标准 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 /注册自定义协议处理程序
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).
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.
Is it possible to create webpage that will be able to install custom handler for my custom URI scheme just like Google Mail do?
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
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.
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");
or
navigator.registerProtocolHandler("web+myscheme", "https://www.example.com/?uri=%s", "My Cool App");
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>
will trigger GET
request to http://www.example.com/?uri=web%2Bmyscheme%3Asome%20data
Here are some useful links:
- Standard 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-a-custom-protocol-handler
这篇关于从网页安装URI方案的服务处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!