Skip to content

Commit

Permalink
Automatically restart disconnected sessions (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Feb 5, 2024
1 parent f0b06f5 commit dc931f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 19 additions & 1 deletion R/live.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ LiveHTML <- R6::R6Class(
#' @description Called when `print()`ed
#' @param ... Ignored
print = function(...) {
print(html_elements(self, "html"))
print(self$html_elements("html > *"))
invisible(self)
},

#' @description
#' Display a live view of the site
view = function() {
private$check_active()
self$session$view()
invisible(self)
},
Expand All @@ -113,6 +114,7 @@ LiveHTML <- R6::R6Class(
#' Extract HTML elements from the current page.
#' @param css,xpath CSS selector or xpath expression.
html_elements = function(css, xpath) {
private$check_active()
nodes <- private$find_nodes(css, xpath)

elements <- map_chr(nodes, function(node_id) {
Expand All @@ -128,6 +130,7 @@ LiveHTML <- R6::R6Class(
#' @param css CSS selector or xpath expression.
#' @param n_clicks Number of clicks
click = function(css, n_clicks = 1) {
private$check_active()
check_number_whole(n_clicks, min = 1)

# Implementation based on puppeteer as described in
Expand Down Expand Up @@ -172,6 +175,7 @@ LiveHTML <- R6::R6Class(

#' @description Get the current scroll position.
get_scroll_position = function() {
private$check_active()
out <- self$session$Runtime$evaluate(
'({ x: window.scrollX, y: window.scrollY })',
returnByValue = TRUE
Expand All @@ -182,6 +186,7 @@ LiveHTML <- R6::R6Class(
#' @description Scroll selected element into view.
#' @param css CSS selector or xpath expression.
scroll_into_view = function(css) {
private$check_active()
node <- private$wait_for_selector(css)
self$session$DOM$scrollIntoViewIfNeeded(node)

Expand All @@ -191,6 +196,7 @@ LiveHTML <- R6::R6Class(
#' @description Scroll to specified location
#' @param top,left Number of pixels from top/left respectively.
scroll_to = function(top = 0, left = 0) {
private$check_active()
check_number_whole(top)
check_number_whole(left)

Expand All @@ -206,6 +212,7 @@ LiveHTML <- R6::R6Class(
#' @param top,left Number of pixels to scroll up/down and left/right
#' respectively.
scroll_by = function(top = 0, left = 0) {
private$check_active()
check_number_whole(top)
check_number_whole(left)

Expand All @@ -224,6 +231,7 @@ LiveHTML <- R6::R6Class(
#' @param css CSS selector or xpath expression.
#' @param text A single string containing the text to type.
type = function(css, text) {
private$check_active()
check_string(text)

node <- private$wait_for_selector(css)
Expand All @@ -240,6 +248,7 @@ LiveHTML <- R6::R6Class(
#' @param modifiers A character vector of modifiers. Must be one or more
#' of `"Shift`, `"Control"`, `"Alt"`, or `"Meta"`.
press = function(css, key_code, modifiers = character()) {
private$check_active()
desc <- as_key_desc(key_code, modifiers)

node <- private$wait_for_selector(css)
Expand All @@ -255,6 +264,15 @@ LiveHTML <- R6::R6Class(
private = list(
root_id = NULL,

check_active = function() {
if (new_chromote && !self$session$is_active()) {
suppressMessages({
self$session <- self$session$respawn()
private$root_id <- self$session$DOM$getDocument(0)$root$nodeId
})
}
},

wait_for_selector = function(css, timeout = 5) {
done <- now() + timeout
while(now() < done) {
Expand Down
12 changes: 12 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
new_chromote <- NULL

.onLoad <- function(...) {
if (is_installed("chromote")) {
new_chromote <<- utils::packageVersion("chromote") >= "0.1.2.9000"
} else {
# If chromote is not installed yet, assume it's not new to be safe.
new_chromote <- FALSE
}

invisible()
}

0 comments on commit dc931f4

Please sign in to comment.