hrms-checkin/src/utils/forceExternalBrowser.ts
waruneeauy 354f528651
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m9s
fixed: force redirect open browser chrome / safari
2026-06-05 16:45:35 +07:00

57 lines
1.6 KiB
TypeScript

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()