Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 1997roylee/adff37cc82e78c72ee9a12e2b54d74ad to your computer and use it in GitHub Desktop.
Save 1997roylee/adff37cc82e78c72ee9a12e2b54d74ad to your computer and use it in GitHub Desktop.

Revisions

  1. @jamesdiacono jamesdiacono revised this gist Jun 2, 2020. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions deep-link-from-browser.js
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,16 @@ function DeepLinker(options) {
    didHide = false; // reset
    } else {
    // ignore duplicate focus event when returning from native app on
    // iOS Safari 13.3
    // iOS Safari 13.3+
    if (!hasFocus && options.onFallback) {
    options.onFallback();
    // wait for app switch transition to fully complete - only then is
    // 'visibilitychange' fired
    setTimeout(function() {
    // if browser was not hidden, the deep link failed
    if (!didHide) {
    options.onFallback();
    }
    }, 1000);
    }
    }

  2. @jamesdiacono jamesdiacono revised this gist Feb 20, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions deep-link-from-browser.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ function DeepLinker(options) {
    };

    // window is focused when dialogs are hidden, or browser comes into view
    function onFallback() {
    function onFocus() {
    if (didHide) {
    if (options.onReturn) {
    options.onReturn();
    @@ -43,7 +43,7 @@ function DeepLinker(options) {
    [
    [window, 'blur', onBlur],
    [document, 'visibilitychange', onVisibilityChange],
    [window, 'focus', onFallback],
    [window, 'focus', onFocus],
    ].forEach(function(conf) {
    conf[0][mode + 'EventListener'](conf[1], conf[2]);
    });
  3. @jamesdiacono jamesdiacono created this gist Feb 20, 2020.
    88 changes: 88 additions & 0 deletions deep-link-from-browser.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    function DeepLinker(options) {
    if (!options) {
    throw new Error('no options')
    }

    var hasFocus = true;
    var didHide = false;

    // window is blurred when dialogs are shown
    function onBlur() {
    hasFocus = false;
    };

    // document is hidden when native app is shown or browser is backgrounded
    function onVisibilityChange(e) {
    if (e.target.visibilityState === 'hidden') {
    didHide = true;
    }
    };

    // window is focused when dialogs are hidden, or browser comes into view
    function onFallback() {
    if (didHide) {
    if (options.onReturn) {
    options.onReturn();
    }

    didHide = false; // reset
    } else {
    // ignore duplicate focus event when returning from native app on
    // iOS Safari 13.3
    if (!hasFocus && options.onFallback) {
    options.onFallback();
    }
    }

    hasFocus = true;
    };

    // add/remove event listeners
    // `mode` can be "add" or "remove"
    function bindEvents(mode) {
    [
    [window, 'blur', onBlur],
    [document, 'visibilitychange', onVisibilityChange],
    [window, 'focus', onFallback],
    ].forEach(function(conf) {
    conf[0][mode + 'EventListener'](conf[1], conf[2]);
    });
    }

    // add event listeners
    bindEvents('add');

    // expose public API
    this.destroy = bindEvents.bind(null, 'remove');
    this.openURL = function(url) {
    // it can take a while for the dialog to appear
    var dialogTimeout = 500;

    setTimeout(function() {
    if (hasFocus && options.onIgnored) {
    options.onIgnored();
    }
    }, dialogTimeout);

    window.location = url;
    };
    }

    /* usage */

    var url = 'fb://profile/240995729348595';
    var badURL = 'lksadjgajsdhfaskd://slkdfs';

    var linker = new DeepLinker({
    onIgnored: function() {
    console.log('browser failed to respond to the deep link');
    },
    onFallback: function() {
    console.log('dialog hidden or user returned to tab');
    },
    onReturn: function() {
    console.log('user returned to the page from the native app');
    },
    });

    linker.openURL(url);