2019-10-24 01:22:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2019-12-27 00:41:33 +00:00
|
|
|
|
using CefSharp;
|
|
|
|
|
using CefSharp.OffScreen;
|
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
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
_browser = new ChromiumWebBrowser();
|
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 00:41:33 +00:00
|
|
|
|
public async Task<Uri> NavigateTo(Uri uri)
|
2019-10-24 01:22:11 +00:00
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
await _driver.NavigateTo(uri);
|
|
|
|
|
return await GetLocation();
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 00:41:33 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2019-12-27 00:41:33 +00:00
|
|
|
|
_browser.Dispose();
|
2019-10-24 01:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|