Beispiel #1
0
        public Task <JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan?timeout)
        {
            var factory = RenderThreadTaskFactory;
            var browser = browsers.FirstOrDefault(x => x.BrowserId == browserId);

            if (browser == null)
            {
                return(TaskExtensions.FromResult(new JavascriptResponse
                {
                    Success = false,
                    Message = string.Format("Browser with Id {0} not found in Render Sub Process.", browserId)
                }));
            }

            var task = factory.StartNew(() =>
            {
                try
                {
                    var response = browser.DoEvaluateScript(frameId, script);

                    return(response);
                }
                catch (Exception ex)
                {
                    return(new JavascriptResponse
                    {
                        Success = false,
                        Message = ex.ToString()
                    });
                }
            }, TaskCreationOptions.AttachedToParent);

            return(timeout.HasValue ? task.WithTimeout(timeout.Value) : task);
        }
        /// <summary>
        /// Execute a method call over the DevTools protocol. This is a more structured
        /// version of SendDevToolsMessage. <see cref="ExecuteDevToolsMethod"/> can only be called on the
        /// CEF UI Thread, this method can be called on any thread.
        /// See the DevTools protocol documentation at https://chromedevtools.github.io/devtools-protocol/ for details
        /// of supported methods and the expected <paramref name="parameters"/> dictionary contents.
        /// See the SendDevToolsMessage documentation for additional usage information.
        /// </summary>
        /// <param name="browser">the browser instance</param>
        /// <param name="messageId">is an incremental number that uniquely identifies the message (pass 0 to have the next number assigned
        /// automatically based on previous values)</param>
        /// <param name="method">is the method name</param>
        /// <param name="parameters">are the method parameters represented as a dictionary,
        /// which may be empty.</param>
        /// <returns>return a Task that can be awaited to obtain the assigned message Id. If the message was
        /// unsuccessfully submitted for validation, this value will be 0.</returns>
        public static Task <int> ExecuteDevToolsMethodAsync(this IBrowser browser, int messageId, string method, IDictionary <string, object> parameters = null)
        {
            WebBrowserExtensions.ThrowExceptionIfBrowserNull(browser);

            var browserHost = browser.GetHost();

            WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(browserHost);

            if (CefThread.CurrentlyOnUiThread)
            {
                return(TaskExtensions.FromResult(browserHost.ExecuteDevToolsMethod(messageId, method, parameters)));
            }

            if (CefThread.CanExecuteOnUiThread)
            {
                return(CefThread.ExecuteOnUiThread(() =>
                {
                    return browserHost.ExecuteDevToolsMethod(messageId, method, parameters);
                }));
            }

            //CEF returns 0 to signify failure, we'll do the same.
            return(TaskExtensions.FromResult(0));
        }