2019-12-26 23:26:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-02-06 05:30:31 +00:00
|
|
|
|
using System.Net;
|
2020-02-11 00:30:38 +00:00
|
|
|
|
using System.Net.Http;
|
2019-12-26 23:26:53 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CefSharp;
|
2020-02-06 05:30:31 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-12-26 23:26:53 +00:00
|
|
|
|
using Wabbajack.Lib.LibCefHelpers;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.WebAutomation
|
|
|
|
|
{
|
|
|
|
|
public class CefSharpWrapper : IWebDriver
|
|
|
|
|
{
|
2020-04-10 01:29:53 +00:00
|
|
|
|
private readonly IWebBrowser _browser;
|
|
|
|
|
public Action<Uri>? DownloadHandler { get; set; }
|
2019-12-26 23:26:53 +00:00
|
|
|
|
public CefSharpWrapper(IWebBrowser browser)
|
|
|
|
|
{
|
|
|
|
|
_browser = browser;
|
2020-04-28 22:46:50 +00:00
|
|
|
|
|
|
|
|
|
_browser.DownloadHandler = new DownloadHandler(this);
|
|
|
|
|
_browser.LifeSpanHandler = new PopupBlocker(this);
|
2019-12-26 23:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task NavigateTo(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
|
|
2020-04-10 01:29:53 +00:00
|
|
|
|
EventHandler<LoadingStateChangedEventArgs>? handler = null;
|
2019-12-26 23:26:53 +00:00
|
|
|
|
handler = (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (!e.IsLoading)
|
|
|
|
|
{
|
|
|
|
|
_browser.LoadingStateChanged -= handler;
|
|
|
|
|
tcs.SetResult(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
_browser.LoadingStateChanged += handler;
|
|
|
|
|
_browser.Load(uri.ToString());
|
2020-04-28 22:46:50 +00:00
|
|
|
|
|
2019-12-26 23:26:53 +00:00
|
|
|
|
return tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> EvaluateJavaScript(string text)
|
|
|
|
|
{
|
|
|
|
|
var result = await _browser.EvaluateScriptAsync(text);
|
|
|
|
|
if (!result.Success)
|
|
|
|
|
throw new Exception(result.Message);
|
|
|
|
|
|
|
|
|
|
return (string)result.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<Helpers.Cookie[]> GetCookies(string domainPrefix)
|
|
|
|
|
{
|
|
|
|
|
return Helpers.GetCookies(domainPrefix);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 05:30:31 +00:00
|
|
|
|
private const string CefStateName = "cef-state";
|
|
|
|
|
|
2019-12-26 23:26:53 +00:00
|
|
|
|
public async Task WaitForInitialized()
|
|
|
|
|
{
|
|
|
|
|
while (!_browser.IsBrowserInitialized)
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
}
|
2020-02-11 00:30:38 +00:00
|
|
|
|
|
|
|
|
|
public string Location => _browser.Address;
|
2019-12-26 23:26:53 +00:00
|
|
|
|
}
|
2020-02-06 05:30:31 +00:00
|
|
|
|
|
2020-02-16 01:53:57 +00:00
|
|
|
|
public class PopupBlocker : ILifeSpanHandler
|
|
|
|
|
{
|
2020-04-10 01:29:53 +00:00
|
|
|
|
private readonly CefSharpWrapper _wrapper;
|
2020-02-16 01:53:57 +00:00
|
|
|
|
|
|
|
|
|
public PopupBlocker(CefSharpWrapper cefSharpWrapper)
|
|
|
|
|
{
|
|
|
|
|
_wrapper = cefSharpWrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl,
|
|
|
|
|
string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures,
|
2020-04-10 01:29:53 +00:00
|
|
|
|
IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser? newBrowser)
|
2020-02-16 01:53:57 +00:00
|
|
|
|
{
|
|
|
|
|
// Block popups
|
2020-05-04 22:49:24 +00:00
|
|
|
|
newBrowser = null;
|
2020-02-16 01:53:57 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
|
|
|
|
|
{
|
2020-04-28 22:46:50 +00:00
|
|
|
|
return false;
|
2020-02-16 01:53:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 05:30:31 +00:00
|
|
|
|
public class DownloadHandler : IDownloadHandler
|
|
|
|
|
{
|
|
|
|
|
private CefSharpWrapper _wrapper;
|
|
|
|
|
|
|
|
|
|
public DownloadHandler(CefSharpWrapper wrapper)
|
|
|
|
|
{
|
|
|
|
|
_wrapper = wrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem,
|
|
|
|
|
IBeforeDownloadCallback callback)
|
|
|
|
|
{
|
2020-04-10 01:29:53 +00:00
|
|
|
|
_wrapper.DownloadHandler?.Invoke(new Uri(downloadItem.Url));
|
2020-02-06 05:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem,
|
|
|
|
|
IDownloadItemCallback callback)
|
|
|
|
|
{
|
|
|
|
|
callback.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-26 23:26:53 +00:00
|
|
|
|
}
|