如何检查是否支持自定义协议 [英] How to check if a custom protocol supported

查看:153
本文介绍了如何检查是否支持自定义协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用注册自己协议的软件。我们可以从浏览器运行应用程序,然后通过以下链接运行:

We are using software that registers its own protocol. We can run application from browser then by link like:

customprotocol://do_this.

但有没有办法检查用户系统支持的自定义协议?如果不是,我们首先要求用户安装软件。

but is there a way to check is such custom protocol supported by user`s system? If not we would like to ask user to install software first.

例如:

if (canHandle ('customprotocol')) {
     // run software
}
else {
    // ask to install
}

编辑
我了解protocolLong属性,但它仅适用于IE。

Edit I know about protocolLong property but it works only in IE.

推荐答案

不幸的是,实现这一目标并不容易。当然没有预先确定是否安装协议处理程序的方法。

Unfortunately, there's no easy way of achieving this. There's certainly no method of pre-determining whether or not the protocol handler is installed.

Internet Explorer ,正如您所提到的,具有 protocolLong 属性,但我是无法让它为所有自定义协议处理程序返回未知协议以外的任何内容 - 如果有人知道如何让IE返回正确的值,请告诉我,以便我可以更新此部分。我在IE中找到的最佳解决方案是附加到用户代理字符串或安装浏览器扩展程序以及展示Javascript可访问属性的应用程序。

Internet Explorer, as you mentioned, has the protocolLong property but I'm having trouble getting it to return anything other than "Unknown Protocol" for all custom protocol handlers -- if anyone knows how to get IE to return the correct value please let me know so I can update this section. The best solution I've found with IE is to append to the user agent string or install a browser extension along with your app that exposes a Javascript accessible property.

Firefox 是到目前为止最简单的主流浏览器,因为它将允许您尝试捕获失败的导航尝试。返回的错误对象包含 name 属性,其值为 NS_ERROR_UNKNOWN_PROTOCOL

Firefox is by far the easiest of the major browsers, as it will allow you to try and catch a navigation attempt that fails. The error object returned contains a name property whose value is NS_ERROR_UNKNOWN_PROTOCOL:

try {
    iframe.contentWindow.location.href = "randomprotocolstring://test/";
} catch(e) {
    if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL")
        window.location = "/download/";
}

Firefox将弹出自己的警告框:

Firefox will pop up with its own alert box:


Firefox不知道如何打开此地址,因为协议(randomprotocolstring)与任何程序都没有关联。

Firefox doesn't know how to open this address, because the protocol (randomprotocolstring) isn't associated with any program.

关闭此框后, catch 块将会执行,并且您有一个工作后备。

Once you close this box, the catch block will execute and you have a working fallback.

其次是 Opera ,它允许您使用可预测性法则来检测单击的自定义协议链接是否成功。如果自定义协议单击有效,则页面将保持相同的位置。如果没有安装处理程序,Opera将导航到错误页面。这样可以很容易地使用iframe进行检测:

Second is Opera, which allows you to employ the laws of predictability to detect success of a custom protocol link clicked. If a custom protocol click works, the page will remain the same location. If there is no handler installed, Opera will navigate to an error page. This makes it rather easy to detect with an iframe:

   iframe.contentWindow.location = "randomprotocolstring://test/";
   window.setTimeout(function () {
       try {
           alert(ifr.contentWindow.location); 
       } catch (e) { window.location = "/download/"; }
   }, 0);

setTimeout 这是为了确保我们在导航后检查位置。重要的是要注意,如果您尝试访问该页面,Opera会抛出ReferenceException(跨域安全性错误)。这没关系,因为我们需要知道的是,位置从更改为:空白,所以尝试... catch 工作得很好。

The setTimeout here is to make sure we check the location after navigation. It's important to note that if you try and access the page, Opera throws a ReferenceException (cross-domain security error). That doesn't matter, because all we need to know is that the location changed from about:blank, so a try...catch works just fine.

Chrome 在这方面正式糟透了。如果自定义协议处理程序失败,它绝对会拉链。如果处理程序有效......你猜对了......它绝对是拉链的。我不敢区分这两者。我很害怕。

Chrome officially sucks with this regard. If a custom protocol handler fails, it does absolutely zip. If the handler works... you guessed it... it does absolutely zip. No way of differentiating between the two, I'm afraid.

我还没有测试过 Safari ,但我担心它会像铬。

I haven't tested Safari but I fear it would be the same as Chrome.

欢迎您尝试我写的测试代码在调查时(我自己对它有既得利益)。它与Opera和Firefox交叉兼容,但目前在IE和Chrome中无效。

You're welcome to try the test code I wrote whilst investigating this (I had a vested interest in it myself). It's Opera and Firefox cross compatible but currently does nothing in IE and Chrome.

这篇关于如何检查是否支持自定义协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