Giter Site home page Giter Site logo

chromote's Introduction

Chromote: Headless Chrome Remote Interface

R-CMD-check CRAN status Lifecycle: experimental

Chromote is an R implementation of the Chrome DevTools Protocol. It works with Chrome, Chromium, Opera, Vivaldi, and other browsers based on Chromium. By default it uses Google Chrome (which must already be installed on the system). To use a different browser, see Specifying which browser to use.

Chromote is not the only R package that implements the Chrome DevTools Protocol. Here are some others:

The interface to Chromote is similar to chrome-remote-interface for node.js.

Installation

# CRAN
install.packages("chromote")

# Development
remotes::install_github("rstudio/chromote")

Basic usage

This will start a headless browser and open an interactive viewer for it in a normal browser, so that you can see what the headless browser is doing.

library(chromote)

b <- ChromoteSession$new()

# In a web browser, open a viewer for the headless browser. Works best with
# Chromium-based browsers.
b$view()

The browser can be given commands, as specified by the Chrome DevTools Protocol. For example, $Browser$getVersion() (which corresponds to the Browser.getVersion in the API docs) will query the browser for version information:

b$Browser$getVersion()
#> $protocolVersion
#> [1] "1.3"
#>
#> $product
#> [1] "HeadlessChrome/98.0.4758.102"
#>
#> $revision
#> [1] "@273bf7ac8c909cde36982d27f66f3c70846a3718"
#>
#> $userAgent
#> [1] "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.102 Safari/537.36"
#>
#> $jsVersion
#> [1] "9.8.177.11"

If you have the viewer open and run the following, you’ll see the web page load in the viewer:

b$Page$navigate("https://www.r-project.org/")

In the official Chrome DevTools Protocol (CDP) documentation, this is the Page.navigate command.

In addition to full support of the CDP, ChromoteSession objects also some convenience methods, like $screenshot(). (See the Examples section below for more information about screenshots.)

# Saves to screenshot.png
b$screenshot()

# Takes a screenshot of elements picked out by CSS selector
b$screenshot("sidebar.png", selector = ".sidebar")

Note: All members of Chromote and ChromoteSession objects which start with a capital letter (like b$Page, b$DOM, and b$Browser) correspond to domains from the Chrome DevTools Protocol, and are documented in the official CDP site. All members which start with a lower-case letter (like b$screenshot and b$close) are not part of the Chrome DevTools Protocol, and are specific to Chromote and ChromoteSession.

Here is an example of how to use Chromote to find the position of a DOM element using DOM.getBoxModel.

x <- b$DOM$getDocument()
x <- b$DOM$querySelector(x$root$nodeId, ".sidebar")
x <- b$DOM$getBoxModel(x$nodeId)
str(x)
#> List of 1
#>  $ model:List of 6
#>   ..$ content:List of 8
#>   .. ..$ : num 128
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : num 988
#>   .. ..$ : num 128
#>   .. ..$ : num 988
#>   ..$ padding:List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ border :List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ margin :List of 8
#>   .. ..$ : int 15
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 1030
#>   .. ..$ : int 15
#>   .. ..$ : num 1030
#>   ..$ width  : int 195
#>   ..$ height : int 960

Or you can do the same thing by chaining commands together with a magrittr pipe:

b$DOM$getDocument() %>%
  { b$DOM$querySelector(.$root$nodeId, ".sidebar") } %>%
  { b$DOM$getBoxModel(.$nodeId) } %>%
  str()
#> List of 1
#>  $ model:List of 6
#>   ..$ content:List of 8
#>   .. ..$ : num 128
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : num 988
#>   .. ..$ : num 128
#>   .. ..$ : num 988
#>   ..$ padding:List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ border :List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ margin :List of 8
#>   .. ..$ : int 15
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 1030
#>   .. ..$ : int 15
#>   .. ..$ : num 1030
#>   ..$ width  : int 195
#>   ..$ height : int 960

Creating new tabs and managing the process

To create a new tab/window:

b1 <- b$new_session()

Once it’s created, you can perform operations with the new tab without affecting the first one.

b1$view()
b1$Page$navigate("https://github.com/rstudio/chromote")
#> $frameId
#> [1] "714439EBDD663E597658503C86F77B0B"
#>
#> $loaderId
#> [1] "F39339CBA7D1ACB83618FEF40C3C7467"

To close a browser tab/window, you can run:

b1$close()

This is different from shutting down the browser process. If you call b$close(), the browser process will still be running, even if all tabs have been closed. If all tabs have been closed, you can still create a new tab by calling b1$new_session().

To shut down the process, call:

b1$parent$close()

b1$parent is a Chromote object (as opposed to ChromoteSession), which represents the browser as a whole. This is explained in The Chromote object model.

Commands and Events

The Chrome DevTools Protocol has two types of methods: commands and events. The methods used in the previous examples are commands. That is, they tell the browser to do something; the browser does it, and then sends back some data.

Events are quite different from commands. When, for example, you run b$Page$loadEventFired(), it does not send a message to the browser. Rather, this method tells the R process to wait until it receives a Page.loadEventFired message from the browser.

Here is an example of how that event can be used. Note that these two lines of code must be run together, without any delay at all (this can be enforced by wrapping both lines of code in { .... }).

# Send a command to navigate to a page
b$Page$navigate("https://www.r-project.org")
#> $frameId
#> [1] "0ADE3CFBAF764B0308ADE1ACCC33358B"
#>
#> $loaderId
#> [1] "112AF4AC0C13FF4A95BED8173C3F4C7F"

# Wait for the Page.loadEventFired event
b$Page$loadEventFired()
#> $timestamp
#> [1] 680.7603

After running these two lines, the R process will be blocked. While it’s blocked, the browser will load the page, and then send a message to the R process saying that the Page.loadEventFired event has occurred. The message looks something like this:

{"method":"Page.loadEventFired","params":{"timestamp":699232.345338}}

After the R process receives this message, the function returns the value, which looks like this:

$timestamp
[1] 699232.3

Note: This sequence of commands, with Page$navigate() and then Page$loadEventFired() will not work 100% of the time. See Loading a Page Reliably for more information.

Technical note: Chromote insulates the user from some of the details of how the CDP implements event notifications. Event notifications are not sent from the browser to the R process by default; you must first send a command to enable event notifications for a domain. For example Page.enable enables event notifications for the Page domain – the browser will send messages for all Page events. (See the Events section in this page). These notifications will continue to be sent until the browser receives a Page.disable command.

By default, Chromote hides this implementation detail. When you call b$Page$loadEventFired(), Chromote sends a Page.enable command automatically, and then waits until it receives the Page.loadEventFired event notification. Then it sends a Page.disable command.

Note that in asynchronous mode, the behavior is slightly more sophisticated: it maintains a counter of how many outstanding events it is waiting for in a given domain. When that count goes from 0 to 1, it sends the X.enable command; when the count goes from 1 to 0, it sends the X.disable command. For more information, see the Async events section.

If you do not want automatic event enabling and disabling, then when creating the ChromoteSession object, use ChromoteSession$new(auto_events = FALSE).

The Chromote object model

There are two R6 classes that are used to represent the Chrome browser. One is Chromote, and the other is ChromoteSession. A Chromote object represents the browser as a whole, and it can have multiple targets, which each represent a browser tab. In the Chrome DevTools Protocol, each target can have one or more debugging sessions to control it. A ChromoteSession object represents a single session.

When a ChromoteSession object is instantiated, a target is created, then a session is attached to that target, and the ChromoteSession object represents the session. (It is possible, though not very useful, to have multiple ChromoteSession objects connected to the same target, each with a different session.)

A Chromote object can have any number of ChromoteSession objects as children. It is not necessary to create a Chromote object manually. You can simply call:

b <- ChromoteSession$new()

and it will automatically create a Chromote object if one has not already been created. The Chromote package will then designate that Chromote object as the default Chromote object for the package, so that any future calls to ChromoteSession$new() will automatically use the same Chromote. This is so that it doesn’t start a new browser for every ChromoteSession object that is created.

In the Chrome DevTools Protocol, most commands can be sent to individual sessions using the ChromoteSession object, but there are some commands which can only be sent to the overall browser, using the Chromote object.

To access the parent Chromote object from a ChromoteSession, you can simply use $parent:

b <- ChromoteSession$new()
m <- b$parent

With a Chromote object, you can get a list containing all the ChromoteSessions, with $get_sessions():

m$get_sessions()

Normally, subsequent calls to ChromoteSession$new() will use the existing Chromote object. However, if you want to start a new browser process, you can manually create a Chromote object, then spawn a session from it; or you can pass the new Chromote object to ChromoteSession$new():

cm <- Chromote$new()
b1 <- cm$new_session()

# Or:
b1 <- ChromoteSession$new(parent = cm)

Note that if you use either of these methods, the new Chromote object will not be set as the default that is used by future calls to ChromoteSesssion$new(). See Specifying which browser to use for information on setting the default Chromote object.

There are also the following classes which represent the browser at a lower level:

  • Browser: This represents an instance of a browser that supports the Chrome DevTools Protocol. It contains information about how to communicate with the Chrome browser. A Chromote object contains one of these.
  • Chrome: This is a subclass of Browser that represents a local browser. It extends the Browser class with a processx::process object, which represents the browser’s system process.
  • ChromeRemote: This is a subclass of Browser that represents a browser running on a remote system.

Debugging

Calling b$debug_messages(TRUE) will enable the printing of all the JSON messages sent between R and Chrome. This can be very helpful for understanding how the Chrome DevTools Protocol works.

b <- ChromoteSession$new()
b$parent$debug_messages(TRUE)
b$Page$navigate("https://www.r-project.org/")
#> SEND {"method":"Page.navigate","params":{"url":"https://www.r-project.org/"},"id":53,"sessionId":"12CB6B044A379DA0BDCFBBA55318247C"}
#> $frameId
#> [1] "BAAC175C67E55886207BADE1776E7B1F"
#>
#> $loaderId
#> [1] "66DED3DF9403DA4A307444765FDE828E"

# Disable debug messages
b$parent$debug_messages(FALSE)

Synchronous vs. asynchronous usage

