概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
edgeを操作せよ。
方針
- ドッキング使う。
- ChromeDevToolsProtocol使う。
- websocket-sharp使う。
- litjson使う。
写真
サンプルコード
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using WebSocketSharp;
using WebSocketSharp.Net;
using LitJson;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
namespace Dock
{
public partial class Win1: WeifenLuo.WinFormsUI.Docking.DockContent {
private WebSocket client;
string webSocketDebuggerUrl;
public string msg = "";
public TextBox bo1;
public Win1() {
bo1 = new TextBox();
bo1.Text = "";
bo1.Multiline = true;
bo1.Font = new Font("MS Pゴシック", 12);
bo1.Size = new Size(300, 20);
bo1.Location = new Point(50, 50);
Controls.AddRange(new Control[] {
bo1
});
Button btn0 = new Button();
btn0.Location = new Point(150, 100);
btn0.Text = "send";
btn0.Click += btn0_Click;
Controls.AddRange(new Control[] {
btn0
});
}
void btn0_Click(object sender, System.EventArgs e) {
int ind = bo1.Text.IndexOf("getkey");
if (ind == 0)
{
getkey();
bo1.Text = "";
return;
}
ind = bo1.Text.IndexOf("open");
if (ind == 0)
{
open();
bo1.Text = "";
return;
}
ind = bo1.Text.IndexOf("alert");
if (ind == 0)
{
alert();
bo1.Text = "";
return;
}
MessageBox.Show("No Command!!");
bo1.Text = "";
}
void getkey() {
WebRequest request = WebRequest.Create("http://localhost:9222/json");
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JsonData jsonData = JsonMapper.ToObject(responseFromServer);
webSocketDebuggerUrl = (string) jsonData[0]["webSocketDebuggerUrl"];
client = new WebSocket(webSocketDebuggerUrl);
client.OnOpen += (ss, ee) => Console.WriteLine(string.Format("Connected : to {0} successfully ", webSocketDebuggerUrl));
client.OnError += (ss, ee) => Console.WriteLine("Error : " + ee.Message);
client.OnMessage += (ss, ee) => Console.WriteLine("Echo : " + ee.Data);
client.OnClose += (ss, ee) => Console.WriteLine(string.Format("Disconnected : with {0} ", webSocketDebuggerUrl));
client.Connect();
MessageBox.Show("ok");
}
response.Close();
}
void open() {
string json = "{\"id\": 1, \"method\": \"Runtime.evaluate\", \"params\": {\"expression\": \"location.href='https://auctions.yahoo.co.jp/jp/show/submit?category=0'\"}}";
if (!string.IsNullOrEmpty(json))
client.Send(json);
}
void alert() {
string jsCode = "javascript:alert('ok');";
string json = "{\"id\":1, \"method\":\"Runtime.evaluate\", \"params\":{\"expression\":\"" + jsCode + "\"}}";
if (!string.IsNullOrEmpty(json))
client.Send(json);
}
}
public partial class Win2: WeifenLuo.WinFormsUI.Docking.DockContent {
public string msg = "";
public Win2() {
Button btn0 = new Button();
btn0.Location = new Point(50, 50);
btn0.Text = "start";
btn0.Click += btn0_Click;
Controls.AddRange(new Control[] {
btn0
});
}
void btn0_Click(object sender, System.EventArgs e) {
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo {
FileName = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe",
Arguments = @"--remote-debugging-Port=9222",
CreateNoWindow = true,
UseShellExecute = false,
};
process.Start();
}
msg = "edge start ok\r\n";
}
}
public partial class Win3: WeifenLuo.WinFormsUI.Docking.DockContent {
public TextBox bo1;
public Win3() {
bo1 = new TextBox();
bo1.Text = "";
bo1.Multiline = true;
bo1.Font = new Font("MS Pゴシック", 12);
bo1.Dock = DockStyle.Fill;
Controls.AddRange(new Control[] {
bo1
});
}
}
public partial class Form1: Form {
public Win2 left1;
public Win3 right1;
public Win1 center1;
public Win3 bottom1;
public Form1() {
ClientSize = new Size(800, 600);
Text = "dock";
var dockPanel1 = new DockPanel();
dockPanel1.ShowDocumentIcon = true;
dockPanel1.Dock = DockStyle.Fill;
dockPanel1.DocumentStyle = DocumentStyle.DockingWindow;
Controls.Add(dockPanel1);
left1 = new Win2();
left1.TabText = "left1";
left1.Show(dockPanel1, DockState.DockLeft);
right1 = new Win3();
right1.TabText = "right1";
right1.Show(dockPanel1, DockState.DockRight);
right1.bo1.AppendText("getkey\r\nopen\r\nalert\r\n");
center1 = new Win1();
center1.TabText = "center1";
center1.Show(dockPanel1, DockState.Document);
bottom1 = new Win3();
bottom1.TabText = "bottom1";
bottom1.Show(dockPanel1, DockState.DockBottom);
dockPanel1.UpdateDockWindowZOrder(DockStyle.Left, true);
Timer timer = new Timer {
Interval = 200
};
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
if (center1.msg != "")
{
bottom1.bo1.AppendText(center1.msg);
center1.msg = "";
}
if (left1.msg != "")
{
bottom1.bo1.AppendText(left1.msg);
left1.msg = "";
}
}
[STAThread]
public static void Main() {
Application.Run(new Form1());
}
}
}
以上。