title: I need to manipulate focus shifting
body: <p>This is the scenario I want:</p>
  
  <p>I'm checking for the presence of an application (using protocol detection), if it's not present the exe file automatically starts downloading. The webpage continuously checks in regular interval whether the app has been successfully downloaded and when the installation is complete, the browser prompts to open the app.</p>
  
  <p><strong>What is working:</strong> </p>
  
  <p>The download is starting automatically. The webpage is checking for the presence of the app in regular interval.</p>
  
  <p><strong>What is NOT working:</strong></p>
  
  <p>After the exe is downloaded, when I start the download, the focus shifts and the webpage thinks that the app has been launched, whereas the app actually hasn't yet been installed even.</p>
  
  <p>I need a focus checking mechanism which detects when the specific app has been launched and focus shifts and not respond to any other focus shifts.</p>
  
  <p>This is my code:</p>
  
  <pre><code>_registerEvent = function(target, eventType, cb) {
              if (target.addEventListener) {
                  target.addEventListener(eventType, cb);
                  return {
                      remove: function() {
                          target.removeEventListener(eventType, cb);
                      }
                  };
              } else {
                  target.attachEvent(eventType, cb);
                  return {
                      remove: function() {
                          target.detachEvent(eventType, cb);
                      }
                  };
              }
          },
   checkIfHasProtocol = function($el) {
  
                  openUriWithTimeoutHack(url);
  
  
          },
   openUriWithTimeoutHack = function(protocol) {
                  var timeout = setTimeout(function() {
                  handler.remove();
                  checkIfHasProtocol(protocol);
  
              }, 5000);
  
   var target = window;
              while (target != target.parent) {
                  target = target.parent;
              }
  
              var handler = _registerEvent(window, "blur", onBlur);
  
              function onBlur() {
  
  
                      if (tabVisible() &amp;&amp; isBlurred) {
                          clearTimeout(timeout);
                          handler.remove();
                          //App has been launched    
  
  
                      }
                  }
  
              }
  
              window.location = protocol;
          };
  
  tabVisible(function() {
  
          if (tabVisible() &amp;&amp; inCheck) {
  
  
              inCheck = false;
          }
  
  });
  
  window.addEventListener("blur", function() {
  
      isBlurred = true;
  
  });
  
  window.addEventListener("focus", function() {
  
      isBlurred = false;
  
      inCheck = false;
  
  
  });
  </code></pre>