By default, when you call methods from a Chromote or ChromoteSession object, it operates in synchronous mode. For example, when you call a command function (like b$Page$navigate()), a command message is sent to the headless browser, the headless browser executes that command, and it sends a response message back. When the R process receives the response, it converts it from JSON to an R object and the function returns that value. During this time, the R process is blocked; no other R code can execute.

The methods in Chromote/ChromoteSession objects can also be called in asynchronous mode. In async mode, a command function fires off a message to the browser, and then the R process continues running other code; when the response comes back at some time in the future, the R process calls another function and passes the response value to it.

There are two different ways of using async with Chromote. The first is with promises (note that these are not the regular R-language promises; these are similar to JavaScript promises for async programming.) The second way is with callbacks: you call methods with a callback_ argument. Although callbacks are initially easier to use than promises, once you start writing more complex code, managing callbacks becomes very difficult, especially when error handling is involved. For this reason, this document will focus mostly on promises instead of callback-style programming.

When Chromote methods are called in synchronous mode, under the hood, they are implemented with asynchronous functions, and then waiting for the asynchronous functions to resolve.

Technical note about the event loop: When methods are called asynchronously, the R process will run callbacks and promises using an event loop provided by the later package. This event loop is very similar to the one used in JavaScript, which is explained in depth by Philip Roberts in this video. One important difference between JavaScript’s event loop and the one provided by later’s is that in JavaScript, the event loop only runs when the call stack is empty (essentially, when the JS runtime is idle); with later the event loop similarly runs when the call stack is empty (when the R console is idle), but it can also be run at any point by calling later::run_now().

There is another important difference between the JS event loop and the one used by Chromote: Chromote uses private event loops provided by later. Running the private event loop with run_now() will not interfere with the global event loop. This is crucial for being able to run asynchronous code in a way that appears synchronous.

Why async?

The synchronous API is easier to use than the asynchronous one. So why would you want to use the async API? Here are some reasons:

  • The async API allows you to send commands to the browser that may take some time for the browser to complete, and they will not block the R process from doing other work while the browser executes the command.
  • The async API lets you send commands to multiple browser “tabs” and let them work in parallel.

On the other hand, async programming can make it difficult to write code that proceeds in a straightforward, linear manner. Async programming may be difficult to use in, say, an analysis script.

When using Chromote interactively at the R console, it’s usually best to just call methods synchronously. This fits well with a iterative, interactive data analysis workflow.

When you are programming with Chromote instead of using it interactively, it is in many cases better to call the methods asynchronously, because it allows for better performance. In a later section, we’ll see how to write asynchronous code with Chromote that can be run either synchronously or asynchronously. This provides the best of both worlds.

Async commands

When a method is called in synchronous mode, it blocks until the browser sends back a response, and then it returns the value, converted from JSON to an R object. For example:

# Synchronous
str(b$Browser$getVersion())
#> List of 5
#>  $ protocolVersion: chr "1.3"
#>  $ product        : chr "HeadlessChrome/98.0.4758.102"
#>  $ revision       : chr "@273bf7ac8c909cde36982d27f66f3c70846a3718"
#>  $ userAgent      : chr "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.102 Safari/537.36"
#>  $ jsVersion      : chr "9.8.177.11"

In async mode, there are two ways to use the value that the browser sends to the R process. One is to use the callback_ argument with wait_=FALSE. The wait_=FALSE tells it to run the command in async mode; instead of returning the value from the browser, it returns a promise. For example:

# Async with callback
b$Browser$getVersion(wait_ = FALSE, callback_ = str)
#> <Promise [pending]>
#> List of 5
#>  $ protocolVersion: chr "1.3"
#>  $ product        : chr "HeadlessChrome/98.0.4758.102"
#>  $ revision       : chr "@273bf7ac8c909cde36982d27f66f3c70846a3718"
#>  $ userAgent      : chr "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.102 Safari/537.36"
#>  $ jsVersion      : chr "9.8.177.11"

Notice that the function returned <Promise [pending]>, and then it printed out the data. We’ll come back to the promise part.

Technical note: When you pass a function as callback_, that function is used as the first step in the promise chain that is returned.

If you run the command in a code block (or a function), the entire code block will finish executing before the callback can be executed. For example:

{
  b$Browser$getVersion(wait_ = FALSE, callback_ = str)
  1+1
}
#> [1] 2
#> List of 5
#>  $ protocolVersion: chr "1.3"
#>  $ product        : chr "HeadlessChrome/98.0.4758.102"
#>  $ revision       : chr "@273bf7ac8c909cde36982d27f66f3c70846a3718"
#>  $ userAgent      : chr "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.102 Safari/537.36"
#>  $ jsVersion      : chr "9.8.177.11"

In the code above, it executes the 1+1 and returns the value before the str callback can be executed on the message from the browser.

If you want to store the value from the browser, you can write a callback that stores the value like so:

# This will extract the product field
product <- NULL
b$Browser$getVersion(wait_ = FALSE, callback_ = function(msg) {
  product <<- msg$product
})
#> <Promise [pending]>
# Wait for a moment, then run:
product
#> [1] "HeadlessChrome/98.0.4758.102"

But to get the value, you need to wait for the callback to execute before you can use the value. Waiting for a value is simple when running R interactively – you can just add a message("message arrived") call in the callback and wait for it before running the next line of code – but waiting for the value is not easy to do using ordinary straight-line coding. Fortunately, Chromote has a way to wait for async operations, which we’ll see later.

The other way of using the value is to use promises. If wait_=FALSE and no callback_ is passed to the command, then it will simply return a promise. Promises have many advantages over plain old callbacks: they are easier to chain, and they provide better error-handling capabilities. You can chain more steps to the promise: when the promise resolves – that is, when the message is received from the browser – it will run the next step in the promise chain.

Here’s an example that uses promises to print out the version information. Note that the surrounding curly braces are there to indicate that this whole thing must be run as a block without any idle time in between the function calls – if you were to run the code in the R console line-by-line, the browser would send back the message and the promise would resolve before you called p$then(), which is where you tell the promise what to do with the return value. (The curly braces aren’t strictly necessary – you could run the code inside the braces in a single paste operation and have the same effect.)

{
  p <- b$Browser$getVersion(wait_ = FALSE)
  p$then(function(value) {
    print(value$product)
  })
}
# Wait for a moment, then prints:
#> [1] "HeadlessChrome/98.0.4758.102"

Here are some progressively more concise ways of achieving the same thing. As you work with promises, you will see these various forms of promise chaining. For more information, see the promises documentation.

library(promises)

# Regular function pipe to then()
b$Browser$getVersion(wait_ = FALSE) %>% then(function(value) {
  print(value$product)
})

# Promise-pipe to anonymous function, which must be wrapped in parens
b$Browser$getVersion(wait_ = FALSE) %...>% (function(value) {
  print(value$product)
})

# Promise-pipe to an expression (which gets converted to a function with the first argument `.`)
b$Browser$getVersion(wait_ = FALSE) %...>% { print(.$product) }

# Promise-pipe to a named function, with parentheses
print_product <- function(msg) print(msg$product)
b$Browser$getVersion(wait_ = FALSE) %...>% print_product()

# Promise-pipe to a named function, without parentheses
b$Browser$getVersion(wait_ = FALSE) %...>% print_product

The earlier example where we found the dimensions of a DOM element using CSS selectors was done with the synchronous API and %>% pipes. The same can be done in async mode by switching from the regular pipe to the promise-pipe, and calling all the methods with wait_=FALSE:

b$DOM$getDocument(wait_ = FALSE) %...>%
  { b$DOM$querySelector(.$root$nodeId, ".sidebar", wait_ = FALSE) } %...>%
  { b$DOM$getBoxModel(.$nodeId, wait_ = FALSE) } %...>%
  str()


# Or, more verbosely:
b$DOM$getDocument(wait_ = FALSE)$
  then(function(value) {
    b$DOM$querySelector(value$root$nodeId, ".sidebar", wait_ = FALSE)
  })$
  then(function(value) {
    b$DOM$getBoxModel(value$nodeId, wait_ = FALSE)
  })$
  then(function(value) {
    str(value)
  })

Each step in the promise chain uses the value from the previous step, via . or value. Note that not all asynchronous code works in such a linear, straightforward way. Sometimes it is necessary to save data from intermediate steps in a broader-scoped variable, if it is to be used in a later step in the promise chain.

Turning asynchronous code into synchronous code

There may be times, especially when programming with Chromote, where you want to wait for a promise to resolve before continuing. To do this, you can use the Chromote or ChromoteSession’s wait_for() method.

# A promise chain
p <- b$DOM$getDocument(wait_ = FALSE) %...>%
  { b$DOM$querySelector(.$root$nodeId, ".sidebar", wait_ = FALSE) } %...>%
  { b$DOM$getBoxModel(.$nodeId, wait_ = FALSE) } %...>%
  str()

b$wait_for(p)
#> List of 1
#>  $ model:List of 6
#>   ..$ content:List of 8
#>   .. ..$ : num 128
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : num 988
#>   .. ..$ : num 128
#>   .. ..$ : num 988
#>   ..$ padding:List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ border :List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ margin :List of 8
#>   .. ..$ : int 15
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 1030
#>   .. ..$ : int 15
#>   .. ..$ : num 1030
#>   ..$ width  : int 195
#>   ..$ height : int 960

This documentation will refer to this technique as synchronizing asynchronous code. The way that wait_for() works is that it runs the Chromote object’s private event loop until the promise has resolved. Because the event loop is private, running it will not interfere with the global event loop, which, for example, may used by Shiny to serve a web application.

The $wait_for() method will return the value from the promise, so instead of putting the str() in the chain, you call str() on the value returned by $wait_for():

p <- b$DOM$getDocument(wait_ = FALSE) %...>%
  { b$DOM$querySelector(.$root$nodeId, ".sidebar", wait_ = FALSE) } %...>%
  { b$DOM$getBoxModel(.$nodeId, wait_ = FALSE) }

x <- b$wait_for(p)
str(x)
#> List of 1
#>  $ model:List of 6
#>   ..$ content:List of 8
#>   .. ..$ : num 128
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : int 28
#>   .. ..$ : num 292
#>   .. ..$ : num 988
#>   .. ..$ : num 128
#>   .. ..$ : num 988
#>   ..$ padding:List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ border :List of 8
#>   .. ..$ : num 112
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 988
#>   .. ..$ : num 112
#>   .. ..$ : num 988
#>   ..$ margin :List of 8
#>   .. ..$ : int 15
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : int 28
#>   .. ..$ : num 308
#>   .. ..$ : num 1030
#>   .. ..$ : int 15
#>   .. ..$ : num 1030
#>   ..$ width  : int 195
#>   ..$ height : int 960

There are some methods in Chromote and ChromoteSession objects which are written using asynchronous method calls, but conditionally use wait_for() so that they can be called either synchronously or asynchronously. The $screenshot() method works this way, for example. You can call b$screenshot(wait_=TRUE) (which is the default) for synchronous behavior, or b$screenshot(wait_=FALSE) for async behavior.

If you want to write a function that can be called in either sync or async mode, you can use this basic structure: First, construct a promise chain by calling the CDP methods with wait_=FALSE. Then, at the end, if the user used wait_=TRUE, wait for the promise to resolve; otherwise, simply return the promise.

getBoxModel <- function(b, selector = "html", wait_ = TRUE) {
  p <- b$DOM$getDocument(wait_ = FALSE) %...>%
    { b$DOM$querySelector(.$root$nodeId, selector, wait_ = FALSE) } %...>%
    { b$DOM$getBoxModel(.$nodeId, wait_ = FALSE) }

  if (wait_) {
    b$wait_for(p)
  } else {
    p
  }
}

# Synchronous call
str(getBoxModel(b, ".sidebar"))

# Asynchronous call
getBoxModel(b, ".sidebar", wait_ = FALSE) %...>%
  str()

But, you might be wondering, if we want a synchronous API, why not simply write the synchronous code by calling the individual methods synchronously, and using a normal pipe to connect them, as in:

b$DOM$getDocument() %>%
  { b$DOM$querySelector(.$root$nodeId, ".sidebar") } %>%
  { b$DOM$getBoxModel(.$nodeId) } %>%
  str()

There are two reasons for this. The first is that this would require a duplication of all the code for the sync and async code paths. Another reason is that the internal async code can be written to send multiple independent command chains to the ChromoteSession (or multiple ChromoteSessions), and they will be executed concurrently. If there are multiple promise chains, you can do something like the following to wait for all of them to resolve:

# Starting with promises p1, p2, and p3, create a promise that resolves only
# after they have all been resolved.
p <- promise_all(p1, p2, p3)
b$wait_for(p)

Async events

In addition to commands The Chrome DevTools Protocol also has events. These are messages that are sent from the browser to the R process when various browser events happen.

As an example, it can be a bit tricky to find out when to take a screenshot. When you send the browser a command to navigate to a page, it sends a response immediately, but it may take several more seconds for it to actually finish loading that page. When it does, the Page.loadEventFired event will be fired.

b <- ChromoteSession$new()

# Navigate and wait for Page.loadEventFired.
# Note: these lines are put in a single code block to ensure that there is no
# idle time in between.
{
  b$Page$navigate("https://www.r-project.org/")
  str(b$Page$loadEventFired())
}
#> List of 1
#>  $ timestamp: num 683

With the synchronous API, the call to b$Page$loadEventFired() will block until Chromote receives a Page.loadEventFired message from the browser. However, with the async promise API, you would write it like this:

b$Page$navigate("https://www.r-project.org/", wait_ = FALSE) %...>%
  { b$Page$loadEventFired(wait_ = FALSE) } %...>%
  { str(.) }

# Or, more verbosely:
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)$
  then(function(value) {
    b$Page$loadEventFired(wait_ = FALSE)
  })$
  then(function(value) {
    str(value)
  })

There will be a short delay after running the code before the value is printed.

However, even this is not perfectly reliable, because in some cases, the browser will navigate to the page before it receives the loadEventFired command from Chromote. If that happens, the load even will have already happened before the browser starts waiting for it, and it will hang. The correct way to deal with this is to issue the loadEventFired command before navigating to the page, and then wait for the loadEventFired promise to resolve.

# This is the correct way to wait for a page to load with async and then chain more commands
p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)

# A promise chain of more commands after the page has loaded
p$then(function(value) {
  str(value)
})

If you want to block until the page has loaded, you can once again use wait_for(). For example:

p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)

# wait_for returns the last value in the chain, so we can call str() on it
str(b$wait_for(p))
#> List of 1
#>  $ timestamp: num 683

Technical note: The Chrome DevTools Protocol itself does not automatically enable event notifications. Normally, you would have to call the Page.enable method to turn on event notifications for the Page domain. However, Chromote saves you from needing to do this step by keeping track of how many callbacks there are for each domain. When the number of event callbacks for a domain goes from 0 to 1, Chromote automatically calls $enable() for that domain, and when it goes from 1 to 0, it it calls $disable().

In addition to async events with promises, they can also be used with regular callbacks. For example:

b$Page$loadEventFired(callback_ = str)

This tells Chromote to call str() (which prints to the console) on the message value every single time that a Page.loadEventFired event message is received. It will continue doing this indefinitely. (Calling an event method this way also increments the event callback counter.)

When an event method is called with a callback, the return value is a function which will cancel the callback, so that it will no longer fire. (The canceller function also decrements the event callback counter. If you lose the canceller function, there is no way to decrement the callback counter back to 0.)

cancel_load_event_callback <- b$Page$loadEventFired(callback_ = str)

# Each of these will cause the callback to fire.
n1 <- b$Page$navigate("https://www.r-project.org/")
n2 <- b$Page$navigate("https://cran.r-project.org/")

cancel_load_event_callback()

# No longer causes the callback to fire.
n3 <- b$Page$navigate("https://www.rstudio.com/")

Resource cleanup and garbage collection

When Chromote starts a Chrome process, it calls Chrome$new(). This launches the Chrome process it using processx::process(), and enables a supervisor for the process. This means that if the R process stops, the supervisor will detect this and shut down any Chrome processes that were registered with the supervisor. This prevents the proliferation of Chrome processes that are no longer needed.

The Chromote package will, by default, use a single Chrome process and a single Chromote object, and each time ChromoteSession$new() is called, it will spawn them from the Chromote object. See The Chromote object model for more information.

Specifying which browser to use

Chromote will look in specific places for the Chrome web browser, depending on platform. This is done by the chromote:::find_chrome() function.

If you wish to use a different browser from the default, you can set the CHROMOTE_CHROME environment variable, either with Sys.setenv(CHROMOTE_CHROME="/path/to/browser").

Sys.setenv(CHROMOTE_CHROME = "/Applications/Chromium.app/Contents/MacOS/Chromium")

b <- ChromoteSession$new()
b$view()
b$Page$navigate("https://www.whatismybrowser.com/")

Another way is create a Chromote object and explicitly specify the browser, then spawn ChromoteSessions from it.

m <- Chromote$new(
  browser = Chrome$new(path = "/Applications/Chromium.app/Contents/MacOS/Chromium")
)

# Spawn a ChromoteSession from the Chromote object
b <- m$new_session()
b$Page$navigate("https://www.whatismybrowser.com/")

Yet another way is to create a Chromote object with a specified browser, then set it as the default Chromote object.

m <- Chromote$new(
  browser = Chrome$new(path = "/Applications/Chromium.app/Contents/MacOS/Chromium")
)

# Set this Chromote object as the default. Then any
# ChromoteSession$new() will be spawned from it.
set_default_chromote_object(m)
b <- ChromoteSession$new()
b$view()
b$Page$navigate("https://www.whatismybrowser.com/")

Chrome on remote hosts

Chromote can control a browser running on a remote host. To start the browser, open a terminal on the remote host and run one of the following, depending on your platform:

Warning: Depending on how the remote machine is configured, the Chrome debug server might be accessible to anyone on the Internet. Proceed with caution.

# Mac
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless \
  --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222

# Linux
google-chrome --headless --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222

# Windows
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  --headless \
  --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222

Then, in your local R session, create a Chromote object with the host and port (you will need to use the correct IP address). Once it’s created, you can spawn a session off of it which you can control as normal:

r <- Chromote$new(
  browser = ChromeRemote$new(host = "10.0.0.5", port = 9222)
)

b <- r$new_session()

b$Browser$getVersion()
b$view()
b$Page$navigate("https://www.whatismybrowser.com/")
b$Page$loadEventFired()
b$screenshot("browser.png")
b$screenshot("browser_string.png", selector = ".string-major")

When you use $view() on the remote browser, your local browser may block scripts for security reasons, which means that you won’t be able to view the remote browser. If your local browser is Chrome, there will be a shield-shaped icon in the location bar that you can click in order to enable loading the scripts. (Note: Some browsers don’t seem to work at all with the viewer.)

Technical note: There seem to be some timing issues with remote browsers. In the example above, the browser may finish navigating to the web site before the R process receives the response message for $navigate(), and therefore before R starts waiting for Page.loadEventFired. In order to avoid these timing problems, it is better to write code like this:

{
  p <- b$Page$loadEventFired(wait_ = FALSE)
  b$Page$navigate("https://www.whatismybrowser.com/", wait_ = FALSE)
  b$wait_for(p)
}
b$screenshot("browser.png")

This tells it to fire off the Page.navigate command and not wait for it, and then immediately start waiting for Page.loadEventFired event.

Attaching to existing tabs

In the examples above, we connected to an existing browser, but created a new tab to attach to. It’s also possible to attach to an existing browser and and existing tab. In Chrome debugging terminology a tab is called a “Target”, and there is a command to retrieve the list of current Targets:

r$Target$getTargets()

Every target has a unique identifier string associated with it called the targetId; "9DAE349A3A533718ED9E17441BA5159B" is an example of one.

Here we define a function that retrieves the ID of the first Target (tab) from a Chromote object:

first_id <- function(r) {
  ts <- r$Target$getTargets()$targetInfos
  stopifnot(length(ts) > 0)
  r$Target$getTargets()$targetInfos[[1]]$targetId
}

The following code shows an alert box in the first tab, whatever it is:

rc <- ChromeRemote$new(host = "localhost", port = 9222)
r <- Chromote$new(browser = rc)
tid <- first_id(r)
b <- r$new_session(targetId = tid)
b$Runtime$evaluate('alert("this is the first tab")')

