Browse Source

Added a few new dsl functions (mouse & kbd funcs) and a new example in the README

boB Rudis 3 years ago
parent
commit
e3b9e8fb74
12 changed files with 253 additions and 13 deletions
  1. 6
    0
      NAMESPACE
  2. 82
    0
      R/dsl.r
  3. 20
    1
      README.Rmd
  4. 31
    12
      README.md
  5. BIN
      img/cap.png
  6. BIN
      img/flash.png
  7. 16
    0
      man/splash_click.Rd
  8. 19
    0
      man/splash_focus.Rd
  9. 16
    0
      man/splash_press.Rd
  10. 16
    0
      man/splash_release.Rd
  11. 25
    0
      man/splash_send_keys.Rd
  12. 22
    0
      man/splash_send_text.Rd

+ 6
- 0
NAMESPACE View File

@@ -41,7 +41,9 @@ export(render_json)
41 41
 export(render_png)
42 42
 export(splash)
43 43
 export(splash_active)
44
+export(splash_click)
44 45
 export(splash_debug)
46
+export(splash_focus)
45 47
 export(splash_go)
46 48
 export(splash_har)
47 49
 export(splash_history)
@@ -51,7 +53,11 @@ export(splash_local)
51 53
 export(splash_perf_stats)
52 54
 export(splash_plugins)
53 55
 export(splash_png)
56
+export(splash_press)
57
+export(splash_release)
54 58
 export(splash_response_body)
59
+export(splash_send_keys)
60
+export(splash_send_text)
55 61
 export(splash_user_agent)
56 62
 export(splash_version)
57 63
 export(splash_wait)

+ 82
- 0
R/dsl.r View File

@@ -99,6 +99,88 @@ splash_go <- function(splash_obj, url) {
99 99
    splash_obj
100 100
 }
101 101
 
102
+#' Trigger mouse click event in web page.
103
+#'
104
+#' @param splash_obj splashr object
105
+#' @param x,y coordinates (distances from the left or top, relative to the current viewport)
106
+#' @export
107
+splash_click <- function(splash_obj, x, y) {
108
+   splash_obj$calls <- c(splash_obj$calls,
109
+                         sprintf("splash:mouse_click(%s, %s)", x, y))
110
+   splash_obj
111
+}
112
+
113
+#' Focus on a document element provided by a CSS selector
114
+#'
115
+#' @md
116
+#' @param splash_obj splashr object
117
+#' @param selector valid CSS selector
118
+#' @references See [the docs](https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-send-text) for more info
119
+#' @export
120
+splash_focus <- function(splash_obj, selector) {
121
+   splash_obj$calls <- c(splash_obj$calls,
122
+                         sprintf('splash:select("%s").node:focus()', selector))
123
+   splash_obj
124
+}
125
+
126
+#' Send text as input to page context, literally, character by character.
127
+#'
128
+#' This is different from [splash_send_keys]
129
+#'
130
+#' @md
131
+#' @note This adds a call to `splash:wait` so you do not have to
132
+#' @param splash_obj splashr object
133
+#' @param text string to send
134
+#' @references See [the docs](https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-send-keys) for more info
135
+#' @export
136
+splash_send_text <- function(splash_obj, text) {
137
+   splash_obj$calls <- c(splash_obj$calls,
138
+                         sprintf('splash:send_text("%s")', text),
139
+                         "splash:wait(0.1)")
140
+   splash_obj
141
+}
142
+
143
+#' Send keyboard events to page context.
144
+#'
145
+#' - whitespace is ignored and only used to separate the different keys
146
+#' - characters are literally represented
147
+#'
148
+#' This is different from [splash_send_text]
149
+#'
150
+#' @md
151
+#' @param splash_obj splashr object
152
+#' @param keys string to send
153
+#' @references See [the docs](https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-send-keys) for more info
154
+#' @export
155
+splash_send_keys <- function(splash_obj, keys) {
156
+   splash_obj$calls <- c(splash_obj$calls,
157
+                         sprintf('splash:send_keys("%s")', keys),
158
+                         "splash:wait(0.1)")
159
+   splash_obj
160
+}
161
+
162
+#' Trigger mouse release event in web page.
163
+#'
164
+#' @param splash_obj splashr object
165
+#' @param x,y coordinates (distances from the left or top, relative to the current viewport)
166
+#' @export
167
+splash_release <- function(splash_obj, x, y) {
168
+   splash_obj$calls <- c(splash_obj$calls,
169
+                         sprintf("splash:mouse_release(%s, %s)", x, y))
170
+   splash_obj
171
+}
172
+
173
+#' Trigger mouse press event in web page.
174
+#'
175
+#' @param splash_obj splashr object
176
+#' @param x,y coordinates (distances from the left or top, relative to the current viewport)
177
+#' @export
178
+splash_press <- function(splash_obj, x, y) {
179
+   splash_obj$calls <- c(splash_obj$calls,
180
+                         sprintf("splash:mouse_press(%s, %s)", x, y))
181
+   splash_obj
182
+}
183
+
102 184
 #' Wait for a period time
