using ChaoXingAutoClass.Browser; using Microsoft.Playwright; using System.Text.Json; var profile = args.ElementAtOrDefault(0) ?? throw new ArgumentException("Pass the dedicated browser profile path."); var endpointFile = args.ElementAtOrDefault(1) ?? throw new ArgumentException("Pass the endpoint file path."); await using var host = await HiddenBrowserHost.StartAsync(profile, CancellationToken.None); Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(endpointFile))!); await File.WriteAllTextAsync(endpointFile, host.Endpoint); Console.WriteLine(host.Endpoint); if (args.Contains("--standalone")) await Task.Delay(Timeout.InfiniteTimeSpan); else while (await Console.In.ReadLineAsync() is { } line && line != "stop") { string? requestId = null; try { using var request = JsonDocument.Parse(line); requestId = request.RootElement.GetProperty("requestId").GetString(); var command = request.RootElement.GetProperty("command").GetString(); if (command == "create") { var page = await host.CreateHiddenPageAsync(requestId!); Console.WriteLine(JsonSerializer.Serialize(new { requestId, targetId = page.TargetId, url = page.Url })); } else if (command == "close") { await host.CloseHiddenPageAsync(request.RootElement.GetProperty("targetId").GetString()!); Console.WriteLine(JsonSerializer.Serialize(new { requestId })); } } catch (Exception error) { Console.WriteLine(JsonSerializer.Serialize(new { requestId, error = error.Message })); } }