Examples

Loading a page reliably

In many cases, the commands Page$navigate() and then $Page$loadEventFired() will not reliably block until the page loads. For example:

# Not reliable
b$Page$navigate("https://www.r-project.org/")
b$Page$loadEventFired()  # Block until page has loaded

This is because the browser might successfully navigate to the page before it receives the loadEventFired command from R.

In order to navigate to a page reliably, you must issue the loadEventFired command first in async mode, then issue the navigate command, and then wait for the loadEventFired promise to resolve. (If it has already resolved at this point, then the code will continue.)

# Reliable method 1: for use with synchronous API
p <- b$Page$loadEventFired(wait_ = FALSE)  # Get the promise for the loadEventFired
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)

# Block until p resolves
b$wait_for(p)

# Add more synchronous commands here
b$screenshot("browser.png")

The above code uses the async API to do the waiting, but then assumes that you want to write subsequent code with the synchronous API.

If you want to go fully async, then instead of calling wait_for(p), you would simply chain more promises from p, using $then().

# Reliable method 2: for use with asynchronous API
p <- b$Page$loadEventFired(wait_ = FALSE)  # Get the promise for the loadEventFired
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)

# Chain more async commands after the page has loaded
p$then(function(value) {
  b$screenshot("browser.png", wait_ = FALSE)
})

This is explained in more detail in the Async Events section.

Taking a screenshot of a web page

Take a screenshot of the viewport and display it using the showimage package. This uses Chromote’s $screenshot() method, which wraps up many calls to the Chrome DevTools Protocol.

b <- ChromoteSession$new()

# ==== Semi-synchronous version ====
# Run the next two lines together, without any delay in between.
p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)
b$wait_for(p)
b$screenshot(show = TRUE)  # Saves to screenshot.png and displays in viewer

# ==== Async version ====
p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("https://www.r-project.org/", wait_ = FALSE)
p$then(function(value) {
    b$screenshot(show = TRUE)
  })

It is also possible to use selectors to specify what to screenshot, as well as the region (“content”, “border”, “padding”, or “margin”).

# Using CSS selectors, choosing the region, and using scaling
b$screenshot("s1.png", selector = ".sidebar")
b$screenshot("s2.png", selector = ".sidebar", region = "margin")
b$screenshot("s3.png", selector = ".page", region = "margin", scale = 2)

If a vector is passed to selector, it will take a screenshot with a rectangle that encompasses all the DOM elements picked out by the selectors. Similarly, if a selector picks out multiple DOM elements, all of them will be in the screenshot region.

Setting width and height of the viewport (window)

The default size of a ChromoteSession viewport is 992 by 1323 pixels. You can set the width and height when it is created:

b <- ChromoteSession$new(width = 390, height = 844)

b$Page$navigate("https://www.r-project.org/")
b$screenshot("narrow.png")

With an existing ChromoteSession, you can set the size with b$Emulation$setVisibleSize():

b$Emulation$setVisibleSize(width=1600, height=900)
b$screenshot("wide.png")

You can take a “Retina” (double) resolution screenshot by using b$screenshot(scale=2):

b$screenshot("wide-2x.png", scale = 2)

Taking a screenshot of a web page after interacting with it

Headless Chrome provides a remote debugging UI which you can use to interact with the web page. The ChromoteSession’s $view() method opens a regular browser and navigates to the remote debugging UI.

b <- ChromoteSession$new()

b$view()
b$Page$navigate("https://www.google.com") # Or just type the URL in the navigation bar

At this point, you can interact with the web page by typing in text and clicking on things.

Then take a screenshot:

b$screenshot()

Taking screenshots of web pages in parallel

With async code, it’s possible to navigate to and take screenshots of multiple websites in parallel.

library(promises)
library(chromote)
urls <- c(
  "https://www.r-project.org/",
  "https://github.com/",
  "https://news.ycombinator.com/"
)

screenshot_p <- function(url, filename = NULL) {
  if (is.null(filename)) {
    filename <- gsub("^.*://", "", url)
    filename <- gsub("/", "_", filename)
    filename <- gsub("\\.", "_", filename)
    filename <- sub("_$", "", filename)
    filename <- paste0(filename, ".png")
  }

  b <- ChromoteSession$new()
  p <- b$Page$loadEventFired(wait_ = FALSE)
  b$Page$navigate(url, wait_ = FALSE)
  p$then(function(value) {
      b$screenshot(filename, wait_ = FALSE)
    })$
    then(function(value) {
      message(filename)
    })$
    finally(function() {
      b$close()
    })
}

# Screenshot multiple simultaneously
ps <- lapply(urls, screenshot_p)
pa <- promise_all(.list = ps)$then(function(value) {
  message("Done!")
})

# Block the console until the screenshots finish (optional)
cm <- default_chromote_object()
cm$wait_for(pa)
#> www_r-project_org.png
#> github_com.png
#> news_ycombinator_com.png
#> Done!

Setting custom headers

Currently setting custom headers requires a little extra work because it requires Network.enable be called before using it. In the future we’ll streamline things so that it will happen automatically.

b <- ChromoteSession$new()
# Currently need to manually enable Network domain notifications. Calling
# b$Network$enable() would do it, but calling it directly will bypass the
# callback counting and the notifications could get automatically disabled by a
# different Network event. We'll enable notifications for the Network domain by
# listening for a particular event. We'll also store a callback that will
# decrement the callback counter, so that we can disable notifications ater.
disable_network_notifications <- b$Network$responseReceived(function (msg) NULL)
b$Network$setExtraHTTPHeaders(headers = list(
  foo = "bar",
  header1 = "value1"
))

# Visit a web page that prints out the request headers
b$Page$navigate("http://scooterlabs.com/echo")
b$screenshot(show = TRUE)


# Unset extra headers. Note that `list(a=1)[0]` creates an empty _named_ list;
# an empty unnamed list will cause an error because they're converted to JSON
# differently. A named list becomes "{}", but an unnamed list becomes "[]".
b$Network$setExtraHTTPHeaders(headers = list(a=1)[0])

# Request again
b$Page$navigate("http://scooterlabs.com/echo")
b$screenshot(show = TRUE)


# Disable extra headers entirely, by decrementing Network callback counter,
# which will disable Network notifications.
disable_network_notifications()

Custom User-Agent

Synchronous version:

# ==== Synchronous version ====
b$Network$setUserAgentOverride(userAgent = "My fake browser")

b$Page$navigate("http://scooterlabs.com/echo")
b$screenshot(show = TRUE)


# ==== Async version ====
b$Network$setUserAgentOverride(userAgent = "My fake browser", wait_ = FALSE)
p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("http://scooterlabs.com/echo", wait_ = FALSE)
p$then(function(value) {
    b$screenshot(show = TRUE)
  })

Extracting text from a web page

One way to extract text from a page is to tell the browser to run JavaScript code that does it:

# ==== Synchronous version ====
b$Page$navigate("https://www.whatismybrowser.com/")

# Run JavaScript to extract text from the page
x <- b$Runtime$evaluate('document.querySelector(".corset .string-major a").innerText')
x$result$value
#> [1] "Chrome 75 on macOS (Mojave)"


# ==== Async version ====
p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("https://www.whatismybrowser.com/", wait_ = FALSE)
p$then(function(value) {
    b$Runtime$evaluate(
      'document.querySelector(".corset .string-major a").innerText'
    )
  })$
  then(function(value) {
    print(value$result$value)
  })

Another way is to use CDP commands to extract content from the DOM. This does not require executing JavaScript in the browser’s context, but it is also not as flexible as JavaScript.

# ==== Synchronous version ====
b$Page$navigate("https://www.whatismybrowser.com/")
x <- b$DOM$getDocument()
x <- b$DOM$querySelector(x$root$nodeId, ".corset .string-major a")
b$DOM$getOuterHTML(x$nodeId)
#> $outerHTML
#> [1] "<a href=\"/detect/what-version-of-chrome-do-i-have\">Chrome 75 on macOS (Mojave)</a>"


# ==== Async version ====
p <- b$Page$loadEventFired(wait_ = FALSE)
b$Page$navigate("https://www.whatismybrowser.com/", wait_ = FALSE)
p$then(function(value) {
    b$DOM$getDocument()
  })$
  then(function(value) {
    b$DOM$querySelector(value$root$nodeId, ".corset .string-major a")
  })$
  then(function(value) {
    b$DOM$getOuterHTML(value$nodeId)
  })$
  then(function(value) {
    print(value)
  })

Websites that require authentication

For websites that require authentication, you can use Chromote to get screenshots by doing the following:

  1. Log in interactively and navigate to the page.
  2. Capture cookies from the page and save them.
  3. In a later R session, load the cookies.
  4. Use the cookies in Chromote and navigate to the page.
  5. Take a screenshot.

There are two ways to capture the cookies.

Method 1: The first method uses the headless browser’s viewer. This can be a bit inconvenient because it requires going through the entire login process, even if you have already logged in with a normal browser.

First navigate to the page:

library(chromote)
b <- ChromoteSession$new()
b$view()
b$Page$navigate("https://beta.rstudioconnect.com/content/123456/")

Next, log in interactively via the viewer. Once that’s done, use Chromote to capture the cookies.

cookies <- b$Network$getCookies()
str(cookies)
saveRDS(cookies, "cookies.rds")

After saving the cookies, you can restart R and navigate to the page, using the cookies.

library(chromote)
b <- ChromoteSession$new()
b$view()
cookies <- readRDS("cookies.rds")
b$Network$setCookies(cookies = cookies$cookies)
# Navigate to the app that requires a login
b$Page$navigate("https://beta.rstudioconnect.com/content/123456/")
b$screenshot()

Method 2: The second method captures the cookies using a normal browser. This is can be more convenient because, if you are already logged in, you don’t need to do it again. This requires a Chromium-based browser, and it requires running DevTools-in-DevTools on that browser.

First, navigate to the page in your browser. Then press CMD-Option-I (Mac) or Ctrl-Shift-I (Windows/Linux). The developer tools panel will open. Make sure to undock the developer tools so that they are in their own window. Then press CMD-Option-I or Ctrl-Shift-I again. A second developer tools window will open. (See this SO answer for detailed instructions.)

