From 45edbfe2434430ce709550d3875dfd315cafe665 Mon Sep 17 00:00:00 2001 From: Duncan Hsieh Date: Mon, 1 Jun 2020 14:31:41 +0800 Subject: [PATCH] Improve user experience Assume that the browser triggers the visibilityChange event when the user successfully opens deeplink. When the user successfully opens deeplink, this script can cancel the opening of the app store. --- lib/public/script.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/public/script.js b/lib/public/script.js index ac75f4f..01bb58d 100644 --- a/lib/public/script.js +++ b/lib/public/script.js @@ -7,6 +7,19 @@ function deepLink(options) { 'https://market.android.com/details?id=' + androidPackageName; var ua = window.navigator.userAgent; + var hiddenProp, visibilityChange; + var timeoutId; + if (typeof document.hidden !== "undefined") { + hiddenProp = "hidden"; + visibilityChange = "visibilitychange"; + } else if (typeof document.msHidden !== "undefined") { + hiddenProp = "msHidden"; + visibilityChange = "msvisibilitychange"; + } else if (typeof document.webkitHidden !== "undefined") { + hiddenProp = "webkitHidden"; + visibilityChange = "webkitvisibilitychange"; + } + // split the first :// from the url string var split = url.split(/:\/\/(.+)/); var scheme = split[0]; @@ -45,11 +58,21 @@ function deepLink(options) { window.location = urls.fallback; } + function handleVisibilityChange() { + window.clearTimeout(timeoutId); + document.removeEventListener(visibilityChange, handleVisibilityChange, false); + } + function launchWekitApproach(url, fallback) { + if (typeof document.addEventListener !== "undefined" || hiddenProp !== undefined) { + document.addEventListener(visibilityChange, handleVisibilityChange, false); + } + document.location = url; - setTimeout(function() { + + timeoutId = setTimeout(function() { document.location = fallback; - }, 250); + }, 2500); } function launchIframeApproach(url, fallback) {