[CHANGE] Add account dialog
This commit is contained in:
117
Manager.App/Components/Dialogs/AccountDialog.razor.cs
Normal file
117
Manager.App/Components/Dialogs/AccountDialog.razor.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System.Net;
|
||||
using Manager.YouTube;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using MudBlazor;
|
||||
|
||||
namespace Manager.App.Components.Dialogs
|
||||
{
|
||||
public partial class AccountDialog : ComponentBase
|
||||
{
|
||||
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
|
||||
[Parameter] public string DefaultUserAgent { get; set; } = "";
|
||||
|
||||
public YouTubeClient Client { get; set; } = new();
|
||||
|
||||
private bool _isLoading;
|
||||
|
||||
private bool _showCookieTextImport;
|
||||
private string _cookieText = "";
|
||||
private string _cookieDomain = ".youtube.com";
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Client.UserAgent = DefaultUserAgent;
|
||||
base.OnInitialized();
|
||||
}
|
||||
|
||||
private void AddCookie()
|
||||
{
|
||||
Client.CookieContainer.Add(new Cookie { Name = "SET_NAME", Domain = ".youtube.com" });
|
||||
}
|
||||
|
||||
private async Task RemoveCookie(Cookie? cookie)
|
||||
{
|
||||
if (cookie == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cookie.Expired = true;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private void ToggleCookieTextImport()
|
||||
{
|
||||
_showCookieTextImport =! _showCookieTextImport;
|
||||
}
|
||||
|
||||
private void ApplyTextCookies()
|
||||
{
|
||||
_showCookieTextImport = false;
|
||||
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
|
||||
Client.CookieContainer.Add(cookies);
|
||||
_cookieText = string.Empty;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
public static CookieCollection ParseCookieHeader(string cookieHeader, string domain = "")
|
||||
{
|
||||
var collection = new CookieCollection();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(cookieHeader))
|
||||
return collection;
|
||||
|
||||
var cookies = cookieHeader.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var cookieStr in cookies)
|
||||
{
|
||||
var parts = cookieStr.Split('=', 2);
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
var name = parts[0].Trim();
|
||||
var value = parts[1].Trim();
|
||||
|
||||
// Escape invalid characters
|
||||
var safeName = Uri.EscapeDataString(name);
|
||||
var safeValue = Uri.EscapeDataString(value);
|
||||
|
||||
var cookie = new Cookie(safeName, safeValue);
|
||||
|
||||
if (!string.IsNullOrEmpty(domain))
|
||||
cookie.Domain = domain;
|
||||
|
||||
collection.Add(cookie);
|
||||
}
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
private bool CanValidate()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Client.UserAgent) || Client.CookieContainer.Count <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool CanSave()
|
||||
{
|
||||
return Client.ClientState is { LoggedIn: true };
|
||||
}
|
||||
|
||||
private async Task ValidateAccount()
|
||||
{
|
||||
_isLoading = true;
|
||||
await Client.GetStateAsync();
|
||||
_isLoading = false;
|
||||
}
|
||||
|
||||
private void OnSave()
|
||||
{
|
||||
MudDialog?.Close(DialogResult.Ok(Client));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user