11# Acknowledgment
2- # This function is based on the source code of the webshot2 package.
3- # We would like to acknowledge and express our gratitude to the authors
4- # and contributors of webshot2 for their valuable work in developing tools
5- # for web content capture.
2+ # This function is based on the source code of the webshot2 package.
3+ # We would like to acknowledge and express our gratitude to the authors
4+ # and contributors of webshot2 for their valuable work in developing tools
5+ # for web content capture.
66#
7- # In this implementation, we have adapted and modified parts of the original
8- # webshot2 code to address an issue where PNG files were being generated as
9- # empty images when used on Windows systems. Our modifications aim to improve
7+ # In this implementation, we have adapted and modified parts of the original
8+ # webshot2 code to address an issue where PNG files were being generated as
9+ # empty images when used on Windows systems. Our modifications aim to improve
1010# compatibility and ensure reliable output across different platforms.
1111#
1212# Reference:
@@ -26,12 +26,12 @@ biblioShot <- function(
2626 max_concurrent = getOption(" biblioShot.concurrent" , default = 6 ),
2727 verbose = FALSE
2828) {
29-
3029 if (length(url ) == 0 ) {
3130 stop(" Need url." )
3231 }
3332
34- url <- vapply(url ,
33+ url <- vapply(
34+ url ,
3535 function (x ) {
3636 if (! is_url(x )) {
3737 file_url(x )
@@ -42,9 +42,15 @@ biblioShot <- function(
4242 character (1 )
4343 )
4444
45- if (! is.null(cliprect ) && ! is.list(cliprect )) cliprect <- list (cliprect )
46- if (! is.null(selector ) && ! is.list(selector )) selector <- list (selector )
47- if (! is.null(expand ) && ! is.list(expand )) expand <- list (expand )
45+ if (! is.null(cliprect ) && ! is.list(cliprect )) {
46+ cliprect <- list (cliprect )
47+ }
48+ if (! is.null(selector ) && ! is.list(selector )) {
49+ selector <- list (selector )
50+ }
51+ if (! is.null(expand ) && ! is.list(expand )) {
52+ expand <- list (expand )
53+ }
4854
4955 if (is.null(selector )) {
5056 selector <- " html"
@@ -58,21 +64,23 @@ biblioShot <- function(
5864 }
5965
6066 args_all <- list (
61- url = url ,
62- file = file ,
63- vwidth = vwidth ,
64- vheight = vheight ,
65- selector = selector ,
66- cliprect = cliprect ,
67- expand = expand ,
68- delay = delay ,
69- zoom = zoom ,
67+ url = url ,
68+ file = file ,
69+ vwidth = vwidth ,
70+ vheight = vheight ,
71+ selector = selector ,
72+ cliprect = cliprect ,
73+ expand = expand ,
74+ delay = delay ,
75+ zoom = zoom ,
7076 useragent = useragent ,
7177 verbose = verbose
7278 )
7379
7480 n_urls <- length(url )
75- args_all <- mapply(args_all , names(args_all ),
81+ args_all <- mapply(
82+ args_all ,
83+ names(args_all ),
7684 FUN = function (arg , name ) {
7785 if (length(arg ) == 0 ) {
7886 return (vector(mode = " list" , n_urls ))
@@ -81,7 +89,11 @@ biblioShot <- function(
8189 } else if (length(arg ) == n_urls ) {
8290 return (arg )
8391 } else {
84- stop(" Argument `" , name , " ` should be NULL, length 1, or same length as `url`." )
92+ stop(
93+ " Argument `" ,
94+ name ,
95+ " ` should be NULL, length 1, or same length as `url`."
96+ )
8597 }
8698 },
8799 SIMPLIFY = FALSE
@@ -92,15 +104,22 @@ biblioShot <- function(
92104 cm <- default_chromote_object()
93105
94106 # A list of promises for the screenshots
95- res <- lapply(args_all ,
96- function (args ) {
97- new_session_screenshot(cm ,
98- args $ url , args $ file , args $ vwidth , args $ vheight , args $ selector ,
99- args $ cliprect , args $ expand , args $ delay , args $ zoom , args $ useragent ,
100- verbose
101- )
102- }
103- )
107+ res <- lapply(args_all , function (args ) {
108+ new_session_screenshot(
109+ cm ,
110+ args $ url ,
111+ args $ file ,
112+ args $ vwidth ,
113+ args $ vheight ,
114+ args $ selector ,
115+ args $ cliprect ,
116+ args $ expand ,
117+ args $ delay ,
118+ args $ zoom ,
119+ args $ useragent ,
120+ verbose
121+ )
122+ })
104123
105124 p <- promises :: promise_all(.list = res )
106125 res <- cm $ wait_for(p )
@@ -123,7 +142,6 @@ new_session_screenshot <- function(
123142 useragent ,
124143 verbose = FALSE
125144) {
126-
127145 filetype <- tolower(tools :: file_ext(file ))
128146 if (filetype != " png" && filetype != " pdf" ) {
129147 stop(" File extension must be 'png' or 'pdf'" )
@@ -140,60 +158,65 @@ new_session_screenshot <- function(
140158 stop(" Invalid value for cliprect: " , cliprect )
141159 }
142160 } else {
143- if (! is.null(cliprect ) && ! (is.numeric(cliprect ) && length(cliprect ) == 4 )) {
144- stop(" `cliprect` must be a vector with four numbers, or a list of such vectors" )
161+ if (
162+ ! is.null(cliprect ) && ! (is.numeric(cliprect ) && length(cliprect ) == 4 )
163+ ) {
164+ stop(
165+ " `cliprect` must be a vector with four numbers, or a list of such vectors"
166+ )
145167 }
146168 }
147169
148170 s <- NULL
149171
150- p <- chromote $ new_session(wait_ = FALSE ,
151- width = vwidth ,
152- height = vheight
153- ) $
154- then(function (session ) {
155- s <<- session
172+ p <- chromote $ new_session(
173+ wait_ = FALSE ,
174+ width = vwidth ,
175+ height = vheight
176+ ) $ then(function (session ) {
177+ s <<- session
156178
157- if (! is.null(useragent )) {
158- s $ Network $ setUserAgentOverride(userAgent = useragent )
159- }
160- res <- s $ Page $ loadEventFired(wait_ = FALSE )
161- s $ Page $ navigate(url , wait_ = FALSE )
162- res
163- })$
164- then(function (value ) {
165- if (delay > 0 ) {
166- promises :: promise(function (resolve , reject ) {
167- later :: later(
168- function () {
169- resolve(value )
170- },
171- delay
172- )
173- })
174- } else {
175- value
176- }
177- })$
178- then(function (value ) {
179- if (filetype == " png" ) {
180- s $ screenshot(
181- filename = file , selector = selector , cliprect = cliprect ,
182- expand = expand , scale = zoom ,
183- show = FALSE , wait_ = FALSE
179+ if (! is.null(useragent )) {
180+ s $ Network $ setUserAgentOverride(userAgent = useragent )
181+ }
182+ res <- s $ Page $ loadEventFired(wait_ = FALSE )
183+ s $ Page $ navigate(url , wait_ = FALSE )
184+ res
185+ })$ then(function (value ) {
186+ if (delay > 0 ) {
187+ promises :: promise(function (resolve , reject ) {
188+ later :: later(
189+ function () {
190+ resolve(value )
191+ },
192+ delay
184193 )
185-
186- } else if (filetype == " pdf" ) {
187- s $ screenshot_pdf(filename = file , wait_ = FALSE )
188- }
189- })$
190- then(function (value ) {
191- if (verbose ) message(url , " screenshot completed" )
192- normalizePath(value )
193- })$
194- finally(function () {
195- s $ close()
196- })
194+ })
195+ } else {
196+ value
197+ }
198+ })$ then(function (value ) {
199+ if (filetype == " png" ) {
200+ s $ screenshot(
201+ filename = file ,
202+ selector = selector ,
203+ cliprect = cliprect ,
204+ expand = expand ,
205+ scale = zoom ,
206+ show = FALSE ,
207+ wait_ = FALSE
208+ )
209+ } else if (filetype == " pdf" ) {
210+ s $ screenshot_pdf(filename = file , wait_ = FALSE )
211+ }
212+ })$ then(function (value ) {
213+ if (verbose ) {
214+ message(url , " screenshot completed" )
215+ }
216+ normalizePath(value )
217+ })$ finally(function () {
218+ s $ close()
219+ })
197220
198221 p
199222}
@@ -207,7 +230,10 @@ file_url <- function(filename) {
207230 if (is_windows()) {
208231 paste0(" file://" , normalizePath(filename , mustWork = TRUE ))
209232 } else {
210- enc2utf8(paste0(" file:///" , normalizePath(filename , winslash = " /" , mustWork = TRUE )))
233+ enc2utf8(paste0(
234+ " file:///" ,
235+ normalizePath(filename , winslash = " /" , mustWork = TRUE )
236+ ))
211237 }
212238}
213239
@@ -218,13 +244,13 @@ is_mac <- function() Sys.info()[["sysname"]] == "Darwin"
218244is_linux <- function () Sys.info()[[" sysname" ]] == " Linux"
219245
220246long_to_wide <- function (x ) {
221- if (length(x ) == 0 )
247+ if (length(x ) == 0 ) {
222248 return (x )
249+ }
223250
224251 lapply(seq_along(x [[1 ]]), function (n ) {
225252 lapply(stats :: setNames(names(x ), names(x )), function (name ) {
226253 x [[name ]][[n ]]
227254 })
228255 })
229256}
230-
0 commit comments