Add UserAgent setting

This commit is contained in:
Stone_Red
2024-04-27 20:11:40 +02:00
parent aa1484f5d4
commit f53eb36dd0
4 changed files with 19 additions and 1 deletions
+3
View File
@@ -1,6 +1,7 @@
using Markdowser.Utilities; using Markdowser.Utilities;
using System.IO; using System.IO;
using System.Reflection;
using System.Text.Json; using System.Text.Json;
namespace Markdowser.Models; namespace Markdowser.Models;
@@ -15,6 +16,8 @@ public class Settings
public string? HomeUrl { get; set; } public string? HomeUrl { get; set; }
public string UserAgent { get; set; } = $"{Assembly.GetExecutingAssembly().GetName().Name}/{Assembly.GetExecutingAssembly().GetName().Version?.ToString()}";
public static void SaveSettings() public static void SaveSettings()
{ {
if (!Directory.Exists(Configuration.ApplicationDataPath)) if (!Directory.Exists(Configuration.ApplicationDataPath))
@@ -161,7 +161,6 @@ public partial class MainWindowViewModel : ViewModelBase
public MainWindowViewModel() public MainWindowViewModel()
{ {
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd($"{nameof(Markdowser)}/{Assembly.GetExecutingAssembly().GetName().Version?.ToString()}");
httpClient.Timeout = TimeSpan.FromSeconds(10); httpClient.Timeout = TimeSpan.FromSeconds(10);
content = DefaultContent; content = DefaultContent;
@@ -232,6 +231,13 @@ public partial class MainWindowViewModel : ViewModelBase
} }
} }
httpClient.DefaultRequestHeaders.UserAgent.Clear();
if (!httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd(Settings.Current.UserAgent))
{
WindowNotificationManager.Show(new Notification("Invalid User Agent", "Failed to set user agent.", NotificationType.Error));
return;
}
IsBusy = true; IsBusy = true;
Progress = 0; Progress = 0;
@@ -16,6 +16,12 @@ public class SettingsViewModel : ViewModelBase
set => Settings.Current.SearchEngineUrl = value; set => Settings.Current.SearchEngineUrl = value;
} }
public string UserAgent
{
get => Settings.Current.UserAgent;
set => Settings.Current.UserAgent = value;
}
public bool DarkMode public bool DarkMode
{ {
get => Settings.Current.DarkMode; get => Settings.Current.DarkMode;
+3
View File
@@ -28,5 +28,8 @@
<TextBlock Text="Home URL" /> <TextBlock Text="Home URL" />
<TextBox Text="{Binding HomeUrl}" Watermark="URL, or leave empty to use default" /> <TextBox Text="{Binding HomeUrl}" Watermark="URL, or leave empty to use default" />
<TextBlock Text="User Agent" />
<TextBox Text="{Binding UserAgent}" Watermark="User agent string" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>