fixed: force redirect open browser chrome / safari
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m9s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m9s
This commit is contained in:
parent
5c05df26cf
commit
354f528651
3 changed files with 176 additions and 1 deletions
57
src/utils/forceExternalBrowser.ts
Normal file
57
src/utils/forceExternalBrowser.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
const EXTERNAL_BROWSER_QUERY_KEY = 'openExternalBrowser'
|
||||
|
||||
const isLineBrowser = (userAgent: string) => /Line/i.test(userAgent)
|
||||
|
||||
const isAndroid = (userAgent: string) => /Android/i.test(userAgent)
|
||||
|
||||
const isIOS = (userAgent: string) => /iPhone|iPad|iPod/i.test(userAgent)
|
||||
|
||||
const hasExternalRedirectFlag = () => {
|
||||
const params = new URLSearchParams(window.location.search)
|
||||
return params.get(EXTERNAL_BROWSER_QUERY_KEY) === '1'
|
||||
}
|
||||
|
||||
const buildIOSUrlWithFlag = () => {
|
||||
const url = new URL(window.location.href)
|
||||
url.searchParams.set(EXTERNAL_BROWSER_QUERY_KEY, '1')
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
const buildUrlWithoutFlag = () => {
|
||||
const url = new URL(window.location.href)
|
||||
url.searchParams.delete(EXTERNAL_BROWSER_QUERY_KEY)
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
const buildAndroidIntentUrl = () => {
|
||||
const currentUrl = window.location.href
|
||||
const strippedUrl = currentUrl.replace(/^https?:\/\//i, '')
|
||||
return `intent://${strippedUrl}#Intent;scheme=https;package=com.android.chrome;end`
|
||||
}
|
||||
|
||||
export const forceOpenInExternalBrowser = () => {
|
||||
const userAgent = navigator.userAgent || ''
|
||||
|
||||
// Prevent redirect loops and only run in LINE browser.
|
||||
if (!isLineBrowser(userAgent) || hasExternalRedirectFlag()) {
|
||||
return
|
||||
}
|
||||
|
||||
if (isAndroid(userAgent)) {
|
||||
window.location.replace(buildAndroidIntentUrl())
|
||||
return
|
||||
}
|
||||
|
||||
if (isIOS(userAgent)) {
|
||||
window.location.replace(buildIOSUrlWithFlag())
|
||||
}
|
||||
}
|
||||
|
||||
export const shouldShowIOSLineFallback = () => {
|
||||
const userAgent = navigator.userAgent || ''
|
||||
return (
|
||||
isLineBrowser(userAgent) && isIOS(userAgent) && hasExternalRedirectFlag()
|
||||
)
|
||||
}
|
||||
|
||||
export const getExternalBrowserUrl = () => buildUrlWithoutFlag()
|
||||
Loading…
Add table
Add a link
Reference in a new issue