[CHANGE] Cleanup cookie import
This commit is contained in:
@@ -8,14 +8,6 @@
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<MudStack Spacing="2">
|
||||
@if (_showCookieTextImport)
|
||||
{
|
||||
<MudTextField @bind-Value="@_cookieDomain" Required HelperText="Domain"/>
|
||||
<MudTextField AutoGrow @bind-Value="@_cookieText" HelperText="Cookie text" Placeholder="EXAMPLE: Cookie1=Value1; Cookie2=Value2;"/>
|
||||
|
||||
<MudButton Variant="Variant.Outlined" Disabled="@(string.IsNullOrWhiteSpace(_cookieDomain) || string.IsNullOrWhiteSpace(_cookieText))" OnClick="ApplyTextCookies">Apply</MudButton>
|
||||
}
|
||||
|
||||
<MudStack Row Spacing="2" AlignItems="AlignItems.Start" Justify="Justify.SpaceEvenly" StretchItems="StretchItems.All">
|
||||
<MudStack Spacing="2">
|
||||
<MudTextField Label="UserAgent" Required @bind-Value="@Client.UserAgent"/>
|
||||
@@ -63,19 +55,21 @@
|
||||
}
|
||||
</MudStack>
|
||||
|
||||
<MudPaper Elevation="0" Outlined Class="pa-2">
|
||||
<MudText>Import cookies</MudText>
|
||||
<MudForm @bind-IsValid="@_cookieTextValid" Disabled="@(Client.CookieContainer.Count != 0)">
|
||||
<MudTextField @bind-Value="@_cookieDomain" Immediate Required Label="Domain" RequiredError="Domain is required."/>
|
||||
<MudTextField Class="my-2" Lines="4" AutoGrow @bind-Value="@_cookieText" Immediate Required Label="Cookies" Placeholder="EXAMPLE: Cookie1=Value1; Cookie2=Value2;"
|
||||
Validation="@(new Func<string, string?>(ValidateCookieText))"/>
|
||||
<MudButton Variant="Variant.Outlined" Disabled="@(!_cookieTextValid)" OnClick="ParseCookies">Load</MudButton>
|
||||
</MudForm>
|
||||
</MudPaper>
|
||||
|
||||
<MudDataGrid Items="Client.CookieContainer.GetAllCookies()" Dense Elevation="0" Outlined>
|
||||
<Header>
|
||||
<MudStack Class="ma-2">
|
||||
<MudText>Cookies</MudText>
|
||||
</MudStack>
|
||||
<MudStack Row Spacing="2" Class="ma-1">
|
||||
<MudTooltip Text="Add cookie">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Add" Size="Size.Small" Color="Color.Success" Disabled="_showCookieTextImport" OnClick="AddCookie"/>
|
||||
</MudTooltip>
|
||||
<MudTooltip Text="Add from text">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.InsertDriveFile" Size="Size.Small" Color="Color.Primary" OnClick="ToggleCookieTextImport"/>
|
||||
</MudTooltip>
|
||||
</MudStack>
|
||||
</Header>
|
||||
<Columns>
|
||||
<TemplateColumn Title="Name">
|
||||
|
@@ -14,7 +14,7 @@ namespace Manager.App.Components.Dialogs
|
||||
|
||||
private bool _isLoading;
|
||||
|
||||
private bool _showCookieTextImport;
|
||||
private bool _cookieTextValid;
|
||||
private string _cookieText = "";
|
||||
private string _cookieDomain = ".youtube.com";
|
||||
|
||||
@@ -23,26 +23,38 @@ namespace Manager.App.Components.Dialogs
|
||||
Client.UserAgent = DefaultUserAgent;
|
||||
base.OnInitialized();
|
||||
}
|
||||
|
||||
private void AddCookie()
|
||||
{
|
||||
Client.CookieContainer.Add(new Cookie { Name = "SET_NAME", Domain = ".youtube.com" });
|
||||
}
|
||||
|
||||
private void ToggleCookieTextImport()
|
||||
private void ParseCookies()
|
||||
{
|
||||
_showCookieTextImport =! _showCookieTextImport;
|
||||
}
|
||||
|
||||
private void ApplyTextCookies()
|
||||
{
|
||||
_showCookieTextImport = false;
|
||||
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
|
||||
Client.CookieContainer.Add(cookies);
|
||||
_cookieText = string.Empty;
|
||||
try
|
||||
{
|
||||
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
|
||||
Client.CookieContainer.Add(cookies);
|
||||
_cookieText = string.Empty;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SnackbarService.Add($"Parsing cookies failed: {e.Message}", Severity.Error);
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
|
||||
private static string? ValidateCookieText(string text)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
return "Cookies are required";
|
||||
|
||||
var pairs = text.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var pair in pairs)
|
||||
{
|
||||
if (!pair.Contains('=')) return "Invalid.";
|
||||
var key = pair[..pair.IndexOf('=')].Trim();
|
||||
if (string.IsNullOrEmpty(key)) return "Invalid.";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static CookieCollection ParseCookieHeader(string cookieHeader, string domain = "")
|
||||
{
|
||||
var collection = new CookieCollection();
|
||||
@@ -60,13 +72,15 @@ namespace Manager.App.Components.Dialogs
|
||||
var name = parts[0].Trim();
|
||||
var value = parts[1].Trim();
|
||||
|
||||
var cookie = new Cookie(name, value);
|
||||
var cookie = new Cookie(name, value)
|
||||
{
|
||||
Expires = DateTime.Now.AddDays(1),
|
||||
Path = "/",
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(domain))
|
||||
cookie.Domain = domain;
|
||||
cookie.Expires = DateTime.Now.AddDays(1);
|
||||
cookie.Path = "/";
|
||||
|
||||
|
||||
collection.Add(cookie);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user