In the second developer tools window, run the following:

var cookies = await Main.sendOverProtocol('Network.getCookies', {})
JSON.stringify(cookies)

This will return a JSON string representing the cookies for that page. For example:

[{"cookies":[{"name":"AWSALB","value":"T3dNdcdnMasdf/cNn0j+JHMVkZ3RI8mitnAggd9AlPsaWJdsfoaje/OowIh0qe3dDPiHc0mSafe5jNH+1Aeinfalsd30AejBZDYwE","domain":"beta.rstudioconnect.com","path":"/","expires":1594632233.96943,"size":130,"httpOnly":false,"secure":false,"session":false}]}]

Copy that string to the clipboard. In your R session, you can paste it to this code, surrounded by single-quotes:

cookie_json <- '[{"cookies":[{"name":"AWSALB","value":"T3dNdcdnMasdf/cNn0j+JHMVkZ3RI8mitnAggd9AlPsaWJdsfoaje/OowIh0qe3dDPiHc0mSafe5jNH+1Aeinfalsd30AejBZDYwE","domain":"beta.rstudioconnect.com","path":"/","expires":1594632233.96943,"size":130,"httpOnly":false,"secure":false,"session":false}]}]'

cookies <- jsonlite::fromJSON(cookie_json, simplifyVector = FALSE)[[1]]

Then you can use Chromote to navigate to the page and take a screenshot.

library(chromote)
b <- ChromoteSession$new()
b$view()
b$Network$setCookies(cookies = cookies$cookies)
b$Page$navigate("https://beta.rstudioconnect.com/content/123456/")
b$screenshot()

chromote's People

Contributors

actions-user avatar alandipert avatar bersbersbers avatar colearendt avatar gadenbuie avatar hadley avatar nstrayer avatar olivroy avatar rlesur avatar schloerke avatar stla avatar wch avatar yutannihilation avatar zeloff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chromote's Issues

Chromote not working on windows 10

Hi @schloerke,

I hope all is good. Thanks for your work on this package.
I don't know what changed but I now have an issue when I do this on Windows

library(chromote)
Sys.getenv("CHROMOTE_CHROME")
## [1] "C:\\\\Program Files\\\\BraveSoftware\\\\Brave-Browser\\\\Application\\\\brave.exe"

b <- ChromoteSession$new()
b$view()
## [1] 127
## Warning message:
## In system(paste0("\"", browser, "\" ", if (encodeIfNeeded) URLencode(url) else url),  :
##   '""C:\\Program' not found

I was working fine couple of month ago. I only have this issue on windows, it's working well on Linux.
I suspect that it's related to the path and I think it's related to this

browseURL(url, shQuote(browser$get_path()))

Using just browseURL works, but since get_path is private, I can't use it for my test.

Can someone else can reproduce this error?

Thanks for the support,
Ahmadou

Idle CPU usage

When a Chromote session is started but idle, the R process consumes about 3% CPU on my computer. Should figure out what the cause is and how to improve that.

Feature request: environmental variable or options to specify chromium's argument

As discussed in #5, chromote on docker requires --no-sandbox option.
Here, I want to notice that #5 may cause errors on packages depending on chromote.
For example, webshot2::webshot cannot specify --no-sandbox, and thus fails on docker.
I opened this issue here instead of webshot2 because I don't want individual packages to take care of this issue.

I'd appreciate if we can set chromium's options, including --no-sandbox, via environmental variable or options on R.

As a workaround, I made a following shell script and specified it to the CHROMOTE_CHROMIUM environmental variable.

#! /bin/sh
exec /usr/bin/chromium --no-sandbox --headless "$@"

Cannot launch chromote session from behind firewall

When I launch

chromote::ChromoteSession$new()

in Rstudio on Windows 10

I get the following error

Error in open.connection(con, "rb") :
Failed to connect to proxyzscaler.xxx port 1080: Connection refused

Export `find_chrome` or implement `is_chrome_available`

On github.com/yihui/knitr/pull/1918#discussion_r566879595, we discussed that it would be nice to implement find_chrome or implement is_chrome_available.
Then, other packages may check is chrome is available, and fallback to other methods when chrome is unavailable.
We also think it would be nice if find_chrome is re-exported by webshot2 so that we have a function similar to webshot::find_phantomjs.

cc. @cderv

Errors received after sleeping computer

Only after my computer goes to sleep and wake back up....

When trying to close an R session where some Chromote Session objects have been created, I run many the printed errors saying:

Error: Chromote: timed out waiting for response to command Target.getTargetInfo

I'm wondering if the finalizer or cleanup method should check to make sure the session is alive?

Extract first element of CSS

Thanks for this great package. Can't fetch first element of CSS.

library(shiny)
runExample("01_hello", port = 1234)

Another Session

library(chromote)
b <- ChromoteSession$new()
b$view() # Opens a viewer to the headless browser, in a regular browser window
b$Page$navigate("http://localhost:1234")
b$screenshot("sidebar.png", selector = ".col-sm-4:nth-of-type(1)")

(Why) does Chromote open two Chrome windows by default?

> b <- chromote::ChromoteSession$new()
> b$Browser$getWindowBounds(1)
$bounds
$bounds$left
[1] 0

$bounds$top
[1] 0

$bounds$width
[1] 800

$bounds$height
[1] 600

$bounds$windowState
[1] "normal"


> b$Browser$getWindowBounds(2)
$bounds
$bounds$left
[1] 0

$bounds$top
[1] 0

$bounds$width
[1] 992

$bounds$height
[1] 1323

$bounds$windowState
[1] "normal"


> b$Browser$getWindowBounds(3)
Error in onRejected(reason) : code: -32000
  message: Browser window not found

Chromote not compatible with parallel and foreach packages

Thanks for this useful package. Chromote seems not to work with parallel and foreach packages. See the example below.
I am getting the error Error in unserialize(socklist[[n]]) : error reading from connection

b <- chromote::ChromoteSession$new()
Sys.sleep(2)

library(parallel)
library(foreach)
library(doSNOW)
cluster = makeCluster(3, type = "SOCK")
registerDoSNOW(cluster)

sites <- c("https://www.google.com/", "https://stackoverflow.com/", 
           "https://www.facebook.com/")

abstracts <- function(attributes) {
  x <- b$DOM$getDocument()$root$baseURL
  return(x)
}

research_overall <- foreach(i = 1:length(sites), 
                              .combine=c, 
                              .packages= c('chromote','rvest', 'dplyr')) %dopar% abstracts(sites[i]) 
  

If I modify the above function which does not use chromote, it works -

abstracts <- function(attributes) {
  x <- xml2::read_html(attributes)
  return(x)
}

expose 10 second timeout as option

I know 10 seconds feels like a long time, but here is the issue we are currently facing:

On newly provisioned AMI's, chrome can take closer to 15-20 seconds to launch the first time the ami is run, hence we are continually running into the timeout error on https://github.com/rstudio/chromote/blob/master/R/chromote.R#L62

It would be great to expose this as an option (and I'd be happy to add as a PR), so for those of us sorry souls that have to wait that long the first time this doesn't force other workarounds.

 p <- promise_resolve(TRUE)$
          then(function(value) {
            promise_timeout(
              promise(function(resolve, reject) {
                private$ws$onOpen(resolve)
              }),
-              10,
+              getOption("chromote.websocket_timeout", 10),
              timeout_message = "Chromote: timed out waiting for WebSocket connection to browser."
            )
          })

devtools disconnected after 10 min of activity

Hi,
any fix or a solution to this? please find below the error message:
{
*** caught segfault ***
address 0x0, cause 'memory not mapped'
Process was terminated

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
}

Move `master` branch to `main`

Cc @schloerke

The master branch of this repository will soon be renamed to main, as part of a coordinated change across several GitHub organizations (including, but not limited to: tidyverse, r-lib, tidymodels, and sol-eng). We anticipate this will happen by the end of September 2021.

That will be preceded by a release of the usethis package, which will gain some functionality around detecting and adapting to a renamed default branch. There will also be a blog post at the time of this master --> main change.

The purpose of this issue is to:

  • Help us firm up the list of targetted repositories
  • Make sure all maintainers are aware of what's coming
  • Give us an issue to close when the job is done
  • Give us a place to put advice for collaborators re: how to adapt

message id: entire_lizard

Can't seem to start Profiler

b <- Chromote$new()
b$Profiler$start()
Unhandled promise error: code: -32000
  message: Profiler is not enabled

Any idea how I can enable the profiler?

Does not work with Github Actions

Thanks for this great package. The code below works with local windows OS but returns blank when used in Github Actions. It does not throw any error. Does it require any dependency other than installing chromote? I tried with removing b$view() and using same version of R and chromote in both the environments

b <- ChromoteSession$new() 
b$view()
b$Page$navigate("https://www.cowin.gov.in/home/")
Sys.sleep(8)
b$Runtime$evaluate('document.querySelector("div.pin-search > input").focus()')

Extract Data from Highcharts

I am trying to pull data from highcharts line chart. I am getting this error Highcharts is not defined. I tried as well to include external script from highcharts before scraping data but it didn't work. I found highcharts JS file is being loaded in the page dynamically.

Release to CRAN

Creating an issue to track the initial package release to CRAN. This will be helpful so other repositories can track their dependencies on this issue / monitor for updates 😄

Preferences support

Does chromote support prefs like RSelenium? See example of prefs. Here it translates web page to English.

library(RSelenium)
shell('docker run -d -p 4445:4444 selenium/standalone-chrome')

eCaps <- list(chromeOptions = list(args = c('--disable-gpu','--window-size=1920,1080', '--lang=en'),
                                   prefs = list(translate_whitelists=list('ru' = 'en'),
                                                translate=list('enabled'='true'))))
                                                
remDr <- RSelenium::remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "chrome", extraCapabilities = eCaps)

remDr$open(silent = TRUE)
remDr$navigate("http://premier.gov.ru/events/")

I found args in Chrome$new( ) but could not find prefs.

a <- chromote::Chromote$new(
  browser = chromote::Chrome$new(
    path = chromote::find_chrome(),
    args = c('--start-maximized')
  )
)

b <- a$new_session()
b$Page$navigate("https://www.google.com/")
b$view()

