Esempio n. 1
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="ChromelyResponse"/>.
        /// </returns>
        private ChromelyResponse Execute(ChromelyRequest request)
        {
            var response = new ChromelyResponse(request.Id)
            {
                ReadyState = (int)ReadyState.ResponseIsReady,
                Status     = (int)System.Net.HttpStatusCode.OK,
                StatusText = "OK",
                Data       = DateTime.Now.ToLongDateString()
            };

            try
            {
                var scriptInfo = new ScriptInfo(request.PostData);
                var frame      = FrameHandler.GetFrame(scriptInfo.FrameName);
                if (frame == null)
                {
                    response.Data = $"Frame {scriptInfo.FrameName} does not exist.";
                    return(response);
                }

                frame.ExecuteJavaScript(scriptInfo.Script, null, 0);
                response.Data = "Executed script :" + scriptInfo.Script;
                return(response);
            }
            catch (Exception e)
            {
                response.Data       = e.Message;
                response.ReadyState = (int)ReadyState.RequestReceived;
                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                response.StatusText = "Error";
            }

            return(response);
        }
Esempio n. 2
0
        private void CommandAction(string path, IDictionary <string, string[]> queryParameters)
        {
            try
            {
                var data = new LitJson.JsonData();
                data["Path"] = path;
                if (queryParameters == null || !queryParameters.Any())
                {
                    data["QueryParams"] = "No query parameters";
                }
                else
                {
                    var qdata        = LitJson.JsonMapper.ToJson(queryParameters);
                    var encodedQdata = qdata.JavaScriptStringEncode();
                    data["QueryParams"] = encodedQdata;
                }

                var script = string.Format(COMMANDRESPONSESCRIPTFORMAT, data.ToJson());

                CefFrame frame = FrameHandler.GetFrame("alldemoframe");
                if (frame == null)
                {
                    var errorMsg = $"Frame alldemoframe does not exist.";
                    Log.Error(errorMsg);
                    return;
                }

                frame.ExecuteJavaScript(script, null, 0);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// The evaluate.
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="ChromelyResponse"/>.
        /// </returns>
        private ChromelyResponse Evaluate(ChromelyRequest request)
        {
            var response = new ChromelyResponse(request.Id)
            {
                ReadyState = (int)ReadyState.ResponseIsReady,
                Status     = (int)System.Net.HttpStatusCode.OK,
                StatusText = "OK",
                Data       = DateTime.Now.ToLongDateString()
            };

            try
            {
                var scriptInfo = new ScriptInfo(request.PostData);
                var frame      = FrameHandler.GetFrame(scriptInfo.FrameName);
                if (frame == null)
                {
                    response.Data = $"Frame {scriptInfo.FrameName} does not exist.";
                    return(response);
                }

                var javascriptResponse = frame.EvaluateScriptAsync(scriptInfo.Script);
                javascriptResponse.Wait();
                var status = javascriptResponse.Result.Success
                                    ? "Successfully executed :"
                                    : "Error in executing :";

                response.Data = string.IsNullOrEmpty(javascriptResponse.Result?.Result?.ToString())
                                    ? status + scriptInfo.Script
                                    : javascriptResponse.Result?.Result?.ToString();
                return(response);
            }
            catch (Exception exception)
            {
                response.Data       = exception.Message;
                response.ReadyState = (int)ReadyState.RequestReceived;
                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                response.StatusText = "Error";
            }

            return(response);
        }