@@ -11,7 +11,35 @@ import kotlin.coroutines.resume
1111import kotlinx.coroutines.suspendCancellableCoroutine
1212
1313object WebViewUtil {
14- const val MINIMUM_WEBVIEW_VERSION = 114
14+ private const val CHROME_PACKAGE = " com.android.chrome"
15+ private const val SYSTEM_SETTINGS_PACKAGE = " com.android.settings"
16+
17+ const val MINIMUM_WEBVIEW_VERSION = 118
18+
19+ /* *
20+ * Uses the WebView's user agent string to create something similar to what Chrome on Android
21+ * would return.
22+ *
23+ * Example of WebView user agent string:
24+ * Mozilla/5.0 (Linux; Android 13; Pixel 7 Build/TQ3A.230901.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36
25+ *
26+ * Example of Chrome on Android:
27+ * Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.3
28+ */
29+ fun getInferredUserAgent (context : Context ): String {
30+ return WebView (context)
31+ .getDefaultUserAgentString()
32+ .replace(" ; Android .*?\\ )" .toRegex(), " ; Android 10; K)" )
33+ .replace(" Version/.* Chrome/" .toRegex(), " Chrome/" )
34+ }
35+
36+ fun getVersion (context : Context ): String {
37+ val webView = WebView .getCurrentWebViewPackage() ? : return " how did you get here?"
38+ val pm = context.packageManager
39+ val label = webView.applicationInfo!! .loadLabel(pm)
40+ val version = webView.versionName
41+ return " $label $version "
42+ }
1543
1644 fun supportsWebView (context : Context ): Boolean {
1745 try {
@@ -25,6 +53,16 @@ object WebViewUtil {
2553
2654 return context.packageManager.hasSystemFeature(PackageManager .FEATURE_WEBVIEW )
2755 }
56+
57+ fun spoofedPackageName (context : Context ): String {
58+ return try {
59+ context.packageManager.getPackageInfo(CHROME_PACKAGE , PackageManager .GET_META_DATA )
60+
61+ CHROME_PACKAGE
62+ } catch (_: PackageManager .NameNotFoundException ) {
63+ SYSTEM_SETTINGS_PACKAGE
64+ }
65+ }
2866}
2967
3068fun WebView.isOutdated (): Boolean {
@@ -43,6 +81,8 @@ fun WebView.setDefaultSettings() {
4381 displayZoomControls = false
4482 cacheMode = WebSettings .LOAD_DEFAULT
4583 }
84+
85+ CookieManager .getInstance().acceptThirdPartyCookies(this )
4686}
4787
4888private fun WebView.getWebViewMajorVersion (): Int {
0 commit comments