public async Task RegisterAsync(string name, SelectorsRegisterOptions options = default) { options ??= new SelectorsRegisterOptions(); var script = ScriptsHelper.EvaluationScript(options?.Script, options?.Path); await _channel.RegisterAsync(name, script, options?.ContentScript).ConfigureAwait(false); }
internal async Task CallAsync(Delegate binding) { try { const string taskResultPropertyName = "Result"; var methodParams = binding.Method.GetParameters().Select(parameter => parameter.ParameterType).Skip(1).ToArray(); var args = new List <object> { new BindingSource { Context = _initializer?.Frame?.Page?.Context, Page = _initializer?.Frame?.Page, Frame = _initializer?.Frame, }, }; if (methodParams.Length == 1 && methodParams[0] == typeof(IJSHandle)) { args.Add(_initializer.Handle); } else { for (int i = 0; i < methodParams.Length; i++) { args.Add(ScriptsHelper.ParseEvaluateResult(_initializer.Args[i], methodParams[i])); } } object result = binding.DynamicInvoke(args.ToArray()); if (result is Task taskResult) { result = null; await taskResult.ConfigureAwait(false); var taskResultType = taskResult.GetType(); if (taskResultType.IsGenericType && taskResultType.GenericTypeArguments[0] != VoidTaskResultType) { // the task is already awaited and therefore the call to property Result will not deadlock result = taskResult.GetType().GetProperty(taskResultPropertyName).GetValue(taskResult); } } await _channel.ResolveAsync(ScriptsHelper.SerializedArgument(result)).ConfigureAwait(false); } catch (TargetInvocationException ex) { await _channel.RejectAsync(ex.InnerException).ConfigureAwait(false); } catch (Exception ex) { await _channel.RejectAsync(ex).ConfigureAwait(false); } }
public async Task <IJSHandle> EvaluateHandleAsync(string expression, object arg = null) => await _channel.EvaluateExpressionHandleAsync( expression, null, ScriptsHelper.SerializedArgument(arg)) .ConfigureAwait(false);
public async Task <T> EvaluateAsync <T>(string expression, object arg = null) => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvaluateExpressionAsync( expression, null, ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));
public Task DispatchEventAsync(string type, object eventInit = null) => _channel.DispatchEventAsync( type, eventInit = ScriptsHelper.SerializedArgument(eventInit));
public async Task <T> EvalOnSelectorAllAsync <T>(string selector, string expression, object arg = null) => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvalOnSelectorAllAsync( selector: selector, script: expression, arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));
public async Task <T> JsonValueAsync <T>() => ScriptsHelper.ParseEvaluateResult <T>(await _channel.JsonValueAsync().ConfigureAwait(false));
public async Task <JsonElement?> EvaluateAsync(string expression, object arg = null) => ScriptsHelper.ParseEvaluateResult <JsonElement?>(await _channel.EvaluateExpressionAsync( script: expression, arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));