atomantic / is-ua-webview

📱tiny/simple npm module for detecting webview status of a user-agent
ISC License
58 stars 7 forks source link

Should return false for Chrome on Android #11

Closed steveluscher closed 1 year ago

steveluscher commented 1 year ago

Unfortunately returns true for the following UA:

Mozilla/5.0 (Linux; Android 12; ingot) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Mobile Safari/537.36

…because of this rule:

  // Android Lollipop and Above: webview will be the same as native but it will contain "wv"
  // Android KitKat to lollipop webview will put {version}.0.0.0
  'Android.*(wv|.0.0.0)',

It's accidentally matching the Chrome/106.0.0.0 section.

edulecca commented 1 year ago

workaround to bypass ua: may it helps for someone struggling with regex


    const isWebview = isUAWebview(ua);
    const byPassAndroidChrome = navigator.userAgent.match(new RegExp('Android.*106.0.0.0'));

    const getIsWebView = () => {
        if (byPassAndroidChrome) return false;
        return isWebview;
    }
steveluscher commented 1 year ago

From https://developer.chrome.com/docs/multidevice/user-agent/#webview-on-android:

WebView UA in KitKat to Lollipop

Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/_BuildID_) 
AppleWebKit/537.36 (KHTML, like Gecko) 
Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36

If you're attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/X.X string in the WebView user-agent string. Don't rely on the specific Chrome version number (for example, 30.0.0.0) as the version numbers changes with each release.