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

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

问题描述

我正在尝试检测我处理自定义协议的应用程序是否已安装并使用不同的浏览器工作.我查看了该网站上的其他问题,例如:如何检测浏览器的协议处理程序?,并查看了类似的资源,使其能够在大多数浏览器的大多数平台上运行.

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...

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

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.

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

更新:

使用 onBlur 来检测它仅适用于 Windows 7,因为在 8+ 中,如果它没有找到可以打开您的协议的内容,它会打开从应用商店查找内容"对话框,这会使浏览器失去焦点.

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.

推荐答案

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

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.

模糊/焦点是需要检查的,但您需要结合可见性更改来检查它.HTML5 Visiblity API 和这篇文章 关于它 帮助我找到了一个非常可靠的解决方案,除了上面提到的浏览器,因为它们在 navigator.msLaunchUri 函数并实现了自己的方法,不依赖于模糊/焦点.但是该功能有问题,并且始终无法正确响应.

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.

您可以在这里找到我的 codepen.希望这对您有所帮助,即使答案有点晚了.这也适用于移动浏览器,但我没有测试多个尚未适用于我的 Android 6.0.2.从长远来看可能需要一些调整,但我认为它非常可靠.

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

我的代码使用 Win10 测试:Chrome、Firefox、IE11、Edge + Android:Chrome (6.0.1)

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

还有这个github项目https://github.com/ismailhabib/custom-protocol-detection which sorta 有类似的方法,可能维护得更多一些,但由于某些原因我无法让他们的解决方案正常工作,但也许这正是你所需要的.

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天全站免登陆