Skip to content

Instantly share code, notes, and snippets.

@kouks
Last active February 11, 2019 16:03
Show Gist options
  • Select an option

  • Save kouks/a2ad7eb48639591367507b9a9d6ae0ba to your computer and use it in GitHub Desktop.

Select an option

Save kouks/a2ad7eb48639591367507b9a9d6ae0ba to your computer and use it in GitHub Desktop.
Redirection based on popular browsers
const redirect = (urls) => {
const browser = window.chrome ? 'chrome' : window.safari ? 'safari' : 'firefox'
const platform = /(chrome|safari|firefox)/i.test(navigator.userAgent) ? 'supported' : 'unsupported'
const os = /(android|ios|iphone)/i.test(navigator.userAgent) ? 'mobile' : 'desktop'
const parametrize = (url) => {
url = new URL(url)
Array.from(new URL(location.href).searchParams.entries()).forEach(([k, v]) => url.searchParams.set(k, v))
return url.toString()
}
if (platform === 'supported' && os === 'desktop') {
return parametrize(urls[browser])
}
return parametrize(urls[os])
}
/**
* Usage:
*
* redirect({
* chrome: 'chrome-webstore-url',
* firefox: 'firefox-webstore-url',
* safari: 'safari-webstore-url',
* mobile: 'mobile-fallback-url',
* desktop: 'desktop-fallback-url',
* })
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment