[CHANGE] Cleanup cookie import
This commit is contained in:
@@ -8,14 +8,6 @@
|
|||||||
</TitleContent>
|
</TitleContent>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<MudStack Spacing="2">
|
<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 Row Spacing="2" AlignItems="AlignItems.Start" Justify="Justify.SpaceEvenly" StretchItems="StretchItems.All">
|
||||||
<MudStack Spacing="2">
|
<MudStack Spacing="2">
|
||||||
<MudTextField Label="UserAgent" Required @bind-Value="@Client.UserAgent"/>
|
<MudTextField Label="UserAgent" Required @bind-Value="@Client.UserAgent"/>
|
||||||
@@ -63,19 +55,21 @@
|
|||||||
}
|
}
|
||||||
</MudStack>
|
</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>
|
<MudDataGrid Items="Client.CookieContainer.GetAllCookies()" Dense Elevation="0" Outlined>
|
||||||
<Header>
|
<Header>
|
||||||
<MudStack Class="ma-2">
|
<MudStack Class="ma-2">
|
||||||
<MudText>Cookies</MudText>
|
<MudText>Cookies</MudText>
|
||||||
</MudStack>
|
</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>
|
</Header>
|
||||||
<Columns>
|
<Columns>
|
||||||
<TemplateColumn Title="Name">
|
<TemplateColumn Title="Name">
|
||||||
|
@@ -14,7 +14,7 @@ namespace Manager.App.Components.Dialogs
|
|||||||
|
|
||||||
private bool _isLoading;
|
private bool _isLoading;
|
||||||
|
|
||||||
private bool _showCookieTextImport;
|
private bool _cookieTextValid;
|
||||||
private string _cookieText = "";
|
private string _cookieText = "";
|
||||||
private string _cookieDomain = ".youtube.com";
|
private string _cookieDomain = ".youtube.com";
|
||||||
|
|
||||||
@@ -23,26 +23,38 @@ namespace Manager.App.Components.Dialogs
|
|||||||
Client.UserAgent = DefaultUserAgent;
|
Client.UserAgent = DefaultUserAgent;
|
||||||
base.OnInitialized();
|
base.OnInitialized();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddCookie()
|
|
||||||
{
|
|
||||||
Client.CookieContainer.Add(new Cookie { Name = "SET_NAME", Domain = ".youtube.com" });
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ToggleCookieTextImport()
|
private void ParseCookies()
|
||||||
{
|
{
|
||||||
_showCookieTextImport =! _showCookieTextImport;
|
try
|
||||||
}
|
{
|
||||||
|
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
|
||||||
private void ApplyTextCookies()
|
Client.CookieContainer.Add(cookies);
|
||||||
{
|
_cookieText = string.Empty;
|
||||||
_showCookieTextImport = false;
|
}
|
||||||
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
|
catch (Exception e)
|
||||||
Client.CookieContainer.Add(cookies);
|
{
|
||||||
_cookieText = string.Empty;
|
SnackbarService.Add($"Parsing cookies failed: {e.Message}", Severity.Error);
|
||||||
|
}
|
||||||
StateHasChanged();
|
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 = "")
|
public static CookieCollection ParseCookieHeader(string cookieHeader, string domain = "")
|
||||||
{
|
{
|
||||||
var collection = new CookieCollection();
|
var collection = new CookieCollection();
|
||||||
@@ -60,13 +72,15 @@ namespace Manager.App.Components.Dialogs
|
|||||||
var name = parts[0].Trim();
|
var name = parts[0].Trim();
|
||||||
var value = parts[1].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))
|
if (!string.IsNullOrEmpty(domain))
|
||||||
cookie.Domain = domain;
|
cookie.Domain = domain;
|
||||||
cookie.Expires = DateTime.Now.AddDays(1);
|
|
||||||
cookie.Path = "/";
|
|
||||||
|
|
||||||
collection.Add(cookie);
|
collection.Add(cookie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user