Nicer error message if Chrome isn't installed

If the platform is determined but the Chrome executable is not found, the following error message is printed:

Error in process_initialize(self, private, command, args, stdin, stdout,  : 
  processx error: 'No such file or directory' at unix/processx.c:454

It would be nice if instead the error message was something like:

Chrome executable 'google-chrome-stable' not found. 

Please set the CHROMOTE_CHROME environment variable 
or install Google Chrome: https://www.google.com/chrome/b/

dispatchMouseEvent hanging

The following code is attempting to simulate a click on a link. Based on documentation I've read for Chrome devtools protocol this should be possible with a "mousePressed" followed by a "mouseReleased" event.

When running the following code the first dispatchMouseEvent call hangs and we never get to the 2nd call. If we leave off the button argument everything runs but nothing happens, the same thing happens if we use buttons=1.

The dispatchMouseEvent with "mouseReleased" runs without hanging.

library(chromote)

b = ChromoteSession$new()
b$parent$debug_messages(TRUE)
b$view()

b$Page$navigate("https://example.com")

doc = b$DOM$getDocument()
node = b$DOM$querySelector(doc$root$nodeId, "a")
box = b$DOM$getBoxModel(node$nodeId)

br = box$model$border
x = (br[[1]] + br[[5]])/2
y = (br[[2]] + br[[6]])/2

b$Input$dispatchMouseEvent(type = "mousePressed", x = x, y = y, button="left") # This line hangs
b$Input$dispatchMouseEvent(type = "mouseReleased", x = x, y = y, button="left")

Session info:

> devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       macOS Catalina 10.15.3      
 system   x86_64, darwin19.3.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       Europe/London               
 date     2020-04-24                  

─ Packages ───────────────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                           
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                   
 backports     1.1.6      2020-04-05 [1] CRAN (R 3.6.3)                   
 callr         3.4.3      2020-03-28 [1] CRAN (R 3.6.3)                   
 chromote    * 0.0.0.9001 2020-04-23 [1] Github (rstudio/chromote@a28092d)
 cli           2.0.2      2020-02-28 [1] CRAN (R 3.6.3)                   
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                   
 curl          4.3        2019-12-02 [1] CRAN (R 3.6.2)                   
 desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)                   
 devtools      2.3.0      2020-04-10 [1] CRAN (R 3.6.3)                   
 digest        0.6.25     2020-02-23 [1] CRAN (R 3.6.2)                   
 ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.2)                   
 evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)                   
 fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.2)                   
 fastmap       1.0.1      2019-10-08 [1] CRAN (R 3.6.2)                   
 fs            1.4.1      2020-04-04 [1] CRAN (R 3.6.3)                   
 glue          1.4.0      2020-04-03 [1] CRAN (R 3.6.3)                   
 htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.2)                   
 jsonlite      1.6.1      2020-02-02 [1] CRAN (R 3.6.2)                   
 knitr         1.28       2020-02-06 [1] CRAN (R 3.6.2)                   
 later         1.0.0.9002 2020-02-24 [1] Github (r-lib/later@0fb877a)     
 magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                   
 memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.2)                   
 packrat       0.5.0      2018-11-14 [1] CRAN (R 3.6.0)                   
 pkgbuild      1.0.6      2019-10-09 [1] CRAN (R 3.6.2)                   
 pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.6.0)                   
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 3.6.2)                   
 processx      3.4.2      2020-02-09 [1] CRAN (R 3.6.2)                   
 promises      1.1.0.9000 2020-04-23 [1] Github (rstudio/promises@dfc2754)
 ps            1.3.2      2020-02-13 [1] CRAN (R 3.6.2)                   
 R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.2)                   
 Rcpp          1.0.4.6    2020-04-09 [1] CRAN (R 3.6.3)                   
 remotes       2.1.1      2020-02-15 [1] CRAN (R 3.6.2)                   
 rlang         0.4.5      2020-03-01 [1] CRAN (R 3.6.3)                   
 rmarkdown     2.1        2020-01-20 [1] CRAN (R 3.6.2)                   
 rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)                   
 rstudioapi    0.11       2020-02-07 [1] CRAN (R 3.6.2)                   
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                   
 testthat      2.3.2      2020-03-02 [1] CRAN (R 3.6.3)                   
 usethis       1.6.0      2020-04-09 [1] CRAN (R 3.6.3)                   
 websocket     1.1.0      2019-08-08 [1] CRAN (R 3.6.3)                   
 withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                   
 xfun          0.13       2020-04-13 [1] CRAN (R 3.6.3)                   
 yaml          2.2.1      2020-02-01 [1] CRAN (R 3.6.2) 

Wants to extract the html of the whole document but kept getting no node found

Hi there,

Thanks for creating the package. I think it is super awesome and will be useful for webscrapping.
I kept getting this error:

'Error in onRejected(reason) : code: -32000
message: No node found for given backend id'

My sample code is:

`
library(chromote)

b <- ChromoteSession$new()

b$view()

b$Page$navigate("https://sc.macromicro.me/charts/449/us-cboe-options-put-call-ratio",wait_ = TRUE) #as they block CORS

Sys.sleep(3)

b$Page$navigate("https://sc.macromicro.me/charts/data/449",wait_ = TRUE)

data <- b$DOM$getDocument()

data_content <- b$DOM$getOuterHTML(data$root$nodeId)

json_data <- stringr::str_extract_all(data_content,'\{(.*|\n)?\}')[[1]]

json_data <- fromJSON(json_data)

b$close()
`

Any tips to get this working ? Or is there a better command to extract the content of the page ?

Regards
J

chromote is not finding my chrome (Ubuntu 18.04)

I'm using Ubuntu 18.04 and I'm getting this error:

b <- ChromoteSession$new()
Error in find_chrome() : 
#  `google-chrome` and `chromium-browser` were not found. Try setting the CHROMOTE_CHROME environment variable or adding one of these executables to your PATH.

This is probably because it should be chromium in my system:

Sys.which("chromium")
#            chromium 
# "/snap/bin/chromium" 

Not able to click on the icon in shiny custom input

I have this code:

chromote.click <- function(chromote, selector) {
  doc = chromote$DOM$getDocument()
  node = chromote$DOM$querySelector(doc$root$nodeId, selector)
  box <- chromote$DOM$getBoxModel(node$nodeId)
  left <- box$model$content[[1]]
  top <- box$model$content[[2]]
  x <- left + (box$model$width / 2)
  y <- top + (box$model$height / 2)
  chromote$Input$dispatchMouseEvent(type = "mousePressed", x = x, y = y, button="left")
  chromote$Input$dispatchMouseEvent(type = "mouseReleased", x = x, y = y, button="left")
}

chr <- chromote::ChromoteSession$new()
chromote.click(chr, ".clone-pair")

clone-pair is icon that is part of shiny custom input inside shiny app, that have jQuery click event that duplicate the content into another instance. And all I have is tooltip on icon and action is not triggered, the content is not cloned.

I have no idea how to debug this, if I call view() the R freezes as in #30. and If I click in browser (from view()) it works, fine. It just don't trigger click event only hover.

Sorry I don't have short reprex, but if you want I can bundle all the files and create git repo.

Extract View Source

Thanks for this lovely package. Just wondering if it's possible to extract view source of the page. I tried finding this info in readme but couldn't get success.

Race condition when command response and event notification arrive in the same tick

Example:

b <- Chromote$new()
b$Page$enable()

# Fire a single load event
loadEventFiredOne <- function() {
  promise(function(resolve, reject) {
    b$Page$loadEventFired(function(msg) {
      message("loadEventFired")
      b$Page$loadEventFired(NULL)  # Deregister self
      resolve(msg)
    })
  })
}

b$Page$navigate("about:blank") %...>% {
    loadEventFiredOne()
  } %...>% {
    message("Do next thing (like screenshot)")
  }

This prints nothing. But if you call navigate() again, it actually completes the promise chain:

b$Page$navigate("about:blank")
#> loadEventFired
#> Do next thing (like screenshot)

The reason this happens is as follows: Navigating to about:blank is very fast, and the command response and the event notification arrive on the same tick. The set of messages arrives in this order, in one tick:

command response id: 1
event: Page.frameStartedLoading
event: Page.frameNavigated
event: Page.loadEventFired
event: Page.frameStoppedLoading
event: Page.domContentEventFired

