Exemplo n.º 1
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));
Exemplo n.º 2
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));
Exemplo n.º 3
0
        /// <summary>
        /// Registers a new selector engine.
        /// </summary>
        /// <param name="name">Name that is used in selectors as a prefix, e.g. {name: 'foo'} enables foo=myselectorbody selectors. May only contain [a-zA-Z0-9_] characters.</param>
        /// <param name="script">Script that evaluates to a selector engine instance.</param>
        /// <param name="path">Path to the JavaScript file. If path is a relative path, then it is resolved relative to current working directory.</param>
        /// <param name="content">Raw script content.</param>
        /// <param name="contentScript">Whether to run this selector engine in isolated JavaScript environment. This environment has access to the same DOM, but not any JavaScript objects from the frame's scripts. Defaults to false. Note that running as a content script is not guaranteed when this engine is used together with other registered engines.</param>
        /// <returns>A <see cref="Task"/> that completes when the engine is registered.</returns>
        public async Task RegisterAsync(string name, string script = null, string path = null, string content = null, bool?contentScript = null)
        {
            if (string.IsNullOrEmpty(script))
            {
                script = ScriptsHelper.EvaluationScript(content, path, 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));
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            _registrations.Add(registerParam);
        }
Exemplo n.º 4
0
 internal Task DispatchEventAsync(bool isPageCall, string selector, string type, object eventInit = null, int?timeout = null)
 => _channel.DispatchEventAsync(
     selector,
     type,
     eventInit == null ? EvaluateArgument.Undefined : ScriptsHelper.SerializedArgument(eventInit),
     timeout,
     isPageCall);
Exemplo n.º 5
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));
Exemplo n.º 6
0
        /// <summary>
        /// Registers a new selector engine.
        /// </summary>
        /// <param name="name">Name that is used in selectors as a prefix, e.g. {name: 'foo'} enables foo=myselectorbody selectors. May only contain [a-zA-Z0-9_] characters.</param>
        /// <param name="script">Script that evaluates to a selector engine instance.</param>
        /// <param name="path">Path to the JavaScript file. If path is a relative path, then it is resolved relative to current working directory.</param>
        /// <param name="content">Raw script content.</param>
        /// <param name="contentScript">Whether to run this selector engine in isolated JavaScript environment. This environment has access to the same DOM, but not any JavaScript objects from the frame's scripts. Defaults to false. Note that running as a content script is not guaranteed when this engine is used together with other registered engines.</param>
        /// <returns>A <see cref="Task"/> that completes when the engine is registered.</returns>
        public async Task RegisterAsync(string name, string script = null, string path = null, string content = null, bool?contentScript = null)
        {
            if (string.IsNullOrEmpty(script))
            {
                script = ScriptsHelper.EvaluationScript(content, path, 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);
        }
Exemplo n.º 7
0
 internal async Task <IJSHandle> WaitForFunctionAsync(bool isPageCall, string expression, object args, int?timeout, Polling?polling, int?pollingInterval)
 => (await _channel.WaitForFunctionAsync(
         expression: expression,
         isFunction: expression.IsJavascriptFunction(),
         arg: ScriptsHelper.SerializedArgument(args),
         isPage: isPageCall,
         timeout: timeout,
         polling: polling != null ? null : pollingInterval).ConfigureAwait(false)).Object;
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
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));
Exemplo n.º 10
0
 /// <inheritdoc/>
 public async Task <JsonElement?> EvaluateAsync(string expression)
 => ScriptsHelper.ParseEvaluateResult <JsonElement?>(await _channel.EvaluateExpressionAsync(
                                                         script: expression,
                                                         isFunction: expression.IsJavascriptFunction(),
                                                         arg: EvaluateArgument.Undefined).ConfigureAwait(false));
Exemplo n.º 11
0
 /// <inheritdoc />
 public Task DispatchEventAsync(string type, object eventInit = null, int?timeout = null)
 => _channel.DispatchEventAsync(
     type,
     eventInit == null ? EvaluateArgument.Undefined : ScriptsHelper.SerializedArgument(eventInit),
     timeout);
Exemplo n.º 12
0
 /// <inheritdoc />
 public async Task <JsonElement?> EvalOnSelectorAllAsync(string selector, string pageFunction, object arg)
 => ScriptsHelper.ParseEvaluateResult <JsonElement?>(await _channel.EvalOnSelectorAllAsync(
                                                         selector: selector,
                                                         script: pageFunction,
                                                         isFunction: pageFunction.IsJavascriptFunction(),
                                                         arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));
Exemplo n.º 13
0
 /// <inheritdoc />
 public async Task <JsonElement?> EvalOnSelectorAllAsync(string selector, string pageFunction)
 => ScriptsHelper.ParseEvaluateResult <JsonElement?>(await _channel.EvalOnSelectorAllAsync(
                                                         selector: selector,
                                                         script: pageFunction,
                                                         isFunction: pageFunction.IsJavascriptFunction(),
                                                         arg: EvaluateArgument.Undefined).ConfigureAwait(false));
Exemplo n.º 14
0
 /// <inheritdoc />
 public async Task <T> EvalOnSelectorAsync <T>(string selector, string expression, object arg)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvalOnSelectorAsync(
                                              selector: selector,
                                              script: expression,
                                              isFunction: expression.IsJavascriptFunction(),
                                              arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));
Exemplo n.º 15
0
 /// <inheritdoc />
 public async Task <T> EvalOnSelectorAsync <T>(string selector, string expression)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvalOnSelectorAsync(
                                              selector: selector,
                                              script: expression,
                                              isFunction: expression.IsJavascriptFunction(),
                                              arg: EvaluateArgument.Undefined).ConfigureAwait(false));
Exemplo n.º 16
0
 /// <inheritdoc />
 public async Task <IJSHandle> EvaluateHandleAsync(string expression, object arg)
 => (await _channel.EvaluateExpressionHandleAsync(
         script: expression,
         isFunction: expression.IsJavascriptFunction(),
         arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false))?.Object;
Exemplo n.º 17
0
 /// <inheritdoc />
 public async Task <T> EvaluateAsync <T>(string pageFunction, object arg)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvaluateExpressionAsync(
                                              script: pageFunction,
                                              isFunction: pageFunction.IsJavascriptFunction(),
                                              arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));
Exemplo n.º 18
0
 /// <inheritdoc/>
 public async Task <T> EvaluateAsync <T>(string expression, object arg)
 => ScriptsHelper.ParseEvaluateResult <T>(await _channel.EvaluateExpressionAsync(
                                              script: expression,
                                              isFunction: expression.IsJavascriptFunction(),
                                              arg: arg,
                                              serializeArgument: true).ConfigureAwait(false));
Exemplo n.º 19
0
 /// <inheritdoc />
 public async Task <T> GetJsonValueAsync <T>() => ScriptsHelper.ParseEvaluateResult <T>(await _channel.GetJsonValueAsync().ConfigureAwait(false));