Added support for User-Agent Client Hints (2)

This commit is contained in:
M66B 2022-06-04 12:23:38 +02:00
parent f5e294eeaf
commit 95a3932f8b
1 changed files with 28 additions and 7 deletions

View File

@ -628,13 +628,34 @@ public class ConnectionHelper {
static void setUserAgent(Context context, HttpURLConnection connection) { static void setUserAgent(Context context, HttpURLConnection connection) {
connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context));
// https://web.dev/migrate-to-ua-ch/ if (BuildConfig.DEBUG) {
// https://web.dev/migrate-to-ua-ch/
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean generic_ua = prefs.getBoolean("generic_ua", false);
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA
connection.setRequestProperty("Sec-CH-UA", "\"Chromium\""); connection.setRequestProperty("Sec-CH-UA", "\"Chromium\""); // No WebView API yet
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile
connection.setRequestProperty("Sec-CH-UA-Mobile", "?1"); connection.setRequestProperty("Sec-CH-UA-Mobile", "?1");
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform
connection.setRequestProperty("Sec-CH-UA-Platform", "Android"); connection.setRequestProperty("Sec-CH-UA-Platform", "\"Android\"");
if (!generic_ua) {
String release = Build.VERSION.RELEASE;
if (release == null)
release = "";
release = release.replace("\"", "'");
String model = Build.MODEL;
if (model == null)
model = "";
model = model.replace("\"", "'");
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform-Version
connection.setRequestProperty("Sec-CH-UA-Platform-Version", "\"" + release + "\"");
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Model
connection.setRequestProperty("Sec-CH-UA-Model", "\"" + model + "\"");
}
}
} }
} }