The handlers for these messages are scheduled in the later callback queue (via the websocket's onMessage handler). When the command response is handled, it schedules the next step in the promise chain (which calls loadEventFiredOne(), which enables the callback for the Page.loadEventFired), but that is placed at the end of the queue. So after it handles that first command response message, the queue looks like this:

event: Page.frameStartedLoading
event: Page.frameNavigated
event: Page.loadEventFired
event: Page.frameStoppedLoading
event: Page.domContentEventFired
promise:  loadEventFiredOne()

By the time loadEventFiredOne() executes, the Page.loadEventFired has already been processed -- it executes too late to be useful.

Rename / remove public R6 methods

ChromoteSession

  • Remove $get_parent() or remove $parent field
    • Many examples already use $parent, but as a field, it can also be set. I believe this is wrong and should only keep $get_parent().
  • Move self$protocol to private$protocol
    • It is only used by EventManager which is created in the ChromoteSession$initialize() method. If private was sent in with private$protocol available, the field could be removed.
  • Move self$send_command() to private$send_command()
    • It is only used by EventManager which is created in the ChromoteSession$initialize() method. If private was sent in with private$protocol available, the field could be removed.

Chromote

  • Rename $stop() to $close()
    • For naming consistency. ChromoteSession uses $close().
  • I'd like to move self$send_command() to private$send_command(), but I do not see a clean way to achieve this.
    • Similar with ChromoteSession$mark_closed(), ChromoteSession$init_promise(), ChromoteSession$get_auto_events(), ChromoteSession$invoke_event_callbacks()

@wch, thoughts?

Error when using the auto_events argument with ChromoteSession$new()

The README file indicates that

If you do not want automatic event enabling and disabling, then when creating the ChromoteSession object, use ChromoteSession$new(auto_events = FALSE).

When doing this, we get an error because the argument auto_events is not defined here

initialize = function(
parent = default_chromote_object(),
width = 992,
height = 744,
targetId = NULL,
wait_ = TRUE
) {

Inspector.enable not found

b <- ChromoteSession$new() returns the below error.

Error in onRejected(reason) : code: -32601
message: 'Inspector.enable' wasn't found

I checked on multiple versions of R. Same code works fine with prior version of chromote.

Handle Inspector.targetCrashed event?

When running headless Chrome in docker, the target may crash because of insufficient resources. In that case, the Inspector.targetCrashed event fires.

For now, this event has to be handled by the user of chromote. As chromote provides some helper methods to create the target, I wonder whether this event should be supervised by chromote.

Docker: Chrome debugging port not open after 10 seconds

Hi, when running chromote inside a Debian docker container with chromium I get

export CHROMOTE_CHROME=/usr/bin/chromium
...
> library(chromote)
> b <- ChromoteSession$new()
Error in launch_chrome(path, args) : 
  Chrome debugging port not open after 10 seconds.

I'd probably need a few hints how chromium is spawned and what interprocess communication (IPC) is used, to check whether those still work inside a container.
Yours,
Steffen

screenshot() triggers error


[1] "An error occurred: Error in onRejected(...): code: -32000\n  message: Cannot take screenshot with 0 height.\n"
Warning message:
In onRejected(reason) :
  An error occurred: Error in onRejected(...): code: -32000
  message: Cannot take screenshot with 0 height.

I am using Chromium on Ubuntu LTS 18.04.

While I am able to login and navigate the website (as I can see in Devtools viewport) both in the viewport or by using b$Page$navigate, I am unable to take a screenshot

library(chromote)
b <- ChromoteSession$new()
b$view()
b$Page$navigate("https://dev79379.service-now.com/nav_to.do?uri=%2Fcatalog_home.do%3Fsysparm_view%3Dcatalog_default")
b$screenshot()

After the b$screenshot() command, the viewport is frozen (interaction is not any longer possible)

Do I overlook something or is this a bug?

Add Flatpak support

Hi,
When Chrome or Chromium is install via Flatpak, I haven't found a way to make chromote (in my usecase, for shinytest2) use it. I have tried to set CHROMOTE_CHROME to org.chromium.Chromium, or to usr/bin/flatpak run org.chromium.Chromium, without success. I'm not a very experienced Flatpak user by any means, but would there be a way to support it?
Thanks in advance

b$Console$messageAdded(..., wait_ = FALSE) doesn't return a promise

More of a point of confusion for me than a possible bug:

I know that in sync mode, dev protocol methods that accept R callbacks return R functions that can be invoked to deregister that callback.

I was surprised that b$Console$messageAdded(..., wait_ = FALSE) returns a function and not a promise of a function. I did not expect to be able to set wait_ = FALSE and for a method to return anything other than a promise.

I would have expected the deregistration function in a promise, because I didn't expect to be able to deregister until after the callback was actually installed in Chrome. That said, I can imagine how it might work; chromote could be smart enough to remember I'd deregistered.

Whether or not it works that way (which I have not determined) it seems like it would be good for wait_ = FALSE to guarantee you'll get a promise back, and for the methods that don't to not have that parameter.

Long (infinite?) delay when webshot2 tries to take an image

If I try to render the rgl README (https://github.com/dmurdoch/rgl/blob/master/README.Rmd), fairly often I get lockups. If I break the computation, I usually end up in the Sys.sleep(1) call here:

Sys.sleep(1)
.

I tried to use git bisect to find the source, but the error doesn't happen consistently, so I don't know which commit started it. However, I don't think I've ever seen it in a revision from earlier than May. Here's a traceback after a typical lockup. It sat at the polyhedra chunk for a while, and I hit ESC to trigger the jump into the browser:


> library(chromote)
> rmarkdown::render("README.Rmd")


processing file: README.Rmd
  |...........                                            |  20%
  ordinary text without R code

  |......................                                 |  40%
label: unnamed-chunk-1 (with options) 
List of 1
 $ include: logi FALSE

  |.................................                      |  60%
  ordinary text without R code

  |............................................           |  80%
label: polyhedra (with options) 
List of 2
 $ fig.height: num 1.5
 $ echo      : logi FALSE

Called from: Sys.sleep(1)
Browse[1]> where
where 1: eval(substitute(browser(skipCalls = pos), list(pos = (length(sys.frames()) - 
    frame) + 2)), envir = sys.frame(frame))
where 2: eval(substitute(browser(skipCalls = pos), list(pos = (length(sys.frames()) - 
    frame) + 2)), envir = sys.frame(frame))
where 3: .rs.breakOnError(TRUE)
where 4: (function () 
{
    .rs.breakOnError(TRUE)
})()
where 5 at /Users/murdoch/svn/MyR/chromote/R/synchronize.R#223: Sys.sleep(1)
where 6 at /Users/murdoch/svn/MyR/chromote/R/synchronize.R#148: generateInterrupt()
where 7: value[[3L]](cond)
where 8: tryCatchOne(expr, names, parentenv, handlers[[1L]])
where 9: tryCatchList(expr, classes, parentenv, handlers)
where 10 at /Users/murdoch/svn/MyR/chromote/R/synchronize.R#112: tryCatch({
    result <- force(expr)
    if (is.promising(result)) {
        value <- NULL
        type <- NULL
        result$then(function(val) {
            value <<- val
            type <<- "success"
        })$catch(function(reason) {
            value <<- reason
            type <<- "error"
        })
        while (is.null(type) && !domain$interrupted) {
            run_now(loop = loop)
        }
        if (is.null(type)) {
            generateInterrupt()
        }
        else if (type == "success") {
            value
        }
        else if (type == "error") {
            stop(value)
        }
    }
}, interrupt = function(e) {
    domain$interrupted <<- TRUE
    message("Attempting to interrupt gracefully; press Esc/Ctrl+C to force interrupt")
    while (!loop_empty(loop = loop)) {
        run_now(loop = loop)
    }
    generateInterrupt()
})
where 11 at /Users/murdoch/svn/MyR/chromote/R/synchronize.R#94: force(expr)
where 12: domain$wrapSync(expr)
where 13 at /Users/murdoch/svn/MyR/chromote/R/synchronize.R#111: with_promise_domain(domain, {
    tryCatch({
        result <- force(expr)
        if (is.promising(result)) {
            value <- NULL
            type <- NULL
            result$then(function(val) {
                value <<- val
                type <<- "success"
            })$catch(function(reason) {
                value <<- reason
                type <<- "error"
            })
            while (is.null(type) && !domain$interrupted) {
                run_now(loop = loop)
            }
            if (is.null(type)) {
                generateInterrupt()
            }
            else if (type == "success") {
                value
            }
            else if (type == "error") {
                stop(value)
            }
        }
    }, interrupt = function(e) {
        domain$interrupted <<- TRUE
        message("Attempting to interrupt gracefully; press Esc/Ctrl+C to force interrupt")
        while (!loop_empty(loop = loop)) {
            run_now(loop = loop)
        }
        generateInterrupt()
    })
})
where 14 at /Users/murdoch/svn/MyR/chromote/R/chromote.R#105: synchronize(p, loop = private$child_loop)
where 15: cm$wait_for(p)
where 16 at /Users/murdoch/svn/MyR/rgl/R/r3d.rgl.R#451: webshot2::webshot(f1, file = filename, selector = "#webshot", 
    vwidth = width + 100, vheight = height, ...)
where 17: withVisible(...elt(i))
where 18 at /Users/murdoch/svn/MyR/rgl/R/r3d.rgl.R#451: capture.output(webshot2::webshot(f1, file = filename, selector = "#webshot", 
    vwidth = width + 100, vheight = height, ...), type = "message")
where 19 at /Users/murdoch/svn/MyR/rgl/R/convertScene.R#286: snapshot3d(scene = x, width = width, height = height)
where 20: Encoding(x)
where 21: native_encode(path)
where 22 at /Users/murdoch/svn/MyR/rgl/R/convertScene.R#286: knitr::include_graphics(snapshot3d(scene = x, width = width, 
    height = height))
where 23 at /Users/murdoch/svn/MyR/rgl/R/convertScene.R#300: getSnapshot()
where 24 at /Users/murdoch/svn/MyR/rgl/R/rglwidget.R#337: convertScene(x, width, height, elementId = elementId, webgl = webgl, 
    snapshot = snapshot, oldConvertBBox = oldConvertBBox)
where 25 at /Users/murdoch/svn/MyR/rgl/R/knitr.R#244: rglwidget(scene, width = x$width, height = x$height, webgl = !doSnapshot)
where 26: sew.rglRecordedplot(X[[i]], ...)
where 27: FUN(X[[i]], ...)
where 28: lapply(x, sew, options, ...)
where 29: sew.list(res, options)
where 30: sew(res, options)
where 31: unlist(sew(res, options))
where 32: eng_r(options)
where 33: block_exec(params)
where 34: call_block(x)
where 35: process_group.block(group)
where 36: process_group(group)
where 37: withCallingHandlers(if (tangle) process_tangle(group) else process_group(group), 
    error = function(e) {
        setwd(wd)
        cat(res, sep = "\n", file = output %n% "")
        message("Quitting from lines ", paste(current_lines(i), 
            collapse = "-"), " (", knit_concord$get("infile"), 
            ") ")
    })
where 38: process_file(text, output)
where 39: knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
where 40: rmarkdown::render("README.Rmd")

Browse[1]> 

Here's my sessionInfo():

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] rgl_0.106.23        chromote_0.0.0.9003

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6             webshot2_0.0.0.9000   
 [3] knitr_1.33             magrittr_2.0.1        
 [5] R6_2.5.0               rlang_0.4.11          
 [7] fastmap_1.1.0          stringr_1.4.0         
 [9] tools_4.1.0            websocket_1.4.0       
[11] xfun_0.23              htmltools_0.5.1.1     
[13] crosstalk_1.1.1        yaml_2.2.1            
[15] digest_0.6.27          pkgdown_1.6.1.9001    
[17] processx_3.5.2         later_1.2.0           
[19] htmlwidgets_1.5.3.9000 promises_1.2.0.1      
[21] fs_1.5.0               ps_1.6.0              
[23] curl_4.3.1             memoise_2.0.0         
[25] cachem_1.0.5           evaluate_0.14         
[27] rmarkdown_2.8          stringi_1.6.2         
[29] compiler_4.1.0         jsonlite_1.7.2 

