Last active
February 11, 2019 16:03
-
-
Save kouks/a2ad7eb48639591367507b9a9d6ae0ba to your computer and use it in GitHub Desktop.
Redirection based on popular browsers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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