Created
October 21, 2022 09:52
-
-
Save fostyfost/0591c79f4cd7ca26e5941a53fd4bf1a4 to your computer and use it in GitHub Desktop.
Check `WebView` user agent for in-app cases
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
| // @see https://stackoverflow.com/questions/71459572/not-possible-to-detect-webview-with-javascript-and-user-agent-on-old-android-ver | |
| const rules = [ | |
| // If it says it's a webview, let's go with that. | |
| 'WebView', | |
| // iOS webview will be the same as safari but missing "Safari". | |
| '(iPhone|iPod|iPad)(?!.*Safari)', | |
| // https://developer.chrome.com/docs/multidevice/user-agent/#webview_user_agent | |
| 'Android.*Version/[0-9].[0-9]', | |
| // Also, we should save the wv detected for Lollipop. | |
| // Android Lollipop and Above: webview will be the same as native, | |
| // but it will contain "wv". | |
| 'Android.*wv', | |
| // Old chrome android webview agent | |
| 'Linux; U; Android', | |
| ] | |
| export const isInApp = () => { | |
| const userAgent: string | undefined = ( | |
| navigator.userAgent || navigator.vendor || (window as any).opera | |
| ) | |
| if (userAgent) { | |
| const regex = new RegExp(`(${rules.join('|')})`, 'ig') | |
| return !!userAgent.match(regex) | |
| } | |
| return false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment