WWW-Mechanize-Chrome

 view release on metacpan or  search on metacpan

lib/WWW/Mechanize/Chrome.pm  view on Meta::CPAN

file with the C<$ENV{CHROME_BIN}> environment variable.

Additional arguments for the command are also read from the C<<$ENV{WWW_MECHANIZE_CHROME_ARGS}>>
variable and prepended to the C<launch_arg> array.

=item B<cleanup_signal>

    cleanup_signal => 'SIGKILL'

The signal that is sent to Chrome to shut it down. On Linuxish OSes, this
will be C<TERM>, on OSX and Windows it will be C<KILL>.

=item B<start_url>

  start_url => 'http://perlmonks.org'  # Immediately navigate to a given URL

By default, the browser will open with a blank tab. Use the C<start_url> option
to open the browser to the specified URL. More typically, the C<< ->get >>
method is use to navigate to URLs. Using C<start_url> means you don't
get notified when the URL has finished loading.

=item B<launch_arg>

Pass additional switches and parameters to the browser's executable:

  launch_arg => [ "--some-new-parameter=foo", "--another-option" ]

Examples of other useful parameters include:

    '--start-maximized',
    '--window-size=1280x1696'
    '--ignore-certificate-errors'

    '--disable-web-security',
    '--allow-running-insecure-content',

    '--load-extension'
    '--no-sandbox'

If you don't want the browser to use your OS password store, add:

    '--password-store=basic'

Also see
L<https://peter.sh/experiments/chromium-command-line-switches/>
for a list of command line arguments that Chrome actually has in the source
code.

Additional arguments for the command are also read from the
C<<$ENV{WWW_MECHANIZE_CHROME_ARGS}>>
variable and prepended to the C<launch_arg> array.


=item B<separate_session>

  separate_session => 1   # create a new, empty session

This creates an empty, fresh Chrome session without any cookies. Setting this
will disregard any B<data_directory> setting.

=item B<incognito>

  incognito => 1   # open the browser in incognito mode

Defaults to false. Set to true to launch the browser in incognito mode.

Most likely, you want to use B<separate_session> instead.

=item B<data_directory>

  data_directory => '/path/to/data/directory'  #  set the data directory

By default, an empty data directory is used. Use this setting to change the
base data directory for the browsing session.

  use File::Temp 'tempdir';
  # create a fresh Chrome every time
  my $mech = WWW::Mechanize::Chrome->new(
      data_directory => tempdir(CLEANUP => 1 ),
  );

Using the "main" Chrome cookies:

  my $mech = WWW::Mechanize::Chrome->new(
      data_directory => '/home/corion/.config/chromium',
  );

=item B<profile>

  profile => 'ProfileDirectory'  #  set the profile directory

By default, your current user profile directory is used. Use this setting
to change the profile directory for the browsing session.

You will need to set the B<data_directory> as well, so that Chrome finds the
profile within the data directory. The profile directory/name itself needs
to be a single directory name, not the full path. That single directory name
will be relative to the data directory.

=item B<wait_file>

  wait_file => "$tempdir/CrashpadMetrics-active.pma"

When shutting down, wait until this file does not exist anymore or can be
deleted. This can help making sure that the Chrome process has really shut
down.

=item B<startup_timeout>

  startup_timeout => 5  # set the startup timeout value

Defaults to 20, the maximum number of seconds to wait for the browser to launch.
Higher or lower values can be set based on the speed of the machine. The
process attempts to connect to the browser once each second over the duration
of this setting.

=item B<driver>

  driver => $driver_object  # specify the driver object

Use a L<Chrome::DevToolsProtocol::Target> object that has been manually constructed.

=item B<report_js_errors>

  report_js_errors => 1  # turn javascript error reporting on

lib/WWW/Mechanize/Chrome.pm  view on Meta::CPAN

implementation(s).

The C<< $ENV{WWW_MECHANIZE_CHROME_CONNECTION_STYLE} >> variable can be set to
either C<websocket> or C<pipe> to specify the kind of transport that you
want to use.

The C<pipe> transport is only available on unixish OSes and only with Chrome
v72 onwards.

=head1 METHODS

=cut

