示例#1
0
        /// <summary>
        /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
        /// </summary>
        /// <param name="options">Options for connecting.</param>
        /// <returns>A connected browser.</returns>
        public async Task <Browser> ConnectAsync(ConnectOptions options)
        {
            try
            {
                if (_chromiumLaunched)
                {
                    throw new InvalidOperationException("Unable to create or connect to another chromium process");
                }
                _chromiumLaunched = true;

                var connectionDelay   = options.SlowMo;
                var keepAliveInterval = 0;

                _connection = await Connection.Create(options.BrowserWSEndpoint, connectionDelay, keepAliveInterval, _loggerFactory).ConfigureAwait(false);

                return(await Browser.CreateAsync(_connection, options.IgnoreHTTPSErrors, true, null, () =>
                {
                    try
                    {
                        var closeTask = _connection.SendAsync("Browser.close", null);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, ex.Message);
                    }
                    return null;
                }).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to create connection", ex);
            }
        }
示例#2
0
        /// <summary>
        /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
        /// </summary>
        /// <param name="options">Options for connecting.</param>
        /// <returns>A connected browser.</returns>
        public async Task <Browser> ConnectAsync(ConnectOptions options)
        {
            EnsureSingleLaunchOrConnect();

            if (!string.IsNullOrEmpty(options.BrowserURL) && !string.IsNullOrEmpty(options.BrowserWSEndpoint))
            {
                throw new PuppeteerException("Exactly one of browserWSEndpoint or browserURL must be passed to puppeteer.connect");
            }

            try
            {
                var browserWSEndpoint = string.IsNullOrEmpty(options.BrowserURL)
                    ? options.BrowserWSEndpoint
                    : await GetWSEndpointAsync(options.BrowserURL).ConfigureAwait(false);

                var connection = await Connection.Create(browserWSEndpoint, options, _loggerFactory).ConfigureAwait(false);

                var response = await connection.SendAsync <GetBrowserContextsResponse>("Target.getBrowserContexts").ConfigureAwait(false);

                return(await Browser
                       .CreateAsync(connection, response.BrowserContextIds, options.IgnoreHTTPSErrors, options.DefaultViewport, null)
                       .ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                throw new ChromiumProcessException("Failed to create connection", ex);
            }
        }
示例#3
0
        /// <summary>
        /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
        /// </summary>
        /// <param name="options">Options for connecting.</param>
        /// <returns>A connected browser.</returns>
        public async Task <Browser> ConnectAsync(ConnectOptions options)
        {
            try
            {
                if (_chromiumLaunched)
                {
                    throw new InvalidOperationException("Unable to create or connect to another chromium process");
                }
                _chromiumLaunched = true;

                var connectionDelay   = options.SlowMo;
                var keepAliveInterval = options.KeepAliveInterval;

                _connection = await Connection.Create(options.BrowserWSEndpoint, connectionDelay, keepAliveInterval);

                return(await Browser.CreateAsync(_connection, options, null, () =>
                {
                    var closeTask = _connection.SendAsync("Browser.close", null);
                    return null;
                }));
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to create connection", ex);
            }
        }
示例#4
0
        /// <summary>
        /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
        /// </summary>
        /// <param name="options">Options for connecting.</param>
        /// <returns>A connected browser.</returns>
        public async Task <Browser> ConnectAsync(ConnectOptions options)
        {
            EnsureSingleLaunchOrConnect();

            try
            {
                var connection = await Connection.Create(options.BrowserWSEndpoint, options, _loggerFactory).ConfigureAwait(false);

                var response = await connection.SendAsync <GetBrowserContextsResponse>("Target.getBrowserContexts");

                return(await Browser
                       .CreateAsync(connection, response.BrowserContextIds, options.IgnoreHTTPSErrors, options.DefaultViewport, null)
                       .ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                throw new ChromiumProcessException("Failed to create connection", ex);
            }
        }
示例#5
0
        /// <summary>
        /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
        /// </summary>
        /// <param name="options">Options for connecting.</param>
        /// <returns>A connected browser.</returns>
        public async Task <Browser> ConnectAsync(ConnectOptions options)
        {
            try
            {
                var connectionDelay   = options.SlowMo;
                var keepAliveInterval = options.KeepAliveInterval;

                _connection = await Connection.Create(options.BrowserWSEndpoint, connectionDelay, keepAliveInterval);

                return(await Browser.CreateAsync(_connection, options, null, () =>
                {
                    var closeTask = _connection.SendAsync("Browser.close", null);
                    return null;
                }));
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to create connection", ex);
            }
        }
示例#6
0
 /// <summary>
 /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
 /// </summary>
 /// <param name="options">Options for connecting.</param>
 /// <returns>A connected browser.</returns>
 public static Task <Browser> ConnectAsync(ConnectOptions options)
 => new Launcher().ConnectAsync(options);
示例#7
0
 /// <summary>
 /// Attaches Puppeteer to an existing Chromium instance. The browser will be closed when the Browser is disposed.
 /// </summary>
 /// <param name="options">Options for connecting.</param>
 /// <param name="loggerFactory">The logger factory</param>
 /// <returns>A connected browser.</returns>
 public static Task <Browser> ConnectAsync(ConnectOptions options, ILoggerFactory loggerFactory = null)
 => new Launcher(loggerFactory).ConnectAsync(options);