Skip to content

Commit

Permalink
Fix get property
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Nov 14, 2023
1 parent e03f130 commit 589fb05
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
5 changes: 1 addition & 4 deletions lib/PuppeteerSharp/Binding.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.PageCoverage;

namespace PuppeteerSharp
{
Expand Down Expand Up @@ -85,7 +82,7 @@ internal async Task RunAsync(
if (taskResult.GetType().IsGenericType)
{
// the task is already awaited and therefore the call to property Result will not deadlock
result = taskResult.GetType().GetProperty(taskResultPropertyName).GetValue(taskResult);
result = taskResult.GetType().GetProperty(taskResultPropertyName)?.GetValue(taskResult);
}
}

Expand Down
19 changes: 6 additions & 13 deletions lib/PuppeteerSharp/IsolatedWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Helpers.Json;
using PuppeteerSharp.Input;
using PuppeteerSharp.Messaging;

namespace PuppeteerSharp
Expand Down Expand Up @@ -43,8 +41,6 @@ public IsolatedWorld(

internal Frame Frame { get; }

internal WebWorker Worker { get; }

internal CDPSession Client => Frame?.Client ?? Worker?.Client;

internal bool HasContext => _contextResolveTaskWrapper?.Task.IsCompleted == true;
Expand All @@ -53,6 +49,8 @@ public IsolatedWorld(

internal override IEnvironment Environment => (IEnvironment)Frame ?? Worker;

private WebWorker Worker { get; }

public void Dispose() => _bindingQueue.Dispose();

public ValueTask DisposeAsync() => _bindingQueue.DisposeAsync();
Expand Down Expand Up @@ -91,11 +89,8 @@ await context.Client.SendAsync(
{
return;
}
else
{
_logger.LogError(ex.ToString());
return;
}

_logger.LogError(ex.ToString());
}
}).ConfigureAwait(false);
}
Expand All @@ -114,7 +109,7 @@ internal override async Task<IElementHandle> AdoptBackendNodeAsync(object backen

internal override async Task<IJSHandle> TransferHandleAsync(IJSHandle handle)
{
if ((handle as JSHandle).Realm == this)
if ((handle as JSHandle)?.Realm == this)
{
return handle;
}
Expand All @@ -133,9 +128,7 @@ internal override async Task<IJSHandle> TransferHandleAsync(IJSHandle handle)

internal override async Task<IJSHandle> AdoptHandleAsync(IJSHandle handle)
{
var executionContext = await GetExecutionContextAsync().ConfigureAwait(false);

if ((handle as JSHandle).Realm == this)
if ((handle as JSHandle)?.Realm == this)
{
return handle;
}
Expand Down
23 changes: 7 additions & 16 deletions lib/PuppeteerSharp/JSHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class JSHandle : IJSHandle
{
internal JSHandle(IsolatedWorld world, RemoteObject remoteObject)
{
Logger = Client.Connection.LoggerFactory.CreateLogger(GetType());
Realm = world;
RemoteObject = remoteObject;
Logger = Client.Connection.LoggerFactory.CreateLogger(GetType());
}

/// <inheritdoc/>
Expand All @@ -29,7 +29,7 @@ internal JSHandle(IsolatedWorld world, RemoteObject remoteObject)
/// <inheritdoc/>
public RemoteObject RemoteObject { get; }

internal CDPSession Client { get; }
internal CDPSession Client => Realm.Environment.Client;

internal Func<Task> DisposeAction { get; set; }

Expand All @@ -46,20 +46,11 @@ internal JSHandle(IsolatedWorld world, RemoteObject remoteObject)

/// <inheritdoc/>
public Task<IJSHandle> GetPropertyAsync(string propertyName)
=> BindIsolatedHandleAsync(async handle =>
{
var objectHandle = await handle.EvaluateFunctionHandleAsync(
@"(object, propertyName) => {
const result = { __proto__: null};
result[propertyName] = object[propertyName];
return result;
}",
propertyName).ConfigureAwait(false);
var properties = await objectHandle.GetPropertiesAsync().ConfigureAwait(false);
properties.TryGetValue(propertyName, out var result);
await objectHandle.DisposeAsync().ConfigureAwait(false);
return result;
});
=> EvaluateFunctionHandleAsync(
@"(object, propertyName) => {
return object[propertyName];
}",
propertyName);

/// <inheritdoc/>
public Task<Dictionary<string, IJSHandle>> GetPropertiesAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using PuppeteerSharp.QueryHandlers;

namespace PuppeteerSharp.QueryHandlers
{
Expand All @@ -15,8 +14,6 @@ internal class CustomQuerySelectorRegistry
private readonly Regex _customQueryHandlerNameRegex = new("[a-zA-Z]+$", RegexOptions.Compiled);
private readonly QueryHandler _defaultHandler = new CssQueryHandler();

public Browser Browser { get; private set; }

internal Dictionary<string, QueryHandler> InternalQueryHandlers => new()
{
["aria"] = new AriaQueryHandler(),
Expand Down

0 comments on commit 589fb05

Please sign in to comment.