OnDataFetched

This commit is contained in:
Haiping Chen 2024-06-21 13:23:18 -05:00
parent 2601c0c1dc
commit a97aa8aee1
5 changed files with 53 additions and 6 deletions

View file

@ -31,4 +31,5 @@ public class ElementLocatingArgs
/// Draw outline around the element
/// </summary>
public bool Highlight { get; set; }
public string HighlightColor { get; set; } = "red";
}

View file

@ -11,4 +11,11 @@ public class PageActionArgs
public string Url { get; set; } = null!;
public bool OpenNewTab { get; set; } = false;
/// <summary>
/// On page data fetched
/// </summary>
public DataFetched? OnDataFetched { get; set; }
}
public delegate void DataFetched(string url, string data);

View file

@ -71,7 +71,17 @@ public class PlaywrightInstance : IDisposable
Serilog.Log.Information($"Page is closed: {e.Url}");
};
Serilog.Log.Information($"New page is created: {page.Url}");
await page.SetViewportSizeAsync(1280, 800);
await page.SetViewportSizeAsync(1600, 900);
/*page.Response += async (sender, e) =>
{
Serilog.Log.Information($"Response: {e.Url}");
if (e.Headers.ContainsKey("content-type") && e.Headers["content-type"].Contains("application/json"))
{
var json = await e.JsonAsync();
Serilog.Log.Information(json.ToString());
}
};*/
};
_contexts[ctxId].Close += async (sender, e) =>
@ -84,10 +94,26 @@ public class PlaywrightInstance : IDisposable
return _contexts[ctxId];
}
public async Task<IPage> NewPage(string ctxId)
public async Task<IPage> NewPage(string ctxId, DataFetched? fetched)
{
await InitContext(ctxId);
var page = await _contexts[ctxId].NewPageAsync();
if (fetched != null)
{
page.Response += async (sender, e) =>
{
if (e.Headers.ContainsKey("content-type") &&
e.Headers["content-type"].Contains("application/json") &&
e.Request.ResourceType == "fetch")
{
Serilog.Log.Information($"Response: {e.Url}");
var json = await e.JsonAsync();
fetched(e.Url.ToLower(), JsonSerializer.Serialize(json));
}
};
}
return page;
}

View file

@ -1,3 +1,5 @@
using System.Linq;
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
@ -9,7 +11,7 @@ public partial class PlaywrightWebDriver
try
{
// Check if the page is already open
if (!args.OpenNewTab && context.Pages.Count > 0)
/*if (!args.OpenNewTab && context.Pages.Count > 0)
{
foreach (var p in context.Pages)
{
@ -22,10 +24,11 @@ public partial class PlaywrightWebDriver
return result;
}
}
}
}*/
var page = args.OpenNewTab ? await _instance.NewPage(message.ContextId) :
var page = args.OpenNewTab ? await _instance.NewPage(message.ContextId, fetched: args.OnDataFetched) :
_instance.GetPage(message.ContextId);
var response = await page.GotoAsync(args.Url);
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
@ -50,4 +53,14 @@ public partial class PlaywrightWebDriver
return result;
}
private void Page_Response1(object sender, IResponse e)
{
throw new NotImplementedException();
}
private void Page_Response(object sender, IResponse e)
{
throw new NotImplementedException();
}
}

View file

@ -130,7 +130,7 @@ public partial class PlaywrightWebDriver
await page.EvaluateAsync($@"
(element) => {{
element.style.outline = '2px solid red';
element.style.outline = '2px solid {location.HighlightColor}';
}}", handle);
result.IsHighlighted = true;