JSFiddle - React, Tailwind, and code Playground

by Thiago Mata

HTML

<a class="uri-link" href="#" 
      data-uri-app="alert://" 
      data-url-app-not-found="http://www.google.com?q=not-found-link"
    >
      Alert Link 1
    </a>
<br/>
    <a class="uri-link" href="#" 
      data-uri-app="alert://" 
      data-url-app-not-found="http://www.google.com?q=not-found-link"
      data-url-app-found="http://www.google.com?q=found-link"
      >
      Alert Link 2
    </a>
<br/>
    <a class="uri-link"  href="#"
      data-uri-app="notexists://" 
      data-url-app-not-found="http://www.google.com?q=not-exists"
    >
      Not Exists Link
    </a>
<br/>
    <iframe id="go" style="display:none"></iframe>

JavaScript

/**
 * Try to open the external app using URL sheme
 * @param URL appUri URL Scheme of the app
 * @param URL urlNotFoundApp where should go if app not found (if exists)
 * @param URL urlFoundApp where should go if app found (if exists)
 */
function loadAppByUri(appUri,urlNotFoundApp,urlFoundApp) {
    
  var goToAppPage = null;
  var clearEvents;
  var setEvents;
  var appWasFound;
  var appWasNotFound;
  var waiting = true;
    
  /**
   * Cross browsers events top capture
   * the app openning
   */
  var eventsName = [ 'pagehide', 'blur' ];
    
  /**
   * If the app was founded
   * stop the timeout
   * go to urlFoundApp (if exists) 
   */
  appWasFound = function( event ){
      if( !waiting ) {
          return false;
      }
      waiting = false;
      clearEvents();
      if( urlFoundApp != null ) {
          alert( "app was found" );
          document.location = urlFoundApp;
      }
  }
    
  /**
   * If app was not founded
   * call the location urlNotFoundApp,urlFoundApp
   */
  appWasNotFound = function( event ){
      if( !waiting ) {
          return false;
      }
      waiting = false;
      clearEvents();
      console.log( "chaning location to " + urlNotFoundApp,urlFoundApp );
      if( urlNotFoundApp != null ) {
          alert( "appWasNotFound" );
          document.location = urlNotFoundApp;
      }
  };
    
  /**
   * Set all the events to detected the
   * app beeing called
   */
  setEvents = function() {
    window.clearTimeout( goToAppPage );
    $.each( eventsName , function ( key, eventName ) {
      console.log( "eventName = ", eventName );
      window.addEventListener( eventName, appWasFound );
    });
  }
    
  /**
   * Clear all the events what was waiting 
   * the app calling
   */
  clearEvents = function() {
      console.log("clear events");
      $.each( eventsName , function ( key, eventName ) {
      console.log("removeEventListener ", eventName );
      window.document.removeEventListener( eventName, appWasFound);
 ...