WebBrowsingSettings
This commit is contained in:
parent
7cfe1a1c01
commit
5604273d1d
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Browsing.Settings;
|
||||
|
||||
public class WebBrowsingSettings
|
||||
{
|
||||
public string Driver { get; set; } = "Playwright";
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
|
|
@ -11,8 +11,14 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.41.2" />
|
||||
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
|
||||
<Compile Remove="packages\**" />
|
||||
<EmbeddedResource Remove="packages\**" />
|
||||
<None Remove="packages\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.43.0" />
|
||||
<PackageReference Include="Selenium.WebDriver" Version="4.20.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -33,11 +33,15 @@ public class SeleniumInstance : IDisposable
|
|||
|
||||
string tempFolderPath = $"{Path.GetTempPath()}\\_selenium\\{id}";
|
||||
|
||||
var options = new ChromeOptions();
|
||||
var options = new ChromeOptions
|
||||
{
|
||||
// DebuggerAddress = "localhost:9222",
|
||||
// BrowserVersion = "123.0.6312.46"
|
||||
};
|
||||
options.AddArgument("disable-infobars");
|
||||
options.AddArgument($"--user-data-dir={tempFolderPath}");
|
||||
var selenium = new ChromeDriver(options);
|
||||
selenium.Manage().Window.Maximize();
|
||||
// selenium.Manage().Window.Maximize();
|
||||
selenium.Navigate().GoToUrl("about:blank");
|
||||
_contexts[id] = selenium;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
namespace BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver;
|
||||
|
||||
public partial class SeleniumWebDriver
|
||||
{
|
||||
public async Task<T> EvaluateScript<T>(string contextId, string script)
|
||||
{
|
||||
await _instance.Wait(contextId);
|
||||
var driver = await _instance.InitContext(contextId);
|
||||
var jsExecutor = (IJavaScriptExecutor)driver;
|
||||
var result = jsExecutor.ExecuteAsyncScript(script);
|
||||
return (T)result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
using System.Net.Http;
|
||||
|
||||
namespace BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver;
|
||||
|
||||
public partial class SeleniumWebDriver
|
||||
{
|
||||
public async Task<BrowserActionResult> SendHttpRequest(string contextId, HttpRequestParams args)
|
||||
{
|
||||
var result = new BrowserActionResult();
|
||||
|
||||
var body = args.Method == HttpMethod.Post ?
|
||||
$"body: '{args.Payload}'" : string.Empty;
|
||||
|
||||
// Send AJAX request
|
||||
string script = $@"
|
||||
(async () => {{
|
||||
const response = await fetch('{args.Url}', {{
|
||||
method: '{args.Method}',
|
||||
headers: {{
|
||||
'Content-Type': 'application/json'
|
||||
}},
|
||||
{body}
|
||||
}});
|
||||
return await response.json();
|
||||
}})();
|
||||
";
|
||||
|
||||
try
|
||||
{
|
||||
var response = await EvaluateScript<object>(contextId, script);
|
||||
result.IsSuccess = true;
|
||||
result.Body = JsonSerializer.Serialize(response);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Message = ex.Message;
|
||||
result.StackTrace = ex.StackTrace;
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -57,11 +57,6 @@ public partial class SeleniumWebDriver : IWebBrowser
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<T> EvaluateScript<T>(string contextId, string script)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<string> ExtractData(BrowserActionParams actionParams)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
|
@ -86,9 +81,4 @@ public partial class SeleniumWebDriver : IWebBrowser
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<BrowserActionResult> SendHttpRequest(string contextId, HttpRequestParams actionParams)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Browsing.Settings;
|
||||
using BotSharp.Abstraction.Settings;
|
||||
using BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||
using BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver;
|
||||
using BotSharp.Plugin.WebDriver.Hooks;
|
||||
|
|
@ -14,12 +16,28 @@ public class WebDriverPlugin : IBotSharpPlugin
|
|||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
// services.AddScoped<IWebBrowser, PlaywrightWebDriver>();
|
||||
// services.AddSingleton<PlaywrightInstance>();
|
||||
var settings = new WebBrowsingSettings();
|
||||
config.Bind("WebBrowsing", settings);
|
||||
|
||||
services.AddScoped<IWebBrowser, SeleniumWebDriver>();
|
||||
services.AddScoped(provider =>
|
||||
{
|
||||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
return settings;
|
||||
});
|
||||
|
||||
services.AddScoped<PlaywrightWebDriver>();
|
||||
services.AddSingleton<PlaywrightInstance>();
|
||||
|
||||
services.AddScoped<SeleniumWebDriver>();
|
||||
services.AddSingleton<SeleniumInstance>();
|
||||
|
||||
services.AddScoped<IWebBrowser>(provider => settings.Driver switch
|
||||
{
|
||||
"Playwright" => provider.GetRequiredService<PlaywrightWebDriver>(),
|
||||
"Selenium" => provider.GetRequiredService<SeleniumWebDriver>(),
|
||||
_ => provider.GetRequiredService<PlaywrightWebDriver>(),
|
||||
});
|
||||
|
||||
services.AddScoped<WebDriverService>();
|
||||
services.AddScoped<IConversationHook, WebDriverConversationHook>();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -133,6 +133,10 @@
|
|||
}
|
||||
},
|
||||
|
||||
"WebBrowsing": {
|
||||
"Driver": "Playwright"
|
||||
},
|
||||
|
||||
"Statistics": {
|
||||
"DataDir": "stats"
|
||||
},
|
||||
|
|
@ -224,7 +228,7 @@
|
|||
"ApiKey": "",
|
||||
"Map": {
|
||||
"Endpoint": "https://maps.googleapis.com/maps/api/geocode/json",
|
||||
"Components": "country=US|country=CA",
|
||||
"Components": "country=US|country=CA"
|
||||
},
|
||||
"Youtube": {
|
||||
"Endpoint": "https://www.googleapis.com/youtube/v3/search",
|
||||
|
|
|
|||
Loading…
Reference in a new issue