-
Notifications
You must be signed in to change notification settings - Fork 2
/
Kamite-Watch-Clipboard.ps1
executable file
·208 lines (173 loc) · 5.24 KB
/
Kamite-Watch-Clipboard.ps1
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Watches the clipboard and sends new text to Kamite.
# Based on https://github.com/samjones3/powershell-utilities/blob/main/PowerShell-Clipboard-Watcher.ps1
# The watcher will be active for as long as the console window it was started in stays open.
# Note: Some programs in some situations will trigger clipboard change multiple times, resulting in duplicate chunks.
[CmdletBinding()]
param
(
)
$KamiteHost = "http://localhost:4110"
$global:ShowChunkEndpoint = "$KamiteHost/cmd/chunk/show"
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function PSScriptRoot { $MyInvocation.ScriptName | Split-Path }
Trap { throw $_ }
function Register-ClipboardWatcher
{
if (-not (Test-Path Variable:Global:ClipboardWatcher))
{
Register-ClipboardWatcherType
$Global:ClipboardWatcher = New-Object ClipboardWatcher
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -SupportEvent -Action `
{
Unregister-ClipboardWatcher
}
}
return $Global:ClipboardWatcher
}
function Unregister-ClipboardWatcher
{
if (Test-Path Variable:Global:ClipboardWatcher)
{
$Global:ClipboardWatcher.Dispose();
Remove-Variable ClipboardWatcher -Scope Global
Unregister-Event -SourceIdentifier ClipboardWatcher
}
}
function Register-ClipboardWatcherType
{
Add-Type -ReferencedAssemblies System.Windows.Forms, System.Drawing -Language CSharp -TypeDefinition `
@"
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
public class ClipboardWatcher : IDisposable
{
readonly Thread _formThread;
bool _disposed;
public ClipboardWatcher()
{
_formThread = new Thread(() => { new ClipboardWatcherForm(this); })
{
IsBackground = true
};
_formThread.SetApartmentState(ApartmentState.STA);
_formThread.Start();
}
public void Dispose()
{
if (_disposed)
return;
Disposed();
if (_formThread != null && _formThread.IsAlive)
_formThread.Abort();
_disposed = true;
GC.SuppressFinalize(this);
}
~ClipboardWatcher()
{
Dispose();
}
public event Action<string> ClipboardTextChanged = delegate { };
public event Action Disposed = delegate { };
public void OnClipboardTextChanged(string text)
{
ClipboardTextChanged(text);
}
}
public class ClipboardWatcherForm : Form
{
public ClipboardWatcherForm(ClipboardWatcher clipboardWatcher)
{
HideForm();
RegisterWin32();
ClipboardTextChanged += clipboardWatcher.OnClipboardTextChanged;
clipboardWatcher.Disposed += () => InvokeIfRequired(Dispose);
Disposed += (sender, args) => UnregisterWin32();
Application.Run(this);
}
void InvokeIfRequired(Action action)
{
if (InvokeRequired)
Invoke(action);
else
action();
}
public event Action<string> ClipboardTextChanged = delegate { };
void HideForm()
{
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
Load += (sender, args) => { Size = new Size(0, 0); };
}
void RegisterWin32()
{
User32.AddClipboardFormatListener(Handle);
}
void UnregisterWin32()
{
if (IsHandleCreated)
User32.RemoveClipboardFormatListener(Handle);
}
protected override void WndProc(ref Message m)
{
switch ((WM) m.Msg)
{
case WM.WM_CLIPBOARDUPDATE:
ClipboardChanged();
break;
default:
base.WndProc(ref m);
break;
}
}
void ClipboardChanged()
{
if (Clipboard.ContainsText())
ClipboardTextChanged(Clipboard.GetText());
}
}
public enum WM
{
WM_CLIPBOARDUPDATE = 0x031D
}
public class User32
{
const string User32Dll = "User32.dll";
[DllImport(User32Dll, CharSet = CharSet.Auto)]
public static extern bool AddClipboardFormatListener(IntPtr hWndObserver);
[DllImport(User32Dll, CharSet = CharSet.Auto)]
public static extern bool RemoveClipboardFormatListener(IntPtr hWndObserver);
}
"@
}
function Register-ClipboardTextChangedEvent
{
param
(
[ScriptBlock] $Action
)
# before registering, unregister it, so there is no collision when you keep
# rerunning this thing during debugging and development.
# the -EA flag tells it to run silently (as will throw an error if event is not already registered)
Unregister-Event -SourceIdentifier ClipboardWatcher -EA 0
$watcher = Register-ClipboardWatcher
Register-ObjectEvent $watcher -EventName ClipboardTextChanged -Action $Action -SourceIdentifier ClipboardWatcher
}
Register-ClipboardTextChangedEvent -Action `
{
param
(
[string] $text
)
$body = @{
"chunk" = $text
}
Invoke-RestMethod -Method 'Post' `
-Uri $ShowChunkEndpoint `
-Body "$($body | ConvertTo-Json)"
Write-Host "Sent: $text"
}
Wait-Event -SourceIdentifier NonExistentEventThatWeWillWatchForToKeepTheWindowOpen