2019-10-24 01:22:11 +00:00
|
|
|
|
using System;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
using System.Linq;
|
2019-10-24 01:22:11 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2019-12-27 00:41:33 +00:00
|
|
|
|
using CefSharp;
|
|
|
|
|
using CefSharp.OffScreen;
|
2021-01-29 04:02:26 +00:00
|
|
|
|
using HtmlAgilityPack;
|
2020-12-29 23:15:47 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-05-29 22:51:41 +00:00
|
|
|
|
using Wabbajack.Lib.LibCefHelpers;
|
2019-10-24 01:22:11 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.WebAutomation
|
|
|
|
|
{
|
|
|
|
|
public class Driver : IDisposable
|
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
private IWebBrowser _browser;
|
|
|
|
|
private CefSharpWrapper _driver;
|
2019-10-24 01:22:11 +00:00
|
|
|
|
|
2019-12-27 00:41:33 +00:00
|
|
|
|
public Driver()
|
2019-10-24 01:22:11 +00:00
|
|
|
|
{
|
2020-05-04 21:02:26 +00:00
|
|
|
|
|
2019-12-27 00:41:33 +00:00
|
|
|
|
_browser = new ChromiumWebBrowser();
|
2020-05-04 20:33:45 +00:00
|
|
|
|
|
2019-12-27 00:41:33 +00:00
|
|
|
|
_driver = new CefSharpWrapper(_browser);
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
public static async Task<Driver> Create()
|
|
|
|
|
{
|
|
|
|
|
var driver = new Driver();
|
2019-12-27 00:41:33 +00:00
|
|
|
|
await driver._driver.WaitForInitialized();
|
2019-10-24 01:22:11 +00:00
|
|
|
|
return driver;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public async Task<Uri?> NavigateTo(Uri uri, CancellationToken? token = null)
|
2019-10-24 01:22:11 +00:00
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _driver.NavigateTo(uri, token);
|
|
|
|
|
return await GetLocation();
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException ex)
|
|
|
|
|
{
|
|
|
|
|
await DumpState(uri, ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
private async Task DumpState(Uri uri, Exception ex)
|
2020-12-29 23:15:47 +00:00
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
var file = AbsolutePath.EntryPoint.Combine("CEFStates", DateTime.UtcNow.ToFileTimeUtc().ToString())
|
|
|
|
|
.WithExtension(new Extension(".html"));
|
|
|
|
|
file.Parent.CreateDirectory();
|
|
|
|
|
var source = await GetSourceAsync();
|
|
|
|
|
var cookies = await Helpers.GetCookies();
|
|
|
|
|
var cookiesString = string.Join('\n', cookies.Select(c => c.Name + " - " + c.Value));
|
|
|
|
|
await file.WriteAllTextAsync(uri + "\n " + source + "\n" + ex + "\n" + cookiesString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<long> NavigateToAndDownload(Uri uri, AbsolutePath absolutePath, bool quickMode = false, CancellationToken? token = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await _driver.NavigateToAndDownload(uri, absolutePath, quickMode: quickMode, token: token);
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException ex) {
|
|
|
|
|
await DumpState(uri, ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2020-12-29 23:15:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 01:29:53 +00:00
|
|
|
|
public async ValueTask<Uri?> GetLocation()
|
2019-10-24 01:22:11 +00:00
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return new Uri(_browser.Address);
|
|
|
|
|
}
|
|
|
|
|
catch (UriFormatException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
2020-12-29 23:15:47 +00:00
|
|
|
|
|
|
|
|
|
public async ValueTask<string> GetSourceAsync()
|
|
|
|
|
{
|
|
|
|
|
return await _browser.GetSourceAsync();
|
|
|
|
|
}
|
2020-05-28 22:31:01 +00:00
|
|
|
|
|
2021-01-29 04:02:26 +00:00
|
|
|
|
public async ValueTask<HtmlDocument> GetHtmlAsync()
|
|
|
|
|
{
|
|
|
|
|
var body = await GetSourceAsync();
|
|
|
|
|
var doc = new HtmlDocument();
|
|
|
|
|
doc.LoadHtml(body);
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 22:31:01 +00:00
|
|
|
|
public Action<Uri?> DownloadHandler {
|
|
|
|
|
set => _driver.DownloadHandler = value;
|
|
|
|
|
}
|
2019-10-24 01:22:11 +00:00
|
|
|
|
|
|
|
|
|
public Task<string> GetAttr(string selector, string attr)
|
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
return _driver.EvaluateJavaScript($"document.querySelector(\"{selector}\").{attr}");
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 22:31:01 +00:00
|
|
|
|
public Task<string> EvalJavascript(string js)
|
|
|
|
|
{
|
|
|
|
|
return _driver.EvaluateJavaScript(js);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:22:11 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
_browser.Dispose();
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
2020-05-29 22:51:41 +00:00
|
|
|
|
|
|
|
|
|
public static void ClearCache()
|
|
|
|
|
{
|
|
|
|
|
Helpers.ClearCookies();
|
|
|
|
|
}
|
2020-12-31 06:44:42 +00:00
|
|
|
|
|
|
|
|
|
public async Task DeleteCookiesWhere(Func<Helpers.Cookie, bool> filter)
|
|
|
|
|
{
|
|
|
|
|
await Helpers.DeleteCookiesWhere(filter);
|
|
|
|
|
}
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|