通过自定义协议URL protocal 在网页端打开本地应用程序(兼容各种浏览器)

在安装应用程序时,向系统注册私有自定义协议的方式,可以在网页端通过url的方式打开本地应用程序。

我们常见的协议有http://, https://,ftb://等。

通过自定义协议的方式比安装插件的方式更加方便,自然。

系统注册自定义协议需要一个配置文件,如下:

Windows Registry Editor Version 5.00
 
[HKEY_CLASSES_ROOT\calc]
@="URL:AutoHotKey calc Protocol"
"URL Protocol"=""
 
[HKEY_CLASSES_ROOT\calc\DefaultIcon]
@="calc.exe,1"
 
[HKEY_CLASSES_ROOT\calc\shell]
 
[HKEY_CLASSES_ROOT\calc\shell\open]
 
[HKEY_CLASSES_ROOT\calc\shell\open\command]

@="\"C:\\Windows\\System32\\calc.exe\" \"%1\""

其中 C:\\Windows\\System32\\calc.exe 就是本地系统应用程序安装路径。

     calc是协议名称

    注册配置文件一般保存为**.reg。

文件名称必须命名为注册表中的名称,该配置文件为calc.reg

双击打开,注册成功。

在浏览器中输入calc://就可以成功打开应用程序。

存在的问题:

存在的需求还有检测本地是否安装应用程序,如果没有则提醒下载安装。我在网上找了一个js检测自定义协议是否注册的情形:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>
<body>
    <iframe id="trigger_protocol_ifrm" style="display:none"></iframe>
    <input id="protocolTxt" type="text" value="" />
    <input id="checkBtn" type="button" value="检测是否支持该协议" />
    <script>
    document.getElementById('checkBtn').onclick = function(){
        var url = document.getElementById('protocolTxt').value;
        launchApplication(url, function(){
            alert('success');
        }, function(){
            alert('fail');
        });
    };
    
    var isDone = true;
    var timeout;
    var assistEl = document.getElementById('checkBtn');
    var triggerEl = document.getElementById('trigger_protocol_ifrm');
    
    function done(){
        isDone = true;
        assistEl.onblur = null;
        triggerEl.onerror = null;
        clearTimeout(timeout);
    }
    
    function launchApplication(url, success, fail){
        if(!isDone)return;
        isDone = false;
        
        assistEl.focus();
        assistEl.onblur = function(){
            if(document.activeElement && document.activeElement !== assistEl){
                assistEl.focus();
            } else {
                done();
                success();          
            }
        };
        
        triggerEl.onerror = function(){
            done();
            fail();
        };
 
 
        try{
            triggerEl.src = url;
        }catch(e){
            done();
            fail();
            return;
        }
 
 
        timeout = setTimeout(function(){
            done();
            fail();
        }, 1800);
    }
    </script>
</body>

</html>

参考博客:https://blog.csdn.net/cainiaokan/article/details/44103361

猜你喜欢

转载自blog.csdn.net/zhangyani_502000/article/details/81009907