使用Chrome在Windows 8 +中检测自定义协议处理程序 [英] Detecting Custom Protocol Handler in Windows 8+ with Chrome

查看:190
本文介绍了使用Chrome在Windows 8 +中检测自定义协议处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检测我的应用程序是否已安装并使用不同的浏览器来处理自定义协议。我查看了本网站上的其他问题,例如:
如何检测浏览器的协议处理程序?
,并且查看了这样的资源,以使其在大多数浏览器的大多数平台上都能正常工作。



在将此标记为重复项之前,请听我说... p>

我可以让我的功能在除Windows 8+上的Chrome之外的所有设备上运行。我无法像在Windows 7上那样在Chrome上使用窗口焦点方法,因为它弹出消息,要求我在商店中查找应用程序。



有没有什么办法可以检测Chrome浏览器的Windows 8+自定义协议处理程序?



更新:



使用onBlur检测它只能在Windows 7上运行,因为在8+版本中,如果它找不到可以打开协议的内容,就会打开从应用程序商店查找某个内容对话框这会让浏览器失去焦点。

解决方案

嘿,我认为你是在正确的轨道上。它确实不是那么容易,但到目前为止,chrome并不是我的问题,更像是Edge + IE,但我的解决方案是假设它们不支持协议,如果有任何失败或者它们有时无法正确响应。



模糊/焦点可以检查,但您需要结合可见性更改进行检查。 HTML5 Visiblity API和这篇文章关于它帮助我找出了一个非常稳固的解决方案,除了上面提到的浏览器外,因为它们与 navigator.msLaunchUri 函数,并且实现了不依赖于模糊/焦点的自己的方法。但是这个函数被窃听,并且一直没有正确响应。



你可以找到我的codepen 这里。希望这可以帮助你,尽管答案有点迟。这也适用于移动浏览器,但我没有测试多个,但为我的Android 6.0.2工作。从长远来看,可能需要做一些调整,但我认为它非常稳固。

 (function(){
var noProtocolHash ='#protocolXYnotsupported',
checkDelay = 800,//应用程序可能启动缓慢
isBlurred = false,
inCheck = false,
inLauncherCheck = false,

tabVisible =(函数(){
var stateKey,
eventKey,
keys = {
hidden:visibilitychange,
webkitHidden:webkitvisibilitychange,
mozHidden:mozvisibilitychange,
msHidden:msvisibilitychange
};
for(stateKey in keys){
if(stateKey in document){$ b $ (c)document.addEventListener(eventKey,c)b事件键=键[stateKey];
break;
}
}
返回函数(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)?真假;
},

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);

$ b 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;

$ b $ checkHash = function(){
if(document.location.hash === noProtocolHash){
handleNoProtocol();
}
},

checkLauncherProtocol = function($ el){
inLauncherCheck = true;

navigator.msLaunchUri($ el.attr(href),function(){
handleProtocol();
},
function(){
handleNoProtocol();
});
$ b $ setTimeout(function(){
//当边缘没有正确响应时回退$ b $ if(inLauncherCheck === true){
handleNoProtocol();
}
},500);
},

checkIfHasProtocol = function($ el){
inCheck = true;如果(isEdge()|| isMSIE()){
checkLauncherProtocol($ el);

if
}
else {
checkIfFocusLost($ el)
}
};

checkHash();
tabVisible($ {$ b $ handleProtocol();
inCheck = false;
} $ b $ if(tabVisible()&& inCheck) b});

window.addEventListener(blur,function(){
isBlurred = true;
});

window.addEventListener(focus,function(){
isBlurred = false;
inCheck = false;
});

window.checkIfHasProtocol = checkIfHasProtocol;
})(); ($)
$ b $('。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 哪种类似的方法可能有点维护,但我不能得到他们的解决方案工作的原因,但也许这正是你需要。


I am trying to detect if my application to handle a custom protocol is installed and working using the different browsers. I have looked at other questions on this site such as: How to detect browser's protocol handlers?, and have looked at resources like this to make it work on most platforms in most browsers.

Before you flag this as duplicate, hear me out...

I was able to get my function working on everything except Chrome on Windows 8+. I cannot use the window focus method on Chrome like I can on Windows 7 because it pops up the message that asks me to find an app in the store.

Is there any way (short of an extension) to detect a custom protocol handler in Windows 8+ on Chrome?

UPDATE:

Using an onBlur to detect it only works on Windows 7, because in 8+, if it doesn't find something to open your protocol, it opens the 'find something from the app store' dialog that makes the browser lose focus.

解决方案

Hey I think you were on the right track. It is definitly not that easy but chrome so far was not my problem, more like Edge + IE but my solution is assuming they don't support the protocol if anything fails or they don't respond correctly which they do sometimes.

The blur/focus is something to check but you need to check it in combination with a visibility change. The HTML5 Visiblity API and this post about it helped me figure out a solution that is pretty solid except the mentioned browsers above because they have some issues with the navigator.msLaunchUri function and have their own approach implemented which doesn't rely on blur/focus. But the function is bugged and doesn't respond correctly all the time.

You can find my codepen here. Hopefully that helps you even though it is a bit late for an answer. This works for mobile browsers as well but I didn't test multiple yet worked for my Android 6.0.2. Might need some tweaks in the long run but I think it is pretty solid.

(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();
});

My code is tested with Win10: Chrome, Firefox, IE11, Edge + Android: Chrome (6.0.1)

there is also this github project https://github.com/ismailhabib/custom-protocol-detection which sorta has a similar approach maybe a little bit more maintained but i couldn't get their solution working for some reasons but maybe this is exactly what you need.

这篇关于使用Chrome在Windows 8 +中检测自定义协议处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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