-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
195 lines (169 loc) · 7.13 KB
/
Program.cs
File metadata and controls
195 lines (169 loc) · 7.13 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using CommandLine;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace PlexAutoIntroSkip
{
/// <summary>
/// Program entry point.
/// </summary>
public class Program
{
/// <summary>
/// Sets the specified window's show state.
/// </summary>
/// <remarks>
/// See <see href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow">ShowWindow</see> MS Docs for more information.
/// </remarks>
/// <param name="hWnd">A handle to the window.</param>
/// <param name="nCmdShow">Controls how the window is to be shown.</param>
/// <returns>
/// If the window was previously visible, the return value is nonzero.
/// If the window was previously hidden, the return value is zero.
/// </returns>
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
/// <summary>
/// Program entry point.
/// </summary>
/// <param name="args">Command-line arguments.</param>
public static void Main(string[] args)
{
var options = GetProgramOptions(args);
// Clear error log on startup
File.WriteAllText("error.log", string.Empty);
// Was called using own process window?
var hWnd = Process.GetCurrentProcess().MainWindowHandle;
if (hWnd.ToInt32() != 0)
{
ShowWindow(hWnd, 0);
}
var edgeOptions = new EdgeOptions
{
UseChromium = true,
};
// Disable "Chrome is being controlled by automated test software" infobar.
edgeOptions.AddExcludedArgument("enable-automation");
edgeOptions.AddAdditionalOption("useAutomationExtension", false);
edgeOptions.AddArguments(
$"user-data-dir={Directory.GetCurrentDirectory()}\\User Data",
"profile-directory=Profile 1",
$"app={options.PlexUrl}");
try
{
WebDriverManager.AutoInstall();
}
catch (Exception exception)
{
LogException(exception);
}
var edgeProcessName = "msedge";
var edgeProcessIds = Process.GetProcessesByName(edgeProcessName).Select(p => p.Id);
var service = EdgeDriverService.CreateDefaultService(
WebDriverManager.MsEdgeDriverDirectoryName, WebDriverManager.MsEdgeDriverFileName);
var driver = new EdgeDriver(service, edgeOptions);
var browserProcessId = Process.GetProcessesByName(edgeProcessName).Select(p => p.Id)
.Except(edgeProcessIds)
.First();
while (ProcessExistsById(browserProcessId))
{
try
{
MainProgramLoop(driver, options, browserProcessId);
}
catch (Exception exception)
{
LogException(exception);
}
}
Process.GetProcessById(service.ProcessId).Kill();
}
/// <summary>
/// Run main program loop.
/// </summary>
/// <param name="driver"><see name="RemoteWebDriver"/> to be used to drive web browser.</param>
/// <param name="options"><see name="ProgramOptions"/>.</param>
/// <param name="browserProcessId">Web browser process ID to check if exists.</param>
private static void MainProgramLoop(RemoteWebDriver driver, ProgramOptions options, int browserProcessId)
{
var waitDriver = new WebDriverWait(driver, TimeSpan.FromDays(365));
var nullWebElement = new RemoteWebElement(driver, string.Empty);
var skipIntroButtonXPath = "//button[text()='Skip Intro']";
while (ProcessExistsById(browserProcessId))
{
// Waiting for Skip Intro button to be visible, or for browser to be closed manually.
var skipIntroButton = (RemoteWebElement)waitDriver.Until(webDriver =>
ProcessExistsById(browserProcessId)
? webDriver.FindElement(By.XPath(skipIntroButtonXPath))
: nullWebElement);
// Skip Intro button will only be equal to `nullWebElement` if the browser process
// no longer exists.
if (skipIntroButton == nullWebElement)
{
break;
}
// Skip Intro button is visible before intro starts, so wait for the actual intro to start.
Thread.Sleep(options.SkipButtonWaitTime);
skipIntroButton.Click();
// Waiting for Skip Intro button to no longer be visible.
waitDriver.Until(webDriver =>
{
try
{
webDriver.FindElement(By.XPath(skipIntroButtonXPath));
return false;
}
catch
{
return true;
}
});
}
}
/// <summary>
/// Parse <paramref name="args"/> into <see cref="ProgramOptions"/>.
/// </summary>
/// <param name="args">Command-line arguments.</param>
/// <returns><see cref="ProgramOptions"/></returns>
private static ProgramOptions GetProgramOptions(string[] args)
{
ProgramOptions options = null;
CommandLine.Parser.Default.ParseArguments<ProgramOptions>(args)
.WithParsed(parsedOptions => options = parsedOptions);
return options;
}
/// <summary>
/// Recursively write <see name="Exception"/> to error log file.
/// </summary>
/// <param name="exception">Root <see name="Exception"/>.</param>
private static void LogException(Exception exception)
{
using var writer = new StreamWriter("error.log", append: true);
writer.WriteLine("--------------------------------------------------");
writer.WriteLine($"[{DateTime.Now}]");
writer.WriteLine();
while (exception != null)
{
writer.WriteLine(exception.GetType().FullName);
writer.WriteLine(exception.Message);
writer.WriteLine(exception.StackTrace);
writer.WriteLine();
exception = exception.InnerException;
}
}
/// <summary>
/// Check if the process exists by given <paramref name="processId"/>.
/// </summary>
/// <param name="processId">Process ID.</param>
/// <returns>True if process exists, otherwise false.</returns>
private static bool ProcessExistsById(int processId)
=> Process.GetProcesses().Any(p => p.Id == processId);
}
}