-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZombIE.cs
267 lines (219 loc) · 9.23 KB
/
ZombIE.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* ZombIE 1.0.1
* Copyright (c) 2020 Asai Toshiya.
*
* License: The MIT License (https://opensource.org/licenses/MIT)
*/
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Automation;
namespace YourNamespace
{
public class ZombIE
{
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
private static readonly string DefaultDownloadDirectory = GetDefaultDownloadDirectory();
private static readonly Regex DialogDownloadFileNameRegex = new Regex(@"(.*?) で行う操作を選んでください。");
private static readonly Regex NotificationBarDownloadFileNameRegex = new Regex(@".*? から (.*?) (\(.*?\) )?を開くか、または保存しますか\?");
public static TimeSpan ClickDelay { get; set; } = TimeSpan.FromMilliseconds(500);
public static TimeSpan DownloadTimeout { get; set; } = TimeSpan.Zero;
public static TimeSpan FindElementTimeout { get; set; } = TimeSpan.FromSeconds(30);
public static void DownloadFileTo(string path)
{
var downloadFileName = Timeout<string>(
() => FromDialog() ?? FromNoticationBar(),
(x) => x != null,
FindElementTimeout
);
var downloadedFileName = WaitUntilDownloadCompleted(downloadFileName);
var downloadedFile = Path.Combine(DefaultDownloadDirectory, downloadedFileName);
File.Move(downloadedFile, path);
}
private static void Click(AutomationElement ae)
{
(ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern).Invoke();
}
private static AutomationElement FindElementByName(AutomationElement parent, string name)
{
return parent.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, name));
}
private static AutomationElement FindNotificationBar(IntPtr hWnd)
{
var hWndNotificationBar = FindWindowEx(hWnd, IntPtr.Zero, "Frame Notification Bar", string.Empty);
if (hWnd == IntPtr.Zero)
{
return null;
}
return AutomationElement.FromHandle(hWndNotificationBar);
}
private static AutomationElement FindText(AutomationElement parent)
{
return parent.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
}
private static string FromDialog()
{
var hWnd = FindWindow(null, "Internet Explorer");
if (hWnd == IntPtr.Zero)
{
return null;
}
var window = AutomationElement.FromHandle(hWnd);
var text = FindText(window);
if (text == null)
{
return null;
}
var name = text.Current.Name;
var match = DialogDownloadFileNameRegex.Match(name);
if (!match.Success)
{
return null;
}
var saveButton = FindElementByName(window, "保存(S)");
if (saveButton == null)
{
return null;
}
var fileName = match.Groups[1].Value;
Thread.Sleep(ClickDelay);
Click(saveButton);
return fileName;
}
private static string FromNoticationBar()
{
var processes = Process.GetProcessesByName("iexplore");
foreach (var process in processes)
{
var hWnd = process.MainWindowHandle;
var notificationBar = FindNotificationBar(hWnd);
if (notificationBar == null)
{
continue;
}
var text = FindText(notificationBar);
if (text == null)
{
continue;
}
var textValue = GetValue(text);
var match = NotificationBarDownloadFileNameRegex.Match(textValue);
if (!match.Success)
{
continue;
}
var saveButton = FindElementByName(notificationBar, "保存");
if (saveButton == null)
{
continue;
}
var fileName = match.Groups[1].Value;
Thread.Sleep(ClickDelay);
Click(saveButton);
return fileName;
}
return null;
}
private static string GetDefaultDownloadDirectory()
{
// c# - How to find browser download folder path - Stack Overflow
// https://stackoverflow.com/a/24673279
var path = string.Empty;
var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main");
if (key != null)
{
path = (string)key.GetValue("Default Download Directory");
}
if (string.IsNullOrEmpty(path))
{
path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\downloads";
}
return path;
}
private static string GetValue(AutomationElement ae)
{
return (ae.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern).Current.Value;
}
private static T Timeout<T>(Func<T> action, Predicate<T> predicate, TimeSpan timeout)
{
var endTime = timeout == TimeSpan.Zero ? DateTime.MaxValue : DateTime.Now + timeout;
while (DateTime.Now < endTime)
{
var x = action();
if (predicate(x))
{
return x;
}
Thread.Sleep(100);
}
throw new TimeoutException();
}
private static string WaitUntilDownloadCompleted(string downloadFileName)
{
var movedOrDeletedText = downloadFileName + " は移動または削除された可能性があります。";
var index = downloadFileName.LastIndexOf('.');
var fileNamePattern = index == -1
? Regex.Escape(downloadFileName) + @"( \(\d*?\))*"
: Regex.Escape(downloadFileName.Substring(0, index)) + @"( \(\d*?\))*\." + downloadFileName.Substring(index + 1);
var downloadCompletedRegex = new Regex("(" + fileNamePattern + ") のダウンロードが完了しました。");
var downloadedFileName = Timeout<string>(
() => {
var processes = Process.GetProcessesByName("iexplore");
foreach (var process in processes)
{
var hWnd = process.MainWindowHandle;
var notificationBar = FindNotificationBar(hWnd);
if (notificationBar == null)
{
continue;
}
var text = FindText(notificationBar);
if (text == null)
{
continue;
}
var textValue = GetValue(text);
if (textValue == movedOrDeletedText)
{
var retryButton = FindElementByName(notificationBar, "再試行");
if (retryButton != null)
{
Thread.Sleep(ClickDelay);
Click(retryButton);
}
continue;
}
var match = downloadCompletedRegex.Match(textValue);
if (!match.Success)
{
continue;
}
var closeButton = FindElementByName(notificationBar, "閉じる");
if (closeButton == null)
{
continue;
}
var fileName = match.Groups[1].Value;
Thread.Sleep(ClickDelay);
Click(closeButton);
return fileName;
}
return null;
},
(x) => x != null,
DownloadTimeout
);
return downloadedFileName;
}
}
}