Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Existing PR Improvements #1

Merged
merged 1 commit into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/deeplink.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function (options) {
options.title = options.title || '';
options.label = options.label || '';
options.timeout = options.timeout || 250;
options.usePathOnFallback = options.usePathOnFallback || false;

const deeplink = function (req, res, next) {
const opts = {};
Expand Down
19 changes: 18 additions & 1 deletion lib/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
<div>
<h2>{{label}}</h2>
<div style="justify-content: center">
<img src="./images/app_store.svg" height="40" onclick="onClickIOS();" />
<img
style="display: none"
id="ios_button"
src="./images/app_store.svg"
height="40"
onclick="onClickIOS();"
/>
<img
style="display: none"
id="android_button"
src="./images/play_store.svg"
height="40"
onclick="onClickAndroid();"
Expand All @@ -30,6 +38,15 @@ <h2>{{label}}</h2>
label: '{{label}}',
};

if (options.ios_store_link) {
document.getElementById('ios_button').style.display = 'inline-block';
}

if (options.android_package_name) {
document.getElementById('android_button').style.display =
'inline-block';
}

deepLink(options);
function onClickIOS() {
var ios_url = options.ios_store_link;
Expand Down
60 changes: 51 additions & 9 deletions lib/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@ function deepLink(options) {
var iosStoreLink = options.ios_store_link;
var androidPackageName = options.android_package_name;
var timeout = options.timeout || 250;
var usePathOnFallback = options.usePathOnFallback || false;
var playStoreLink =
'https://play.google.com/store/apps/details?id=' + androidPackageName;
var ua = window.navigator.userAgent;

var timeoutId;

const hiddenProp = () => {
const properties = {};
if (typeof document.hidden !== 'undefined') {
properties.name = 'hidden';
properties.event = 'visibilitychange';
} else if (typeof document.msHidden !== 'undefined') {
properties.name = 'msHidden';
properties.event = 'msvisibilitychange';
} else if (typeof document.webkitHidden !== 'undefined') {
properties.name = 'webkitHidden';
properties.event = 'webkitvisibilitychange';
}

return properties;
};

// split the first :// from the url string
var split = url.split(/:\/\/(.+)/);
var scheme = split[0];
Expand All @@ -25,15 +44,20 @@ function deepLink(options) {
androidPackageName +
';end;',
playStoreLink: playStoreLink,
fallback: fallback,
fallback: usePathOnFallback ? fallback + '/' + path : fallback,
};

var isMobile = {
android: function () {
return /Android/i.test(ua);
},
ios: function () {
return /iPhone|iPad|iPod/i.test(ua);
const isMobile = /iPhone|iPad|iPod/i.test(ua);
const isM1IPad =
navigator.maxTouchPoints &&
navigator.maxTouchPoints > 2 &&
/Macintosh/.test(ua);
return isMobile || isM1IPad;
},
};

Expand All @@ -46,10 +70,28 @@ function deepLink(options) {
window.location = urls.fallback;
}

function launchWebkitApproach(url, fallback, time = 250) {
document.location = url;
setTimeout(function () {
document.location = fallback;
function handleVisibilityChange() {
window.clearTimeout(timeoutId);
document.removeEventListener(
hiddenProp().event,
handleVisibilityChange,
false
);
}

function launchWebkitApproach(url, fallback, time = 3000) {
const hidden = hiddenProp();
if (
typeof document.addEventListener !== 'undefined' ||
hidden.name !== undefined
) {
document.addEventListener(hidden.event, handleVisibilityChange, false);
}

document.location.replace(url);

timeoutId = setTimeout(function () {
document.location.replace(fallback);
}, time);
}

Expand All @@ -59,15 +101,15 @@ function deepLink(options) {
iframe.style.width = '1px';
iframe.style.height = '1px';
iframe.onload = function () {
document.location = url;
document.location.replace(url);
};
iframe.src = url;

window.onload = function () {
document.body.appendChild(iframe);

setTimeout(function () {
window.location = fallback;
window.location.replace(fallback);
}, time);
};
}
Expand Down Expand Up @@ -95,7 +137,7 @@ function deepLink(options) {

function androidLaunch() {
if (ua.match(/Chrome/)) {
document.location = urls.android_intent;
document.location.replace(urls.android_intent);
} else if (ua.match(/Firefox/)) {
launchWebkitApproach(
urls.deepLink,
Expand Down