103 185
 #'
104 186
 #' When script is waiting WebKit continues processing the webpage

+ 20
- 1
README.Rmd View File

@@ -60,6 +60,8 @@ The following functions are implemented:
60 60
 Mini-DSL (domain-specific language). These can be used to create a "script" without actually scripting in Lua. They are a less-powerful/configurable set of calls than what you can make with a full Lua function but the idea is to have it take care of very common but simple use-cases, like waiting a period of time before capturing a HAR/HTML/PNG image of a site:
61 61
 
62 62
 - `splash_plugins`:	Enable or disable browser plugins (e.g. Flash).
63
+- `splash_click`:	Trigger mouse click event in web page.
64
+- `splash_focus`:	Focus on a document element provided by a CSS selector
63 65
 - `splash_images`:	Enable/disable images
64 66
 - `splash_response_body`:	Enable or disable response content tracking.
65 67
 - `splash_go`:	Go to an URL.
@@ -67,7 +69,11 @@ Mini-DSL (domain-specific language). These can be used to create a "script" with
67 69
 - `splash_har`:	Return information about Splash interaction with a website in HAR format.
68 70
 - `splash_html`:	Return a HTML snapshot of a current page.
69 71
 - `splash_png`:	Return a screenshot of a current page in PNG format.
70
-- `splash_user_agent:	Overwrite the User-Agent header for all further requests. NOTE: There are many "helper" user agent strings to go with `splash_user_agent`. Look for objects in `splashr` starting with `ua_`.
72
+- `splash_press`:	Trigger mouse press event in web page.
73
+- `splash_release`:	Trigger mouse release event in web page.
74
+- `splash_send_keys`:	Send keyboard events to page context.
75
+- `splash_send_text`:	Send text as input to page context, literally, character by character.
76
+- `splash_user_agent`:	Overwrite the User-Agent header for all further requests. NOTE: There are many "helper" user agent strings to go with `splash_user_agent`. Look for objects in `splashr` starting with `ua_`.
71 77
 
72 78
 `httr` helpers. These help turn various bits of `splashr` objects into `httr`-ish things:
73 79
 
@@ -199,6 +205,19 @@ rawToChar(res) %>%
199 205
   jsonlite::fromJSON()
200 206
 ```
201 207
 
208
+### Interacting With Flash sites
209
+
210
+```{r eval=FALSE}
211
+splash_local %>% 
212
+  splash_go("https://gis.cdc.gov/GRASP/Fluview/FluHospRates.html") %>%
213
+  splash_wait(4) %>%
214
+  splash_click(460, 550) %>%
215
+  splash_wait(2) %>%
216
+  splash_png()
217
+```
218
+
219
+<img src="img/flash.png" width="50%"/>
220
+
202 221
 ### Rendering Widgets
