use WWW::Mechanize::Chrome; my $mech = WWW::Mechanize::Chrome->new( headless => 1 ); my $console = $mech->add_listener('Runtime.consoleAPICalled', sub { warn join ", ", map { $_->{value} // $_->{description} } @{ $_[0]->{params}->{args} }; }); $mech->get('https://google.com'); # SyntaxError: Illegal return statement at xx line 17. $mech->eval_in_page('if(1){console.log("aaa"); return;} console.log("bbb");'); # ReferenceError: exit is not defined $mech->eval_in_page('if(1){console.log("aaa"); exit;} console.log("bbb");'); # Works but is there anything simpler??? $mech->eval_in_page('function xxx(){if(1){console.log("aaa"); return;} console.log("bbb");} xxx();');