JavaScript - 如何检测自定义 URL 方案是否可用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24571548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:04:14  来源:igfitidea点击:

JavaScript - how to detect if the Custom URL scheme is available or not available?

javascriptinternet-explorergoogle-chromefirefoxurl-scheme

提问by

In Windows operating system i have a custom URI scheme, which is used from

在 Windows 操作系统中,我有一个自定义的 URI 方案,用于从

IE, Firefox, Opera, Safari, Google Chrome

IE、火狐、Opera、Safari、谷歌浏览器

to launch Juniper router VPN SSH client (like Cisco). Basically it works as below if the SSH Client is installed, from the web page VPN SSH Client can be launched.

启动瞻博网络路由器 VPN SSH 客户端(如 Cisco)。如果安装了 SSH 客户端,基本上它的工作原理如下,可以从网页 VPN SSH 客户端启动。

<a href="juniper:open"> VPN SSH Client </a>

Problem:

问题:

sometimes the user did not installed the Juniper router SSH client application from the CD/DVD box, therefore the juniper:open does nothing.

有时用户没有从 CD/DVD 盒安装 Juniper 路由器 SSH 客户端应用程序,因此 juniper:open 什么也不做。

So in that case, i need to detect weather or not the URL scheme is available.

所以在这种情况下,我需要检测天气或 URL 方案是否可用。

Therefore, I tried Javascript method but its not working exactly. because the juniper:open is actually not web link.

因此,我尝试了 Javascript 方法,但它无法正常工作。因为 juniper:open 实际上不是网络链接。

How do i then detect it please?

那我该如何检测呢?

<script>
// Fails
function test1(){
  window.location = 'juniper:open';
  setTimeout(function(){
    if(confirm('Missing. Download it now?')){
      document.location = 'https://www.junper-affiliate.com/setup.zip';
    }
  }, 25);

  //document.location = 'juniper:open';
}

// Fails
function test2(h){
  document.location=h;
  var time = (new Date()).getTime();
  setTimeout(function(){
   var now = (new Date()).getTime();
   if((now-time)<400) {
    if(confirm('Missing. Download it now?')){
     document.location = 'https://www.junper-affiliate.com/setup.zip';
    } else {
     document.location=h;
    }
   }
  }, 300);
 }
</script>

Then:

然后:

<a onclick="test1()">TEST 1</a>
<a href="juniper:open" onclick="test2(this.href);return false;">TEST 2</a>

采纳答案by mlr

EDITFollowing suggestions in comments:

编辑以下评论中的建议:

function goto(url, fallback) {
    var script = document.createElement('script'); 

    script.onload = function() { 
        document.location = url;
    } 
    script.onerror = function() { 
        document.location = fallback;
    } 
    script.setAttribute('src', url); 

    document.getElementsByTagName('head')[0].appendChild(script);

}

and

<a href="javascript:" onclick="goto('juniper:open', 'https://www.junper-affiliate.com/setup.zip');">TEST 2</a> 

The price you have to pay, is a duplicated request for the page.

您必须支付的价格是对页面的重复请求。

EDIT

编辑

This is a good workaround for same-origin policy, which prevents an asynch version using XMLHTTPRequest to work properly, since SOP restricts cross-domain requests to http and juniper:open would therefore always fail.

这是同源策略的一个很好的解决方法,它会阻止使用 XMLHTTPRequest 的异步版本正常工作,因为 SOP 将跨域请求限制为 http,因此 juniper:open 将始终失败。

function goto(url, fallback) {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open('GET', url, false);
    try {
        xmlhttp.send(null);                  // Send the request now
    } catch (e) {
        document.location = fallback;
        return;
    }

    // Throw an error if the request was not 200 OK 
    if (xmlhttp.status === 200) {
        document.location = url;
    } else {
        document.location = fallback;
    }
}

EDIT

编辑

The initial solution below doesn't actually work 'cause no exception is being thrown if the protocal is not supported.

下面的初始解决方案实际上不起作用,因为如果不支持协议,则不会引发异常。

  try {
    document.location = 'juniper:open';
  } catch (e) {
    document.location = 'https://www.junper-affiliate.com/setup.zip';
  }

回答by mlr

After searching a lot i have not came to anything which can help to solve my problem. But this is what i am now making

在搜索了很多之后,我还没有找到任何可以帮助解决我的问题的东西。但这就是我现在正在做的

1) Write a small tiny webserver which can run in the PC for 24/7 on boot, on crash. Use native python:

1) 编写一个小型网络服务器,它可以在 PC 中 24/7 开机、崩溃时全天候运行。使用原生python:

python -m SimpleHTTPServer [port]

2) tiny webserver will listen on port abnormal such as 10001 or such TCP ports which never get used

2) 微型网络服务器会监听异常端口,例如 10001 或从未使用过的 TCP 端口

3) from Browser we have to communicate with the http://localhost:10001just to get a reply

3)从浏览器我们必须与http://localhost:10001只是为了得到答复沟通

4) based on that reply we have to decide

4)根据那个答复,我们必须决定

Otherwise there is no way seems to be available

否则似乎没有办法可用

EDIT: Or you can do this way: InnoSetup - Is there any way to manually create cookie for Internet explorer?

编辑:或者你可以这样做:InnoSetup - 有没有办法为 Internet Explorer 手动创建 cookie?