203 222
 
204 223
 ```{r eval=FALSE}

+ 31
- 12
README.md View File

@@ -58,6 +58,8 @@ The following functions are implemented:
58 58
 Mini-DSL (domain-specific language). These can be used to create a "script" without actually scripting in Lua. They are a less-powerful/configurable set of calls than what you can make with a full Lua function but the idea is to have it take care of very common but simple use-cases, like waiting a period of time before capturing a HAR/HTML/PNG image of a site:
59 59
 
60 60
 -   `splash_plugins`: Enable or disable browser plugins (e.g. Flash).
61
+-   `splash_click`: Trigger mouse click event in web page.
62
+-   `splash_focus`: Focus on a document element provided by a CSS selector
61 63
 -   `splash_images`: Enable/disable images
62 64
 -   `splash_response_body`: Enable or disable response content tracking.
63 65
 -   `splash_go`: Go to an URL.
@@ -65,7 +67,11 @@ Mini-DSL (domain-specific language). These can be used to create a "script" with
65 67
 -   `splash_har`: Return information about Splash interaction with a website in HAR format.
66 68
 -   `splash_html`: Return a HTML snapshot of a current page.
67 69
 -   `splash_png`: Return a screenshot of a current page in PNG format.
68
--   `splash_user_agent:   Overwrite the User-Agent header for all further requests. NOTE: There are many "helper" user agent strings to go with`splash\_user\_agent`. Look for objects in`splashr`starting with`ua\_\`.
70
+-   `splash_press`: Trigger mouse press event in web page.
71
+-   `splash_release`: Trigger mouse release event in web page.
72
+-   `splash_send_keys`: Send keyboard events to page context.
73
+-   `splash_send_text`: Send text as input to page context, literally, character by character.
74
+-   `splash_user_agent`: Overwrite the User-Agent header for all further requests. NOTE: There are many "helper" user agent strings to go with `splash_user_agent`. Look for objects in `splashr` starting with `ua_`.
69 75
 
70 76
 `httr` helpers. These help turn various bits of `splashr` objects into `httr`-ish things:
71 77
 
@@ -148,13 +154,13 @@ splash_debug()
148 154
     ## List of 7
149 155
     ##  $ active  : list()
150 156
     ##  $ argcache: int 0
151
-    ##  $ fds     : int 14
157
+    ##  $ fds     : int 19
152 158
     ##  $ leaks   :List of 4
153 159
     ##   ..$ Deferred  : int 50
154 160
     ##   ..$ LuaRuntime: int 1
155 161
     ##   ..$ QTimer    : int 1
156 162
     ##   ..$ Request   : int 1
157
-    ##  $ maxrss  : int 68444
163
+    ##  $ maxrss  : int 170252
158 164
     ##  $ qsize   : int 0
159 165
     ##  $ url     : chr "http://localhost:8050"
160 166
     ##  - attr(*, "class")= chr [1:2] "splash_debug" "list"
@@ -168,7 +174,7 @@ render_html(url = "http://marvel.com/universe/Captain_America_(Steve_Rogers)")
168 174
 
169 175
     ## {xml_document}
170 176
     ## <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
171
-    ## [1] <head>\n<script type="text/javascript" async="async" src="http://uncanny.marvel.com/id?callback=s_c_il%5B1%5D._se ...
177
+    ## [1] <head>\n<script type="text/javascript" async="async" src="http://dpm.demdex.net/id?d_rtbd=json&amp;d_ver=2&amp;d_ ...
172 178
     ## [2] <body id="index-index" class="index-index" onload="findLinks('myLink');">\n\n\t<div id="page_frame" style="overfl ...
173 179
 
174 180
 ``` r
@@ -199,21 +205,21 @@ print(har)
199 205
     ## --------HAR PAGES-------- 
