嘿,我认为你的方向是正确的。这绝对不是那么容易的,但是到目前为止Chrome并不是我的问题,更像是Edge和IE。但我的解决方案是假设它们不支持协议,如果任何事情失败或者它们没有正确地响应,就会出现这种情况。
模糊/聚焦是需要检查的内容,但您需要与可见性更改一起检查。HTML5可见性API和这篇文章(关于此)帮助我找到了一个相当可靠的解决方案,除了上述浏览器之外,因为它们在navigator.msLaunchUri函数上有一些问题,并且已经实现了自己的方法,不依赖于模糊/聚焦。但是该函数存在缺陷,并且并非总是正确响应。
您可以在这里找到我的codepen (链接)。希望这可以帮助您,尽管答案有点晚了。这也适用于移动浏览器,但我尚未进行多次测试,只在我的Android 6.0.2上进行了测试。长期来看可能需要一些调整,但我认为它相当可靠。
(function() {
var noProtocolHash = '#protocolXYnotsupported',
checkDelay = 800,
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(){
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,它采用了类似的方法,可能更加维护,但由于某些原因我无法让他们的解决方案工作,但也许这正是你需要的。