wabbajack/Wabbajack.App/Extensions/WebViewExtensions.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CefNet;
using CefNet.Avalonia;
using HtmlAgilityPack;
using Wabbajack.DTOs.Logins;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.App.Extensions;
public static class WebViewExtensions
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static async Task WaitForReady(this WebView view)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
while (view.BrowserObject == null) await Task.Delay(200);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
/// <summary>
/// Navigates to the URL and waits until the page is finished loading
/// </summary>
/// <param name="view"></param>
/// <param name="uri"></param>
public static async Task NavigateTo(this WebView view, Uri uri)
{
view.Navigate(uri.ToString());
while (view.IsBusy) await Task.Delay(200);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static async Task<Cookie[]> Cookies(this WebView view, string domainEnding, CancellationToken token)
{
var results = CefCookieManager.GetGlobalManager(null)!;
var cookies = await results.GetCookiesAsync(c => c.Domain.EndsWith(domainEnding), token)!;
return cookies.Select(c => new Cookie
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
Domain = c.Domain,
Name = c.Name,
Path = c.Path,
Value = c.Value
}).ToArray();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static async Task EvaluateJavaScript(this WebView view, string js)
{
view.GetMainFrame().ExecuteJavaScript(js, "", 0);
}
public static async Task<HtmlDocument> GetDom(this WebView view, CancellationToken token)
{
var source = await view.GetMainFrame().GetSourceAsync(token);
var doc = new HtmlDocument();
doc.LoadHtml(source);
return doc;
2021-09-27 12:42:46 +00:00
}
}