200 206
     ## Page id: 1 , Page title: Poynter – A global leader in journalism. Strengthening democracy. 
201 207
     ## --------HAR ENTRIES-------- 
202
-    ## Number of entries: 41 
208
+    ## Number of entries: 17 
203 209
     ## REQUESTS: 
204 210
     ## Page: 1 
205
-    ## Number of entries: 41 
211
+    ## Number of entries: 17 
206 212
     ##   -  http://www.poynter.org/ 
207
-    ##   -  http://www.poynter.org/wp-content/plugins/easy-author-image/css/easy-author-image.css?ver=2016_06_24.1 
208 213
     ##   -  http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css?ver=2016_06_24.1 
209
-    ##   -  http://cloud.webtype.com/css/162ac332-3b31-4b73-ad44-da375b7f2fe3.css?ver=2016_06_24.1 
210
-    ##   -  http://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css?ver=2016_06_24.1 
214
+    ##   -  http://www.poynter.org/wp-content/themes/poynter_timber/assets/scrollbar/jquery.mCustomScrollbar.min.css?ver=2016... 
215
+    ##   -  http://www.poynter.org/wp-content/plugins/jetpack/css/jetpack.css?ver=4.0.4 
216
+    ##   -  http://www.poynter.org/wp-content/themes/poynter_timber/style.css?ver=2016_06_24.1 
211 217
     ##      ........ 
218
+    ##   -  http://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/fonts/fontawesome-webfont.woff?v=4.4.0 
212 219
     ##   -  http://cloud.webtype.com/webtype/ff2/3/7dac2f83-affe-4762-81f1-056f47b74dc7?ec_token=8f7c4c4997246fd7fa920171c994... 
213 220
     ##   -  http://cloud.webtype.com/webtype/ff2/3/4ac7f809-9bdf-4acc-8bd5-a922f05f271b?ec_token=8f7c4c4997246fd7fa920171c994... 
214 221
     ##   -  http://cloud.webtype.com/webtype/ff2/3/c6608520-1978-45ac-9061-74ada664cae4?ec_token=8f7c4c4997246fd7fa920171c994... 
215
-    ##   -  http://cloud.webtype.com/webtype/ff2/3/380e3672-840d-462a-83ee-2ea85a43504a?ec_token=8f7c4c4997246fd7fa920171c994... 
216
-    ##   -  http://cloud.webtype.com/webtype/ff2/3/c6369fc5-fc59-4a12-ac92-25afa6c567a0?ec_token=8f7c4c4997246fd7fa920171c994...
222
+    ##   -  http://static.chartbeat.com/js/chartbeat.js
217 223
 
