用Chrome检测Windows8+中的自定义协议处理程序

Detecting Custom Protocol Handler in Windows 8+ with Chrome

本文关键字:自定义 协议 处理 程序 Chrome 检测 Windows8+      更新时间:2023-09-26

我正在尝试检测我的应用程序是否已安装并使用不同的浏览器来处理自定义协议。我在这个网站上查看了其他问题,例如:如何检测浏览器';s协议处理程序?,并研究了类似的资源,使其在大多数浏览器的大多数平台上运行。

在你将此标记为重复之前,请听我说完。。。

我能够让我的功能在Windows 8+上运行,除了Chrome。我不能像在Windows7上那样在Chrome上使用窗口聚焦方法,因为它会弹出一条消息,要求我在商店里找到一个应用程序。

是否有任何方法(缺少扩展)来检测Chrome上Windows 8+中的自定义协议处理程序

更新:

使用onBlur检测它只适用于Windows 7,因为在8+中,如果它找不到可以打开你的协议的东西,它会打开"从应用商店中查找东西"对话框,这会使浏览器失去焦点。

嘿,我觉得你走对了路。这显然没有那么容易,但到目前为止,chrome并不是我的问题,更像Edge+IE,但我的解决方案是假设他们不支持协议,如果出现任何故障或没有正确响应,他们有时会这样做。

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

你可以在这里找到我的密码笔。希望这对你有所帮助,尽管现在给出答案有点晚了。这也适用于移动浏览器,但我还没有测试多个,还适用于我的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哪一个sorta有类似的方法,也许维护得更好,但由于某些原因,我无法让他们的解决方案发挥作用,但也许这正是你所需要的。

我认为这可能会对您使用不同的浏览器有所帮助,但请更好地定义您的问题,或者至少添加一些示例来澄清。