0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その560

Last updated at Posted at 2025-02-13

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

ブックマークレット開発環境を作れ。

方針

  • ドッキング使う。
  • ChromeDevToolsProtocol使う。
  • websocket-sharp使う。
  • litjson使う。

写真

image.png

サンプルコード


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 TextBox bo2;
		public TextBox bo3;
		private int n = 0;
		public Win1() {
			Label label0 = new Label();
			label0.Location = new Point(20, 10);
			label0.Text = "url";
			bo1 = new TextBox();
			bo1.Text = "https://auctions.yahoo.co.jp/jp/show/submit?category=0";
			bo1.Font = new Font("MS Pゴシック", 12);
			bo1.Size = new Size(300, 20);
			bo1.Location = new Point(50, 30);
			Controls.AddRange(new Control[] {
				label0,
				bo1
			});
			Label label1 = new Label();
			label1.Location = new Point(20, 50);
			label1.Text = "bookmarklet";
			bo2 = new TextBox();
			bo2.Text = "document.querySelector('#fleaTitleForm').value = '商品名は、これです。';";
			bo2.Multiline = true;
			bo2.Font = new Font("MS Pゴシック", 12);
			bo2.Size = new Size(300, 200);
			bo2.Location = new Point(50, 70);
			Controls.AddRange(new Control[] {
				label1,
				bo2
			});
			Label label2 = new Label();
			label2.Location = new Point(20, 280);
			label2.Text = "command";
			bo3 = new TextBox();
			bo3.Text = "getkey";
			bo3.Font = new Font("MS Pゴシック", 12);
			bo3.Size = new Size(300, 20);
			bo3.Location = new Point(50, 300);
			Controls.AddRange(new Control[] {
				label2,
				bo3
			});
			Button btn0 = new Button();
			btn0.Location = new Point(250, 330);
			btn0.Text = "send";
			btn0.Click += btn0_Click;
			Controls.AddRange(new Control[] {
				btn0
			});
		}
		void btn0_Click(object sender, System.EventArgs e) {
			int ind = bo3.Text.IndexOf("getkey");
			if (ind == 0)
			{
				getkey();
				bo3.Text = "";
				return;
			}
			ind = bo3.Text.IndexOf("open");
			if (ind == 0)
			{
				open();
				bo3.Text = "";
				return;
			}
			ind = bo3.Text.IndexOf("test");
			if (ind == 0)
			{
				test();
				bo3.Text = "";
				return;
			}
			MessageBox.Show("No Command!!");
			bo3.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() {
			n++;
			string json = "{\"id\": " + n.ToString() + ", \"method\": \"Runtime.evaluate\", \"params\": {\"expression\": \"location.href='" + bo1.Text + "'\"}}";
			Console.WriteLine(json);
			if (!string.IsNullOrEmpty(json))
				client.Send(json);
		}
		void test() {
			n++;
			string json = "{\"id\":" + n.ToString() + ", \"method\":\"Runtime.evaluate\", \"params\":{\"expression\":\"" + bo2.Text + "\"}}";
			Console.WriteLine(json);
			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\ntest\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());
		}
	}
}






以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?