sub build_command_line {
    my( $class, $options )= @_;

    my @program_names = $class->default_executable_names( $options->{launch_exe} );

    my( $program, $error) = $class->find_executable(\@program_names);
    croak $error if ! $program;

    # Convert the path to an absolute filename, so we can chdir() later
    $program = File::Spec->rel2abs( $program ) || $program;

    my $is_root = ($> == 0);
    $options->{ no_sandbox } = 1
        if $is_root;     # We need this when running as root

    $options->{ launch_arg } //= [];
    $options->{ exclude_switches } ||= [];

    if( my $env = $ENV{WWW_MECHANIZE_CHROME_ARGS}) {
        unshift $options->{launch_arg}->@*, shellwords( $env );
    }

    # We want to read back the URL we can use to talk to Chrome
    if( $^O =~ /mswin/i ) {
        #push @{ $options->{launch_arg}}, '--v=0', '--enable-logging'; # v79 bad, v78 bad, v77 bad, v76 bad, v75 bad, v70 bad
        push @{ $options->{launch_arg}}, '--v=0'; # v79 OK, v62 OK, v61 bad
    };

    if( $options->{pipe}) {
        push @{ $options->{ launch_arg }}, "--remote-debugging-pipe";
    } else {

        $options->{port} //= 9222
            if ! exists $options->{port};

        if (exists $options->{port}) {
            $options->{port} ||= 0;
            push @{ $options->{ launch_arg }}, "--remote-debugging-port=$options->{ port }";
            push @{ $options->{ launch_arg }}, "--remote-allow-origins=*";
        };

        if ($options->{listen_host} || $options->{host} ) {
            my $host = $options->{listen_host} || $options->{host};
            push @{ $options->{ launch_arg }}, "--remote-debugging-address=$host";
        };
    };

    if ($options->{incognito}) {
        push @{ $options->{ launch_arg }}, "--incognito";
    };

    if ($options->{data_directory}) {
        push @{ $options->{ launch_arg }}, "--user-data-dir=$options->{ data_directory }";
    };

    if (my $profile = $options->{profile}) {
        if(! $options->{data_directory}) {
            croak "Cannot use the 'profile' option without also having 'data_directory'";
        } elsif( $profile =~ m![/\\]! ) {
            my $rel = File::Spec->rel2abs($profile, $options->{data_directory});
            if( $rel =~ m![/\\]!) {
                croak "The 'profile' option may not contain the path separator";
            } else {
                $profile = $rel;
            };
        }

        push @{ $options->{ launch_arg }}, "--profile-directory=$profile";
    };

    if( $options->{temp_profile}) {
        if( $options->{profile} ) {
            croak "Cannot use the 'profile' option together with 'temp_profile'";
        }
        push @{ $options->{ launch_arg }}, "--temp-profile";
    }

    if( $options->{silent_launch}) {
        push @{ $options->{ launch_arg }}, "--silent-launch";
    }

    if( $options->{enable_automation}) {
        push @{ $options->{ launch_arg }}, "--enable-automation";
    };

    if( $options->{infobars}) {
        push @{ $options->{ launch_arg }}, "--enable-infobars";
    };

    if( ! exists $options->{enable_first_run} || ! $options->{enable_first_run}) {
        push @{ $options->{ launch_arg }}, "--no-first-run";
    };

    if( ! exists $options->{mute_audio} || $options->{mute_audio}) {
        push @{ $options->{ launch_arg }}, "--mute-audio";
    };

    if( ! exists $options->{default_browser_check} || $options->{default_browser_check}) {
        push @{ $options->{ launch_arg }}, "--no-default-browser-check";
    };

    #my $no_sandbox = $options->{no_sandbox} || ! (exists $options->{no_zygote});
    if( $options->{no_zygote}) {
        push @{ $options->{ launch_arg }}, "--no-zygote";
    };

    #my $no_sandbox = $options->{no_sandbox} || ! (exists $options->{no_zygote});
    if( $options->{no_sandbox} || $is_root ) {
        push @{ $options->{ launch_arg }}, "--no-sandbox";



( run in 0.446 second using v1.01-cache-2.11-cpan-ceb78f64989 )