Short and long screenshots have different widths despite `setDeviceMetricsOverride`

This may be the chromote-only version of rstudio/shinytest2#171 (using chromote 865e24e).

b <- chromote::ChromoteSession$new()
b$Emulation$setDeviceMetricsOverride(width=1920, height=1080, deviceScaleFactor=1, mobile=FALSE)

b$Page$navigate("https://www.google.com/search?q=abc")
Sys.sleep(3)
b$screenshot("snap_long.png")

# b$Page$navigate("https://google.com/404")
# Sys.sleep(3)
# b$screenshot("snap_short.png")

Both snapshots should be 1920 pixels wide, but snap_short.png (for whatever reason - not the longer one!) comes out 1890 pixels wide. [Update: The shorter 404 one's dimenions are due to the HTML element having a 15-pixel-wide padding on both sides, which is not included in screenshots, and can be fixed by passing region="padding" to b$screenshot. See below for an updated example.]

I thought I had a solution for this while working on other issues (involving 05586e3 and/or b$Emulation$setScrollbarsHidden(TRUE) near b$Emulation$setDeviceMetricsOverride, but I cannot currently reproduce that. (see below)

Can't open Chrome after using chromote

I noticed that I can't seem to open Chrome normally on my Mac after using chromote.

As a workaround, I was able to start Chrome by running manually in the Terminal:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome

Idea: provide way to run private event loop for n seconds

In this example, it listens to a screencast for 6 seconds, and uses later() to stop the screencast after that amount of time.

It would be easier to understand if there were a blocking function that ran the private event loop for n seconds.

library(chromote)
b <- ChromoteSession$new(width = 480, height = 360)
frames <- list()
cancel_save_screencast_frames <- b$Page$screencastFrame(callback_ = function(value) {
  frames[[length(frames) + 1]] <<- value
  cat(".")
})
b$Page$navigate("http://giphygifs.s3.amazonaws.com/media/73FzKOYDpp7VK/giphy.mp4")
b$Page$startScreencast(format = "png", everyNthFrame = 2)


# Stop after 6 seconds
later(
  function() {
    b$Page$stopScreencast()
    cancel_save_screencast_frames()

    lapply(seq_along(frames), function(i) {
      writeBin(jsonlite::base64_dec(frames[[i]]$data), sprintf("frame%02d.png", i))  
    })
    b$close()
    message("done")
  },
  6
)

Compatibility with httpuv

My R session hangs when I try to open a local web page served by a httpuv server with wait_=TRUE. It seems that the httpuv loop does not run (this may be related to rstudio/httpuv#250, feel free to close this issue).

Here is an example:

library(chromote)
library(servr)

b <- ChromoteSession$new()

# works fine
b$Page$navigate(url = "https://rstudio.com")

# launch a httpuv server
svr <- httd(file.path(R.home("doc"), "manual"))
faq_url <- paste(svr$url, "R-FAQ.html", sep = "/")
browseURL(faq_url)

# works fine if one uses wait_ = FALSE
b$Page$navigate(url = faq_url, wait_ = FALSE)

# the R session hangs with wait_ = TRUE
b$Page$navigate(url = faq_url)

Can't close connections after starting session

I'm really excited about this and the webshot2 package.

When using chromote inside functions in another R package, I need to be able to cleanly close connections or R CMD CHECK fails, complaining about unclosed connections.

If this package is still in a major state of flux, perhaps it is too premature to get into specifics, but I think this is a good thing to have in mind in general.

For example:

b <- chromote::ChromoteSession$new()
showConnections()
  description
3 "/var/folders/wf/xv5yf5kj6pn458pcbx_vgsyr0000gn/T//RtmplFE5oJ/supervisor_s
tdinb90457c55c61" 
4 "/var/folders/wf/xv5yf5kj6pn458pcbx_vgsyr0000gn/T//RtmplFE5oJ/supervisor_s
tdoutb9045e34114b"
  class  mode text   isopen   can read can write
3 "fifo" "w+" "text" "opened" "yes"    "yes"    
4 "fifo" "w+" "text" "opened" "yes"    "yes"  
b$parent$stop()                                                     
[2020-02-05 21:06:07] [error] handle_read_frame error: websocketpp.transport
:7 (End of File)
named list()
b$parent$stop()                                                         
Error in self$send_command(msg, callback = callback_, error = error_,  : 
  Chromote object is closed.
showConnections()
  description
3 "/var/folders/wf/xv5yf5kj6pn458pcbx_vgsyr0000gn/T//RtmplFE5oJ/supervisor_s
tdinb90457c55c61" 
4 "/var/folders/wf/xv5yf5kj6pn458pcbx_vgsyr0000gn/T//RtmplFE5oJ/supervisor_s
tdoutb9045e34114b"
  class  mode text   isopen   can read can write
3 "fifo" "w+" "text" "opened" "yes"    "yes"    
4 "fifo" "w+" "text" "opened" "yes"    "yes"  

Perhaps I'm just missing how to close things out. I looked in webshot2 to see what is done there, but didn't find anything.

For reference, I'm using chromote in this package.

Incompatibility with github websocket

When I try installing chromote with

devtools::install_github("rstudio/chromote", dependencies = T)

I get this error
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
namespace 'websocket' 1.1.0.9001 is being loaded, but >= 1.1.0.9002 is required

curl package is required for chromote to work

Currently curl is not among the imported package in chromote. It is not a direct dependency and chromote uses jsonlite which need curl when used on urls. curl is a suggested package of jsonlite only.

With a basic installation of chromote, we can't run the basic example without curl.
See the reprex below where we install chromote in a clean library with no other package.

> callr::r(function() {withr::with_temp_libpaths({
+     install.packages("pak", repos = "https://r-lib.github.io/p/pak/dev/")
+     pak::pak("rstudio/chromote")
+     library(chromote)
+     b <- ChromoteSession$new()
+     b$Browser$getVersion()
+     }, action = "replace")}, show = TRUE)
Installing package into 'C:/Users/chris/AppData/Local/Temp/RtmpW2ZuT8/temp_libpath1bc407f653bb4'
(as 'lib' is unspecified)
essai de l'URL 'https://r-lib.github.io/p/pak/dev/bin/windows/contrib/4.0/pak_0.1.2.9001.zip'
 length 13729056 bytes (13.1 MB)
\
downloaded 13.1 MB

package 'pak' successfully unpacked and MD5 sums checked
-
The downloaded binary packages are in
	C:\Users\chris\AppData\Local\Temp\RtmpW2ZuT8\downloaded_packages
\ Loading metadata database ... done
 
> Will install 12 packages.
> Will download 9 CRAN packages (11.53 MB), cached: 2 (2.45 MB).
> Will download 1 package with unknown size.
+ chromote    0.0.0.9003 [bld][cmp][dl] (GitHub: ba9bffc)
+ fastmap     1.1.0      [dl] (432.08 kB)
+ jsonlite    1.7.2      [dl] (541.93 kB)
+ later       1.2.0      [dl] (674.08 kB)
+ magrittr    2.0.1      [dl] (235.91 kB)
+ processx    3.5.2      
+ promises    1.2.0.1    [dl] (2.13 MB)
+ ps          1.6.0      [dl] (407.25 kB)
+ R6          2.5.0      [dl] (84.14 kB)
+ Rcpp        1.0.6      [dl] (3.11 MB)
+ rlang       0.4.11     
+ websocket   1.4.0      [dl] (3.92 MB)
i Getting 9 pkgs (11.53 MB) and 1 pkg with unknown size, 2 (2.45 MB) cached
v Cached copy of chromote 0.0.0.9003 (source) is the latest build
v Cached copy of R6 2.5.0 (windows) is the latest build
v Cached copy of Rcpp 1.0.6 (windows) is the latest build
v Cached copy of fastmap 1.1.0 (windows) is the latest build
v Cached copy of jsonlite 1.7.2 (windows) is the latest build
v Cached copy of later 1.2.0 (windows) is the latest build
v Cached copy of magrittr 2.0.1 (windows) is the latest build
v Cached copy of promises 1.2.0.1 (windows) is the latest build
v Cached copy of ps 1.6.0 (windows) is the latest build
v Cached copy of websocket 1.4.0 (windows) is the latest build
v Installed R6 2.5.0  (232ms)
v Installed fastmap 1.1.0  (228ms)
v Installed jsonlite 1.7.2  (284ms)
v Installed later 1.2.0  (356ms)
v Installed magrittr 2.0.1  (416ms)
v Installed ps 1.6.0  (464ms)
v Installed processx 3.5.2  (589ms)
v Installed rlang 0.4.11  (629ms)
v Installed promises 1.2.0.1  (704ms)
v Installed websocket 1.4.0  (745ms)
v Installed Rcpp 1.0.6  (921ms)
i Packaging chromote 0.0.0.9003
v Packaged chromote 0.0.0.9003 (604ms)
i Building chromote 0.0.0.9003
v Built chromote 0.0.0.9003 (5.4s)
v Installed chromote 0.0.0.9003 (github::rstudio/chromote@ba9bffc) (84ms)
v 1 + 11 pkgs | kept 0, updated 0, new 12 | downloaded 0 (0 B) 11.7s
Error : Required package curl not found. Please run: install.packages('curl')
Error: callr subprocess failed: Required package curl not found. Please run: install.packages('curl')
Type .Last.error.trace to see where the error occurred

Maybe curl should be made a hard dependency of chromote as it seems required for it to work ?

Runtime$evaluate() via ChromeRemote not working

I'm on Ubuntu 18.04 with Chrome 79 and R 3.6.2.

First, I started Chrome from the shell like this:

 google-chrome --remote-debugging-port=9222 --user-data-dir=remote-profile

In RStudio, when I try Runtime$evaluate() it doesn't work:

> library(chromote)
> b <- ChromeRemote$new(host = "localhost", port = 9222)
> r <- Chromote$new(browser = b)
> x <- r$Runtime$evaluate("123")
Error in onRejected(reason) : code: -32601
  message: 'Runtime.evaluate' wasn't found

It does work when I create a Chrome session via ChromoteSession$new().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.