[CHANGE] Cleanup cookie import
This commit is contained in:
@@ -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