コード例 #1
0
        /// <summary>
        /// Launches Chrome using the specified options
        /// </summary>
        static public async Task LaunchAsync(ChromeStartOptions Options, Action Closed = null)
        {
            if (Browser == null)
            {
                // prepare options
                IsAspNetCoreApp = Options.IsAspNetCoreApp;

                if (!string.IsNullOrWhiteSpace(Options.ContentFolder))
                {
                    ContentFolder = Path.GetFullPath(Options.ContentFolder);
                }

                HomeUrl = !IsAspNetCoreApp ? $@"http://{SStaticApp}:{Port}/{Options.HomeUrl}" : $"http://localhost:{Port}";

                List <string> ArgList = new List <string>(DefaultArgs);

                // --app=data:text/html,TITLE
                string AppValue = !IsAspNetCoreApp ? "data:text/html, loading..." : Chrome.HomeUrl;

                ArgList.Add($"--app={AppValue}");   // The --app= argument opens Chrome in app mode that is no fullscreen, no url bar, just the window
                ArgList.Add($"--window-size={Options.Width},{Options.Height}");
                ArgList.Add($"--window-position={Options.Left},{Options.Top}");

                LaunchOptions LaunchOptions = new LaunchOptions
                {
                    Devtools        = false,
                    Headless        = false,
                    Args            = ArgList.ToArray(),
                    ExecutablePath  = !string.IsNullOrWhiteSpace(Options.ChromePath) ? Options.ChromePath : GetChromPath(),
                    DefaultViewport = null
                };

                // launch Chrome
                Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);

                // get the main tab page
                Page[] Pages = await Browser.PagesAsync().ConfigureAwait(false);

                TabPage = Pages[0];

                // event handler for requests when this is a static app, meaning an ordinary HTML Application totally comprised of static files
                if (!IsAspNetCoreApp)
                {
                    await TabPage.SetRequestInterceptionAsync(true).ConfigureAwait(false);

                    TabPage.Request += StaticAppRequestHandler;
                    await TabPage.GoToAsync(Chrome.HomeUrl, WaitUntilNavigation.DOMContentLoaded).ConfigureAwait(false);
                }

                // event handler for close
                TabPage.Close += (sender, ea) => {
                    Closed?.Invoke();
                    Closed  = null;
                    TabPage = null;
                    if (!Browser.IsClosed)
                    {
                        Browser.CloseAsync();
                    }
                    Browser = null;
                };
            }
        }
コード例 #2
0
 /// <summary>
 /// Launches Chrome using the specified options
 /// </summary>
 static public void Launch(ChromeStartOptions Options, Action Closed = null)
 {
     LaunchAsync(Options, Closed).GetAwaiter().GetResult();
 }