This repository was archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathExecuteJavaScriptDemoController.cs
124 lines (112 loc) · 4.21 KB
/
ExecuteJavaScriptDemoController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExecuteJavaScriptDemoController.cs" company="Chromely Projects">
// Copyright (c) 2017-2018 Chromely Projects
// </copyright>
// <license>
// See the LICENSE.md file in the project root for more information.
// </license>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Text.Json;
using Chromely.Core.Configuration;
using Chromely.Core.Logging;
using Chromely.Core.Network;
namespace CrossPlatDemo.Controllers
{
/// <summary>
/// The demo controller.
/// </summary>
[ControllerProperty(Name = "ExecuteJavaScriptDemoController", Route = "executejavascript")]
public class ExecuteJavaScriptDemoController : ChromelyController
{
private readonly IChromelyConfiguration _config;
/// <summary>
/// Initializes a new instance of the <see cref="ExecuteJavaScriptDemoController"/> class.
/// </summary>
public ExecuteJavaScriptDemoController(IChromelyConfiguration config)
{
_config = config;
RegisterPostRequest("/executejavascript/execute", Execute);
}
/// <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 scriptExecutor = _config?.JavaScriptExecutor;
if (scriptExecutor == null)
{
response.Data = $"Frame {scriptInfo.FrameName} does not exist.";
return response;
}
scriptExecutor.ExecuteScript(scriptInfo.Script);
response.Data = "Executed script :" + scriptInfo.Script;
return response;
}
catch (Exception e)
{
Logger.Instance.Log.Error(e);
response.Data = e.Message;
response.ReadyState = (int)ReadyState.RequestReceived;
response.Status = (int)System.Net.HttpStatusCode.BadRequest;
response.StatusText = "Error";
}
return response;
}
/// <summary>
/// The script info.
/// </summary>
private class ScriptInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ScriptInfo"/> class.
/// </summary>
/// <param name="postData">
/// The post data.
/// </param>
public ScriptInfo(object postData)
{
FrameName = string.Empty;
Script = string.Empty;
if (postData != null)
{
var options = new JsonSerializerOptions();
options.ReadCommentHandling = JsonCommentHandling.Skip;
options.AllowTrailingCommas = true;
var requestData = JsonSerializer.Deserialize<scriptInfo>(postData.ToString(), options);
FrameName = requestData.framename ?? string.Empty;
Script = requestData.script ?? string.Empty;
}
}
/// <summary>
/// Gets the frame name.
/// </summary>
public string FrameName { get; }
/// <summary>
/// Gets the script.
/// </summary>
public string Script { get; }
}
private class scriptInfo
{
public string framename { get; set; }
public string script { get; set; }
}
}
}