Skip to content

Commit 3fff9a0

Browse files
authored
Don't emit an internal error when eval causes navigation (#621)
1 parent 7a8bd93 commit 3fff9a0

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

lib/PuppeteerSharp/FrameManager.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,20 @@ internal FrameManager(CDPSession client, FrameTree frameTree, Page page)
3939

4040
#region Public Methods
4141

42-
internal JSHandle CreateJSHandle(int contextId, dynamic remoteObject)
42+
internal JSHandle CreateJSHandle(ExecutionContext context, dynamic remoteObject)
43+
=> remoteObject.subtype == "node"
44+
? new ElementHandle(context, _client, remoteObject, _page, this)
45+
: new JSHandle(context, _client, remoteObject);
46+
47+
internal ExecutionContext ExecutionContextById(int contextId)
4348
{
44-
_contextIdToContext.TryGetValue(contextId, out var storedContext);
49+
_contextIdToContext.TryGetValue(contextId, out var context);
4550

46-
if (storedContext == null)
51+
if (context == null)
4752
{
4853
_logger.LogError("INTERNAL ERROR: missing context with id = {ContextId}", contextId);
4954
}
50-
51-
if (remoteObject.subtype == "node")
52-
{
53-
return new ElementHandle(storedContext, _client, remoteObject, _page, this);
54-
}
55-
56-
return new JSHandle(storedContext, _client, remoteObject);
55+
return context;
5756
}
5857

5958
#endregion
@@ -150,7 +149,7 @@ private void OnExecutionContextCreated(ContextPayload contextPayload)
150149
var context = new ExecutionContext(
151150
_client,
152151
contextPayload,
153-
(ctx, remoteObject) => CreateJSHandle(contextPayload.Id, remoteObject),
152+
(ctx, remoteObject) => CreateJSHandle(ctx, remoteObject),
154153
frame);
155154

156155
_contextIdToContext[contextPayload.Id] = context;

lib/PuppeteerSharp/Page.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,8 +1941,8 @@ private void OnDialog(PageJavascriptDialogOpeningResponse message)
19411941

19421942
private async Task OnConsoleAPI(PageConsoleResponse message)
19431943
{
1944-
var values = message.Args.Select<dynamic, JSHandle>(i =>
1945-
_frameManager.CreateJSHandle(message.ExecutionContextId, i)).ToArray();
1944+
var ctx = _frameManager.ExecutionContextById(message.ExecutionContextId);
1945+
var values = message.Args.Select<dynamic, JSHandle>(i => _frameManager.CreateJSHandle(ctx, i)).ToArray();
19461946
await AddConsoleMessage(message.Type, values);
19471947
}
19481948

0 commit comments

Comments
 (0)