Skip to content

Commit 01fac40

Browse files
committed
window.open('', '_blank')
1 parent 68df1f1 commit 01fac40

File tree

2 files changed

+70
-69
lines changed

2 files changed

+70
-69
lines changed

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,7 @@ public async Task<IBrowserContext> InitContext(string ctxId, BrowserActionArgs a
108108
return _contexts[ctxId];
109109
}
110110

111-
public async Task<IPage> NewPage(MessageInfo message,
112-
bool enableResponseCallback = false,
113-
bool responseInMemory = false,
114-
List<WebPageResponseData>? responseContainer = null,
115-
string[]? excludeResponseUrls = null,
116-
string[]? includeResponseUrls = null)
111+
public async Task<IPage> NewPage(MessageInfo message, PageActionArgs args)
117112
{
118113
var context = await GetContext(message.ContextId);
119114
var page = await context.NewPageAsync();
@@ -123,68 +118,73 @@ public async Task<IPage> NewPage(MessageInfo message,
123118
var js = @"Object.defineProperties(navigator, {webdriver:{get:()=>false}});";
124119
await page.AddInitScriptAsync(js);
125120

126-
if (!enableResponseCallback)
121+
if (!args.EnableResponseCallback)
127122
{
128123
return page;
129124
}
130125

131126
page.Response += async (sender, e) =>
132127
{
133-
if (e.Status != 204 &&
134-
e.Headers.ContainsKey("content-type") &&
135-
(e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr") &&
136-
(excludeResponseUrls == null || !excludeResponseUrls.Any(url => e.Url.ToLower().Contains(url))) &&
137-
(includeResponseUrls == null || includeResponseUrls.Any(url => e.Url.ToLower().Contains(url))))
138-
{
139-
Serilog.Log.Information($"{e.Request.Method}: {e.Url}");
140-
141-
try
142-
{
143-
var result = new WebPageResponseData
144-
{
145-
Url = e.Url.ToLower(),
146-
PostData = e.Request?.PostData ?? string.Empty,
147-
ResponseInMemory = responseInMemory
148-
};
128+
await HandleFetchResponse(e, message, args);
129+
};
149130

150-
if (e.Headers["content-type"].Contains("application/json"))
151-
{
152-
if (e.Status == 200 && e.Ok)
153-
{
154-
var json = await e.JsonAsync();
155-
result.ResponseData = JsonSerializer.Serialize(json);
156-
}
157-
}
158-
else
159-
{
160-
var html = await e.TextAsync();
161-
result.ResponseData = html;
162-
}
131+
return page;
132+
}
163133

164-
if (responseContainer != null && responseInMemory)
165-
{
166-
responseContainer.Add(result);
167-
}
134+
public async Task HandleFetchResponse(IResponse response, MessageInfo message, PageActionArgs args)
135+
{
136+
if (response.Status != 204 &&
137+
response.Headers.ContainsKey("content-type") &&
138+
(response.Request.ResourceType == "fetch" || response.Request.ResourceType == "xhr") &&
139+
(args.ExcludeResponseUrls == null || !args.ExcludeResponseUrls.Any(url => response.Url.ToLower().Contains(url))) &&
140+
(args.IncludeResponseUrls == null || args.IncludeResponseUrls.Any(url => response.Url.ToLower().Contains(url))))
141+
{
142+
Serilog.Log.Information($"{response.Request.Method}: {response.Url}");
143+
144+
try
145+
{
146+
var result = new WebPageResponseData
147+
{
148+
Url = response.Url.ToLower(),
149+
PostData = response.Request?.PostData ?? string.Empty,
150+
ResponseInMemory = args.ResponseInMemory
151+
};
168152

169-
Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}");
170-
var webPageResponseHooks = _services.GetServices<IWebPageResponseHook>();
171-
foreach (var hook in webPageResponseHooks)
153+
if (response.Headers["content-type"].Contains("application/json"))
154+
{
155+
if (response.Status == 200 && response.Ok)
172156
{
173-
hook.OnDataFetched(message, result);
157+
var json = await response.JsonAsync();
158+
result.ResponseData = JsonSerializer.Serialize(json);
174159
}
175160
}
176-
catch (ObjectDisposedException ex)
161+
else
177162
{
178-
Serilog.Log.Information(ex.Message);
163+
var html = await response.TextAsync();
164+
result.ResponseData = html;
179165
}
180-
catch (Exception ex)
166+
167+
if (args.ResponseContainer != null && args.ResponseInMemory)
181168
{
182-
Serilog.Log.Error($"{e.Url}\r\n" + ex.ToString());
169+
args.ResponseContainer.Add(result);
183170
}
184-
}
185-
};
186171

187-
return page;
172+
Serilog.Log.Warning($"Response status: {response.Status} {response.StatusText}, OK: {response.Ok}");
173+
var webPageResponseHooks = _services.GetServices<IWebPageResponseHook>();
174+
foreach (var hook in webPageResponseHooks)
175+
{
176+
hook.OnDataFetched(message, result);
177+
}
178+
}
179+
catch (ObjectDisposedException ex)
180+
{
181+
Serilog.Log.Information(ex.Message);
182+
}
183+
catch (Exception ex)
184+
{
185+
Serilog.Log.Error($"{response.Url}\r\n" + ex.ToString());
186+
}
187+
}
188188
}
189189

190190
/// <summary>

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,40 @@ public async Task<BrowserActionResult> GoToPage(MessageInfo message, PageActionA
99
try
1010
{
1111
IPage? page = null;
12-
if (!args.OpenNewTab && !args.EnableResponseCallback)
12+
if (!args.OpenNewTab)
1313
{
1414
page = _instance.Contexts[message.ContextId].Pages.LastOrDefault();
15+
16+
if (page != null)
17+
{
18+
await page.EvaluateAsync(@"() => {
19+
window.open('', '_blank');
20+
}");
21+
22+
if (args.EnableResponseCallback)
23+
{
24+
page.Response += async (sender, e) =>
25+
{
26+
await _instance.HandleFetchResponse(e, message, args);
27+
};
28+
}
29+
}
1530
}
1631
else
1732
{
18-
page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
19-
responseInMemory: args.ResponseInMemory,
20-
responseContainer: args.ResponseContainer,
21-
excludeResponseUrls: args.ExcludeResponseUrls,
22-
includeResponseUrls: args.IncludeResponseUrls);
33+
page = await _instance.NewPage(message, args);
2334

2435
Serilog.Log.Information($"goto page: {args.Url}");
2536

2637
if (args.OpenNewTab && page != null && page.Url == "about:blank")
2738
{
28-
page = await _instance.NewPage(message,
29-
enableResponseCallback: args.EnableResponseCallback,
30-
responseInMemory: args.ResponseInMemory,
31-
responseContainer: args.ResponseContainer,
32-
excludeResponseUrls: args.ExcludeResponseUrls,
33-
includeResponseUrls: args.IncludeResponseUrls);
39+
page = await _instance.NewPage(message, args);
3440
}
3541
}
3642

3743
if (page == null)
3844
{
39-
page = await _instance.NewPage(message,
40-
enableResponseCallback: args.EnableResponseCallback,
41-
responseInMemory: args.ResponseInMemory,
42-
responseContainer: args.ResponseContainer,
43-
excludeResponseUrls: args.ExcludeResponseUrls,
44-
includeResponseUrls: args.IncludeResponseUrls);
45+
page = await _instance.NewPage(message, args);
4546
}
4647

4748
// Active current tab

0 commit comments

Comments
 (0)