218 224
 You can use [`HARtools::HARviewer`](https://github.com/johndharrison/HARtools/blob/master/R/HARviewer.R) — which this pkg import/exports — to get view the HAR in an interactive HTML widget.
219 225
 
@@ -252,6 +258,19 @@ rawToChar(res) %>%
252 258
     ## $title
253 259
     ## [1] "rud.is | \"In God we trust. All others must bring data\""
254 260
 
261
+### Interacting With Flash sites
262
+
263
+``` r
264
+splash_local %>% 
265
+  splash_go("https://gis.cdc.gov/GRASP/Fluview/FluHospRates.html") %>%
266
+  splash_wait(4) %>%
267
+  splash_click(460, 550) %>%
268
+  splash_wait(2) %>%
269
+  splash_png()
270
+```
271
+
272
+<img src="img/flash.png" width="50%"/>
273
+
255 274
 ### Rendering Widgets
256 275
 
257 276
 ``` r
@@ -289,7 +308,7 @@ library(testthat)
289 308
 date()
290 309
 ```
291 310
 
292
-    ## [1] "Tue Feb 21 16:54:03 2017"
311
+    ## [1] "Thu Feb 23 17:22:15 2017"
293 312
 
294 313
 ``` r
295 314
 test_dir("tests/")

BIN
img/cap.png View File


BIN
img/flash.png View File


+ 16
- 0
man/splash_click.Rd View File

@@ -0,0 +1,16 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/dsl.r
3
+\name{splash_click}
4
+\alias{splash_click}
5
+\title{Trigger mouse click event in web page.}
6
+\usage{
7
+splash_click(splash_obj, x, y)
8
+}
9
+\arguments{
10
+\item{splash_obj}{splashr object}
11
+
12
+\item{x, y}{coordinates (distances from the left or top, relative to the current viewport)}
13
+}
14
+\description{
15
+Trigger mouse click event in web page.
16
+}

+ 19
- 0
man/splash_focus.Rd View File

@@ -0,0 +1,19 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/dsl.r
3
+\name{splash_focus}
4
+\alias{splash_focus}
5
+\title{Focus on a document element provided by a CSS selector}
6
+\usage{
7
+splash_focus(splash_obj, selector)
8
+}
9
+\arguments{
10
+\item{splash_obj}{splashr object}
11
+
12
+\item{selector}{valid CSS selector}
13
+}
14
+\description{
15
+Focus on a document element provided by a CSS selector
16
+}
17
+\references{
18
+See \href{https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-send-text}{the docs} for more info
19
+}

+ 16
- 0
man/splash_press.Rd View File

@@ -0,0 +1,16 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/dsl.r
3
+\name{splash_press}
4
+\alias{splash_press}
5
+\title{Trigger mouse press event in web page.}
6
+\usage{
7
+splash_press(splash_obj, x, y)
8
+}
9
+\arguments{
10
+\item{splash_obj}{splashr object}
11
+
12
+\item{x, y}{coordinates (distances from the left or top, relative to the current viewport)}
13
+}
14
+\description{
15
+Trigger mouse press event in web page.
16
+}

+ 16
- 0
man/splash_release.Rd View File

@@ -0,0 +1,16 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/dsl.r
3
+\name{splash_release}
4
+\alias{splash_release}
5
+\title{Trigger mouse release event in web page.}
6
+\usage{
7
+splash_release(splash_obj, x, y)
8
+}
9
+\arguments{
10
+\item{splash_obj}{splashr object}
11
+
12
+\item{x, y}{coordinates (distances from the left or top, relative to the current viewport)}
13
+}
14
+\description{
15
+Trigger mouse release event in web page.
16
+}

+ 25
- 0
man/splash_send_keys.Rd View File

@@ -0,0 +1,25 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/dsl.r
3
+\name{splash_send_keys}
4
+\alias{splash_send_keys}
5
+\title{Send keyboard events to page context.}
6
+\usage{
7
+splash_send_keys(splash_obj, keys)
8
+}
9
+\arguments{
10
+\item{splash_obj}{splashr object}
11
+
12
+\item{keys}{string to send}
13
+}
14
+\description{
15
+\itemize{
16
+\item whitespace is ignored and only used to separate the different keys
17
+\item characters are literally represented
18
+}
19
+}
20
+\details{
21
+This is different from \link{splash_send_text}
22
+}
23
+\references{
24
+See \href{https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-send-keys}{the docs} for more info
25
+}

+ 22
- 0
man/splash_send_text.Rd View File

@@ -0,0 +1,22 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/dsl.r
3
+\name{splash_send_text}
4
+\alias{splash_send_text}
5
+\title{Send text as input to page context, literally, character by character.}
6
+\usage{
7
+splash_send_text(splash_obj, text)
8
+}
9
+\arguments{
10
+\item{splash_obj}{splashr object}
11
+
12
+\item{text}{string to send}
13
+}
14
+\description{
15
+This is different from \link{splash_send_keys}
16
+}
17
+\note{
18
+This adds a call to \code{splash:wait} so you do not have to
19
+}
20
+\references{
21
+See \href{https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-send-keys}{the docs} for more info
22
+}

Loading…
Cancel
Save