嘿,我认为你在正确的轨道上。这绝对不是那么容易,但到目前为止,chrome 并不是我的问题,更像是 Edge + IE,但我的解决方案是假设它们不支持该协议,如果出现任何故障或者它们有时无法正确响应。
模糊/焦点是需要检查的,但您需要结合可见性变化来检查它。HTML5 Visiblity API 和这篇关于它的帖子帮助我找到了一个非常可靠的解决方案,除了上面提到的浏览器,因为它们在navigator.msLaunchUri函数方面存在一些问题,并且实现了自己的方法,不依赖于模糊/焦点. 但是该功能存在错误,并且始终无法正确响应。
你可以在这里找到我的 codepen 。希望这对您有所帮助,即使答案有点晚了。这也适用于移动浏览器,但我没有测试多个尚未适用于我的 Android 6.0.2。从长远来看可能需要一些调整,但我认为它非常可靠。
(function() {
var noProtocolHash = '#protocolXYnotsupported',
checkDelay = 800, // apps might start slowly
isBlurred = false,
inCheck = false,
inLauncherCheck = false,
tabVisible = (function(){
var stateKey,
eventKey,
keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})(),
isMSIE = function(){
var rv = -1;
if(navigator.appName == 'Microsoft Internet Explorer'){
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if(re.exec(ua) != null){
rv = parseFloat(RegExp.$1);
}
}
else if(navigator.appName == 'Netscape'){
var ua = navigator.userAgent;
var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
if(re.exec(ua) != null){
rv = parseFloat(RegExp.$1);
}
}
return (rv !== -1)? true: false;
},
isEdge = function(){
return window.navigator.userAgent.indexOf("Edge") > -1;
},
checkIfFocusLost = function($el){
try {
document.location.href = $el.attr("href");
} catch (ex) {
document.location.href = document.location.href + '/' + noProtocolHash;
}
setTimeout(checkVisibility, checkDelay);
},
checkVisibility = function(){
if(tabVisible() && !isBlurred){
handleNoProtocol();
}
else {
handleProtocol();
}
},
handleNoProtocol = function(){
$('.result').text('has no protocol');
inLauncherCheck = false;
},
handleProtocol = function(){
$('.result').text('has the protocol');
inLauncherCheck = false;
},
checkHash = function(){
if(document.location.hash === noProtocolHash){
handleNoProtocol();
}
},
checkLauncherProtocol = function($el){
inLauncherCheck = true;
navigator.msLaunchUri($el.attr("href"), function(){
handleProtocol();
},
function(){
handleNoProtocol();
});
setTimeout(function(){
// fallback when edge is not responding correctly
if(inLauncherCheck === true){
handleNoProtocol();
}
}, 500);
},
checkIfHasProtocol = function($el){
inCheck = true;
if(isEdge() || isMSIE()){
checkLauncherProtocol($el);
}
else {
checkIfFocusLost($el)
}
};
checkHash();
tabVisible(function(){
if(tabVisible() && inCheck){
handleProtocol();
inCheck = false;
}
});
window.addEventListener("blur", function(){
isBlurred = true;
});
window.addEventListener("focus", function(){
isBlurred = false;
inCheck = false;
});
window.checkIfHasProtocol = checkIfHasProtocol;
})();
$('.protocol').click(function(e) {
checkIfHasProtocol($(this));
e.preventDefault();
});
我的代码使用 Win10 测试:Chrome、Firefox、IE11、Edge + Android:Chrome (6.0.1)
还有这个 github 项目https://github.com/ismailhabib/custom-protocol-detection,它有一个类似的方法,可能维护得更多一些,但由于某些原因我无法让他们的解决方案工作,但也许这正是你需要什么。