Beispiel #1
0
        public T EvaluateScript <T>(string script, TimeSpan?timeout = null)
        {
            this.EnsureWebViewNotDisposing();
            string     script2    = JavascriptExecutor.WrapScriptWithErrorHandling(script);
            ScriptTask scriptTask = this.QueueScript(script2, timeout, true);

            if (!this.flushRunning)
            {
                if (!scriptTask.WaitHandle.WaitOne(timeout ?? TimeSpan.FromSeconds(15.0)) || scriptTask.Result == null)
                {
                    throw new JavascriptException("Timeout", "Javascript engine is not initialized", null);
                }
            }
            else if (!scriptTask.WaitHandle.WaitOne() || scriptTask.Result == null)
            {
                throw new JavascriptException("Timeout", ((timeout != null) ? string.Format("More than {0}ms elapsed", timeout.Value.TotalMilliseconds) : "Timeout ocurred") + string.Format(" evaluating the script: '{0}'", script), null);
            }
            if (scriptTask.Exception != null)
            {
                throw scriptTask.Exception;
            }
            if (scriptTask.Result.Success)
            {
                return(this.GetResult <T>(scriptTask.Result.Result));
            }
            throw JavascriptExecutor.ParseResponseException(scriptTask.Result);
        }
Beispiel #2
0
        /// <summary>
        /// 方法内部定义了死循环,用此循环来捕捉需要执行的js 代码
        /// </summary>
        private void InnerFlushScripts()
        {
            ScriptTask        scriptTask = null;
            List <ScriptTask> list       = new List <ScriptTask>();
            ScriptTask        scriptTask2;

            for (; ;)                //
            {
                scriptTask2 = this.pendingScripts.Take(this.flushTaskCancelationToken.Token);
                if (scriptTask2.WaitHandle != null)
                {
                    break;
                }
                list.Add(scriptTask2);
                if (this.pendingScripts.Count <= 0)
                {
                    goto IL_42;
                }
            }
            scriptTask = scriptTask2;
IL_42:
            if (list.Count > 0)
            {
                Task <JavascriptResponse> task = this.OwnerWebView.chromium.EvaluateScriptAsync(JavascriptExecutor.WrapScriptWithErrorHandling(string.Join(";" + Environment.NewLine, from s in list
                                                                                                                                                           select s.Script)), this.OwnerWebView.DefaultScriptsExecutionTimeout);
                task.Wait(this.flushTaskCancelationToken.Token);
                JavascriptResponse response = task.Result;
                if (!response.Success)
                {
                    this.OwnerWebView.ExecuteWithAsyncErrorHandling(delegate
                    {
                        throw JavascriptExecutor.ParseResponseException(response);
                    });
                }
            }
            if (scriptTask != null)
            {
                Task <JavascriptResponse> task2 = null;
                try
                {
                    IWebBrowser chromium = this.OwnerWebView.chromium;
                    string      script   = scriptTask.Script;
                    TimeSpan?   timeout  = scriptTask.Timeout;
                    task2 = chromium.EvaluateScriptAsync(script, (timeout != null) ? timeout : this.OwnerWebView.DefaultScriptsExecutionTimeout);
                    task2.Wait(this.flushTaskCancelationToken.Token);
                    scriptTask.Result = task2.Result;
                }
                catch (Exception exception)
                {
                    if (task2 == null || !task2.IsCanceled)
                    {
                        scriptTask.Exception = exception;
                    }
                }
                finally
                {
                    scriptTask.WaitHandle.Set();
                }
            }
        }