cscの作法 その169

# 概要
cscの作法、調べてみた。
ChromeDevToolsProtocol、叩いてみた。
ヤフオクの出品フォームに書き込んでみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/6039ab99-778f-4447-ebc8-e65adf3d8b36.png)

# サンプルコード

```

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Diagnostics;
using WebSocketSharp;
using WebSocketSharp.Net;
using LitJson;
using System.Net;
using System.Text;
using System.Web;
using System.IO;

public class Form1 : Form {
	private WebSocket client;
	Button button0,
		button1,
		button2,
		button3,
		button4;
	string webSocketDebuggerUrl;
	public Form1() {
		this.ClientSize = new Size(400, 400);
		this.Text = "cdp";
		button0 = new Button();
		button0.Location = new Point(10, 10);
		button0.Size = new Size(80, 30);
		button0.Text = "open";
		button1 = new Button();
		button1.Location = new Point(110, 10);
		button1.Size = new Size(80, 30);
		button1.Text = "start";
		button2 = new Button();
		button2.Location = new Point(210, 10);
		button2.Size = new Size(80, 30);
		button2.Text = "set";
		button3 = new Button();
		button3.Location = new Point(10, 60);
		button3.Size = new Size(80, 30);
		button3.Text = "up";
		button4 = new Button();
		button4.Location = new Point(110, 60);
		button4.Size = new Size(80, 30);
		button4.Text = "";
		Controls.Add(button0);
		Controls.Add(button1);
		Controls.Add(button2);
		Controls.Add(button3);
		Controls.Add(button4);
		button0.Click += Button0_Click;
		button1.Click += Button1_Click;
		button2.Click += Button2_Click;
		button3.Click += Button3_Click;
		button4.Click += Button4_Click;
	}
	private void Button0_Click(object sender, EventArgs e) {
		using (var process = new Process()) 
		{
			process.StartInfo = new ProcessStartInfo {
				FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
				Arguments = @"--remote-debugging-Port=9222",
				CreateNoWindow = true,
				UseShellExecute = false,
			};
			process.Start();
		}
	}
	private void Button1_Click(object sender, EventArgs e) {
		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();
		}
		response.Close();
	}
	private void Button2_Click(object sender, EventArgs e) {
		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);
	}
	private void Button3_Click(object sender, EventArgs e) {
		string json = "{\"id\": 1, \"method\": \"Runtime.evaluate\", \"params\": {\"expression\": \"document.querySelector('#fleaTitleForm').value = '商品名は、これです。';\"}}";
		if (!string.IsNullOrEmpty(json))
			client.Send(json);

	}
	private void Button4_Click(object sender, EventArgs e) {

	}
	[STAThread]
	public static void Main() {
		Application.Run(new Form1());
	}
}





```

以上。