Ejemplo n.º 1
0
 internal async Task <T> EvalOnSelectorAllAsync <T>(bool isPageCall, string selector, string script, object args)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvalOnSelectorAllAsync(
                                              selector: selector,
                                              script: script,
                                              isFunction: script.IsJavascriptFunction(),
                                              arg: ScriptsHelper.SerializedArgument(args),
                                              isPage: isPageCall).ConfigureAwait(false));
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public async Task RegisterAsync(string name, string script, string scriptPath, bool?contentScript = null)
        {
            script = ScriptsHelper.EvaluationScript(script, scriptPath, false);

            var registerParam = new SelectorsRegisterParams
            {
                Name          = name,
                Source        = script,
                ContentScript = contentScript,
            };

            var tasks = new List <Task>();

            foreach (var channel in _channels)
            {
                tasks.Add(channel.RegisterAsync(registerParam));
            }

            try
            {
                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            catch (Exception ex) when(ex.Message.Contains("Connection closed"))
            {
                // Ignore connection closed exceptions.
            }

            _registrations.Add(registerParam);
        }
Ejemplo n.º 3
0
 internal async Task <JsonElement?> EvalOnSelectorAllAsync(bool isPageCall, string selector, string script)
 => ScriptsHelper.ParseEvaluateResult <JsonElement?>(await _channel.EvalOnSelectorAllAsync(
                                                         selector: selector,
                                                         script: script,
                                                         isFunction: script.IsJavascriptFunction(),
                                                         arg: EvaluateArgument.Undefined,
                                                         isPage: isPageCall).ConfigureAwait(false));
Ejemplo n.º 4
0
 internal async Task <T> EvaluateAsync <T>(bool isPageCall, string script, object args)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvaluateExpressionAsync(
                                              script: script,
                                              isFunction: script.IsJavascriptFunction(),
                                              arg: args,
                                              isPage: isPageCall,
                                              serializeArgument: true).ConfigureAwait(false));
Ejemplo n.º 5
0
 internal Task DispatchEventAsync(bool isPageCall, string selector, string type, object eventInit, float?timeout)
 => _channel.DispatchEventAsync(
     selector,
     type,
     eventInit == null ? EvaluateArgument.Undefined : ScriptsHelper.SerializedArgument(eventInit),
     timeout,
     isPageCall);
Ejemplo n.º 6
0
 internal async Task <IJSHandle> WaitForFunctionAsync(bool isPageCall, string expression, object args, float?pollingInterval, float?timeout)
 => (await _channel.WaitForFunctionAsync(
         expression: expression,
         isFunction: expression.IsJavascriptFunction(),
         arg: ScriptsHelper.SerializedArgument(args),
         isPage: isPageCall,
         timeout: timeout,
         polling: pollingInterval).ConfigureAwait(false)).Object;
Ejemplo n.º 7
0
        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.Object);
                }
                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)
                {
                    await taskResult.ConfigureAwait(false);

                    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);
                    }
                }

                await _channel.ResolveAsync(result).ConfigureAwait(false);
            }
            catch (TargetInvocationException ex)
            {
                await _channel.RejectAsync(ex.InnerException).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _channel.RejectAsync(ex).ConfigureAwait(false);
            }
        }
Ejemplo n.º 8
0
 /// <inheritdoc />
 public Task DispatchEventAsync(string type, object eventInit)
 => _channel.DispatchEventAsync(
     type,
     eventInit == null ? EvaluateArgument.Undefined : ScriptsHelper.SerializedArgument(eventInit));
Ejemplo n.º 9
0
 /// <inheritdoc />
 public async Task <T> EvalOnSelectorAllAsync <T>(string selector, string expression, object arg)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvalOnSelectorAllAsync(
                                              selector: selector,
                                              script: expression,
                                              isFunction: expression.IsJavascriptFunction(),
                                              arg: arg.ToEvaluateArgument()).ConfigureAwait(false));
Ejemplo n.º 10
0
 /// <inheritdoc />
 public async Task <T> JsonValueAsync <T>() => ScriptsHelper.ParseEvaluateResult <T>(await _channel.JsonValueAsync().ConfigureAwait(false));
Ejemplo n.º 11
0
 /// <inheritdoc />
 public async Task <JsonElement?> EvaluateAsync(string expression, object arg)
 => ScriptsHelper.ParseEvaluateResult <JsonElement?>(await _channel.EvaluateExpressionAsync(
                                                         script: expression,
                                                         isFunction: expression.IsJavascriptFunction(),
                                                         arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));
Ejemplo n.º 12
0
 internal async Task <T> EvaluateAsync <T>(bool isPageCall, string script)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvaluateExpressionAsync(
                                              script: script,
                                              isFunction: script.IsJavascriptFunction(),
                                              arg: EvaluateArgument.Undefined,
                                              isPage: isPageCall).ConfigureAwait(false));