This class aims to launch Chromium or Chrome in headless mode. It possesses methods to manage connections to headless Chromium/Chrome using the Chrome Debugging Protocol.

Usage

remote <- Chrome$new(bin = NULL, debug_port = 9222L,
                     local = FALSE, extra_args = NULL, headless = TRUE,
                     retry_delay = 0.2, max_attempts = 15L)

remote$connect(callback = NULL)
remote$listConnections()
remote$closeConnections(callback = NULL)
remote$version()
remote$user_agent

remote$close(async = FALSE)
remote$view()
remote$is_alive()

Arguments

  • remote: Chrome object representing a remote instance of headless Chromium/Chrome.

  • bin: Character scalar, the path to Chromium or Chrome executable. If not provided, crrri will try to find the chrome binary itself using find_chrome_binary(). You can set a path in HEADLESS_CHROME environment variable to indicate where it is located.

  • debug_port: Integer scalar, the Chromium/Chrome remote debugging port. Note that headless Chromium/Chrome will be available at http://localhost:<debug_port>.

  • local: Logical scalar, indicating whether the local version of the protocol (embedded in crrri) must be used or the protocol must be fetched remotely.

  • extra_args: Character vector, extra command line arguments passed to Chromium/Chrome. You can know more about command line flags (or switches) from chromium developers

  • headless: Logical scalar, indicating whether Chromium/Chrome is launched in headless mode.

  • retry_delay: Number, delay in seconds between two successive tries to connect to headless Chromium/Chrome.

  • max_attempts: Integer scalar, number of tries to connect to headless Chromium/Chrome.

  • callback: Function with one argument.

  • async: Does the function return a promise?

Details

$new() opens a new headless Chromium/Chrome. You can deactivate verbose from chrome process launching byt setting option crrri.verbose to FALSE.

$connect(callback = NULL) connects the R session to the remote instance of headless Chromium/Chrome. The returned value depends on the value of the callback argument. When callback is a function, the returned value is a connection object. When callback is NULL the returned value is a promise which fulfills once R is connected to the remote instance of Chromium/Chrome. Once fulfilled, the value of this promise is the connection object.

$listConnections() returns a list of the connection objects succesfully created using the $connect() method.

$closeConnections(callback = NULL) closes all the connections created using the $connect() method. If callback is NULL, it returns a promise which fulfills when all the connections are closed: once fulfilled, its value is the remote object. If callback is not NULL, it returns the remote object. In this case, callback is called when all the connections are closed and the remote object is passed to this function as the argument.

$version() executes the DevTools Version method. It returns a list of informations available at http://localhost:<debug_port>/json/version.

$user_agent returns a character scalar with the User Agent of the headless Chromium/Chrome.

$close(async = FALSE) closes the remote instance of headless Chromium/Chrome. If async is FALSE this method returns the remote object invisibly. Is async is TRUE, a promise is returned. This promise fulfills when Chromium/Chrome is closed. Once fulfilled, its value is the remote object.

$view() opens a visible Chromium/Chrome browser at http://localhost:<debug_port>. This is useful to 'see' the headless Chromium/Chrome instance. Returns the process of the visible browser.

$is_alive() checks if the remote instance is alive. Returns a logical scalar.

$listTargets() returns a list with information about tabs.

Examples

if (FALSE) { remote <- Chrome$new() remote$connect() %...>% (function(client) { Page <- client$Page Runtime <- client$Runtime Page$enable() %...>% { Page$navigate(url = 'http://r-project.org') } %...>% { Page$loadEventFired() } %...>% { Runtime$evaluate( expression = 'document.documentElement.outerHTML' ) } %...>% (function(result) { cat(result$result$value, "\n") }) }) %...!% { cat("Error:", .$message, "\n") } %>% promises::finally(~ remote$close()) }