-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathToppoki.purs
342 lines (289 loc) · 11.3 KB
/
Toppoki.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
module Toppokki where
import Prelude
import Control.Promise (Promise)
import Control.Promise as Promise
import Data.Function.Uncurried as FU
import Data.Newtype (class Newtype)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Exception (Error)
import Effect.Uncurried as EU
import Foreign (Foreign)
import Node.Buffer (Buffer)
import Prim.Row as Row
import Unsafe.Coerce (unsafeCoerce)
foreign import data Puppeteer :: Type
foreign import data Browser :: Type
foreign import data Page :: Type
foreign import data ElementHandle :: Type
-- This is used when one wants to use chrome-aws-lambda version of puppeteer
foreign import data ChromeAWS :: Type
newtype URL = URL String
derive instance newtypeURL :: Newtype URL _
newtype Selector = Selector String
derive instance newtypeSelector :: Newtype Selector _
type LaunchOptions =
( args :: Array String
, defaultViewport :: Record DefaultViewPort
, devtools :: Boolean
, dumpio :: Boolean
, executablePath :: String
, handleSIGHUP :: Boolean
, handleSIGINT :: Boolean
, handleSIGTERM :: Boolean
, headless :: Boolean
, ignoreDefaultArgs :: Array String
, ignoreHTTPSErrors :: Boolean
, pipe :: Boolean
, slowMo :: Number
, timeout :: Number
, userDataDir :: String
)
type DefaultViewPort =
( width :: Number
, height :: Number
, deviceScaleFactor :: Number
, isMobile :: Boolean
, hasTouch :: Boolean
, isLandscape :: Boolean
)
launch
:: forall options trash
. Row.Union options trash LaunchOptions
=> { | options }
-> Aff Browser
launch = runPromiseAffE1 _launch
launchChromeAWS
:: forall options trash
. Row.Union options trash LaunchOptions
=> ChromeAWS
-> { | options }
-> Aff Browser
launchChromeAWS = runPromiseAffE2 _launchChromeAWS
newPage :: Browser -> Aff Page
newPage = runPromiseAffE1 _newPage
goto :: URL -> Page -> Aff Unit
goto = runPromiseAffE2 _goto
close :: Browser -> Aff Unit
close = runPromiseAffE1 _close
content :: Page -> Aff String
content = runPromiseAffE1 _content
type ScreenshotOptions =
( path :: String
, type :: String
, quality :: Int
, fullPage :: Boolean
, clip ::
{ x :: Int
, y :: Int
, width :: Int
, height :: Int
}
, omitBackground :: Boolean
)
screenshot
:: forall options trash
. Row.Union options trash ScreenshotOptions
=> { | options }
-> Page
-> Aff Buffer
screenshot o p = runPromiseAffE2 _screenshot o p
foreign import data PDFMargin :: Type
type PDFMarginOptions =
( top :: String
, right :: String
, bottom :: String
, left :: String
)
makePDFMargin
:: forall options trash
. Row.Union options trash PDFMarginOptions
=> { | options }
-> PDFMargin
makePDFMargin = unsafeCoerce
type PDFOptions =
( path :: String
, scale :: Int
, displayHeaderFooter :: Boolean
, headerTemplate :: String
, footerTemplate :: String
, printBackground :: Boolean
, landscape :: Boolean
, pageRanges :: String
, format :: String
, width :: String
, height :: String
, margin :: PDFMargin
)
pdf
:: forall options trash
. Row.Union options trash PDFOptions
=> { | options }
-> Page
-> Aff Buffer
pdf = runPromiseAffE2 _pdf
onPageError :: EU.EffectFn1 Error Unit -> Page -> Effect Unit
onPageError = EU.runEffectFn3 _on "pageerror"
onLoad :: EU.EffectFn1 Unit Unit -> Page -> Effect Unit
onLoad = EU.runEffectFn3 _on "load"
pageWaitForSelector
:: forall options trash
. Row.Union options trash
( visible :: Boolean
, hidden :: Boolean
, timeout :: Int
)
=> Selector
-> { | options }
-> Page
-> Aff ElementHandle
pageWaitForSelector = runPromiseAffE3 _pageWaitForSelector
focus :: Selector -> Page -> Aff Unit
focus = runPromiseAffE2 _focus
-- | Select a specific option in select dropdown group.
select
:: Selector
-> String
-> Page
-> Aff Unit
select = runPromiseAffE3 _select
type_
:: forall options trash
. Row.Union options trash
( delay :: Int
)
=> Selector
-> String
-> { | options }
-> Page
-> Aff Unit
type_ = runPromiseAffE4 _type
click :: Selector -> Page -> Aff Unit
click = runPromiseAffE2 _click
foreign import data WaitUntilOption :: Type
networkIdle :: WaitUntilOption
networkIdle = unsafeCoerce $ "networkidle"
networkIdle0 :: WaitUntilOption
networkIdle0 = unsafeCoerce $ "networkidle0"
networkIdle2 :: WaitUntilOption
networkIdle2 = unsafeCoerce $ "networkidle2"
waitForNavigation
:: forall options trash
. Row.Union options trash
( waitUntil :: WaitUntilOption
)
=> { | options }
-> Page
-> Aff Unit
waitForNavigation = runPromiseAffE2 _waitForNavigation
getLocationRef :: Page -> Aff String
getLocationRef p = Promise.toAffE $ FU.runFn1 _getLocationHref p
-- | The function is invoked after the document was created but before any of its scripts were run.
-- |
-- | This is useful to amend the environment, e.g. to seed Math.random.
unsafeEvaluateOnNewDocument :: String -> Page -> Aff Foreign
unsafeEvaluateOnNewDocument = runPromiseAffE2 _unsafeEvaluateOnNewDocument
unsafeEvaluateStringFunction :: String -> Page -> Aff Foreign
unsafeEvaluateStringFunction = runPromiseAffE2 _unsafeEvaluateStringFunction
-- | This method runs document.querySelector within the page and passes it as the first argument to pageFunction. If there's no element matching selector, the method throws an error.
-- |
-- | Second argument is a pageFunction which should be a valid JavaScript function written as a string which we unsafely eval.
-- |
-- | If pageFunction returns a Promise, then page.$$eval would wait for the promise to resolve and return its value.
unsafePageEval :: Selector -> String -> Page -> Aff Foreign
unsafePageEval = runPromiseAffE3 _unsafePageEval
-- | This method runs Array.from(document.querySelectorAll(selector)) within the page and passes it as the first argument to pageFunction.
-- |
-- | Second argument is a pageFunction which should be a valid JavaScript function written as a string which we unsafely eval.
-- |
-- | If pageFunction returns a Promise, then page.$$eval would wait for the promise to resolve and return its value.
unsafePageEvalAll :: Selector -> String -> Page -> Aff Foreign
unsafePageEvalAll = runPromiseAffE3 _unsafePageEvalAll
runPromiseAffE1 :: forall a o. FU.Fn1 a (Effect (Promise o)) -> a -> Aff o
runPromiseAffE1 f a = Promise.toAffE $ FU.runFn1 f a
runPromiseAffE2 :: forall a b o. FU.Fn2 a b (Effect (Promise o)) -> a -> b -> Aff o
runPromiseAffE2 f a b = Promise.toAffE $ FU.runFn2 f a b
runPromiseAffE3 :: forall a b c o. FU.Fn3 a b c (Effect (Promise o)) -> a -> b -> c -> Aff o
runPromiseAffE3 f a b c = Promise.toAffE $ FU.runFn3 f a b c
runPromiseAffE4 :: forall a b c d o. FU.Fn4 a b c d (Effect (Promise o)) -> a -> b -> c -> d -> Aff o
runPromiseAffE4 f a b c d = Promise.toAffE $ FU.runFn4 f a b c d
-- | See [USKeyboardLayout](https://github.com/GoogleChrome/puppeteer/blob/v1.18.1/lib/USKeyboardLayout.js) for a list of all key names.
newtype KeyboardKey = KeyboardKey String
-- | Dispatches a keydown event.
keyboardDown :: forall options trash
. Row.Union options trash ( text :: String )
=> KeyboardKey
-> { | options }
-> Page
-> Aff Unit
keyboardDown = runPromiseAffE3 _keyboardDown
-- | Trigger a single keypress. Shortcut for `keyboard.down` and `keyboard.up`.
keyboardPress
:: forall options trash
. Row.Union options trash
( delay :: Int
, text :: String
)
=> KeyboardKey
-> { | options }
-> Page
-> Aff Unit
keyboardPress = runPromiseAffE3 _keyboardPress
-- | Dispatches a keypress and input event. This does not send a keydown or keyup event.
keyboardSendCharacter :: String -> Page -> Aff Unit
keyboardSendCharacter = runPromiseAffE2 _keyboardSendCharacter
-- | Sends a keydown, keypress/input, and keyup event for each character in the text.
-- | To press a special key, like Control or ArrowDown, use keyboard.press.
keyboardType :: forall options trash
. Row.Union options trash ( delay :: Number )
=> String
-> { | options }
-> Page
-> Aff Unit
keyboardType = runPromiseAffE3 _keyboardType
-- | Dispatches a keyup event.
keyboardUp :: forall options trash
. Row.Union options trash ( text :: String )
=> KeyboardKey
-> { | options }
-> Page
-> Aff Unit
keyboardUp = runPromiseAffE3 _keyboardUp
-- | set Viewport
setViewport :: Record DefaultViewPort -> Page -> Aff Unit
setViewport = runPromiseAffE2 _setViewport
-- | Specific user agent to use in this page
setUserAgent :: String -> Page -> Aff Unit
setUserAgent = runPromiseAffE2 _setUserAgent
-- | Brings page to front (activates tab)
bringToFront :: Page -> Aff Unit
bringToFront = runPromiseAffE1 _bringToFront
foreign import puppeteer :: Puppeteer
foreign import _launch :: forall options. FU.Fn1 options (Effect (Promise Browser))
foreign import _launchChromeAWS :: forall options. FU.Fn2 ChromeAWS options (Effect (Promise Browser))
foreign import _newPage :: FU.Fn1 Browser (Effect (Promise Page))
foreign import _goto :: FU.Fn2 URL Page (Effect (Promise Unit))
foreign import _close :: FU.Fn1 Browser (Effect (Promise Unit))
foreign import _content :: FU.Fn1 Page (Effect (Promise String))
foreign import _screenshot :: forall options. FU.Fn2 options Page (Effect (Promise Buffer))
foreign import _pdf :: forall options. FU.Fn2 options Page (Effect (Promise Buffer))
foreign import _on :: forall a. EU.EffectFn3 String (EU.EffectFn1 a Unit) Page Unit
foreign import _pageWaitForSelector :: forall options. FU.Fn3 Selector options Page (Effect (Promise ElementHandle))
foreign import _select :: forall options. FU.Fn3 Selector options Page (Effect (Promise Unit))
foreign import _focus :: FU.Fn2 Selector Page (Effect (Promise Unit))
foreign import _setViewport :: FU.Fn2 (Record DefaultViewPort) Page (Effect (Promise Unit))
foreign import _type :: forall options. FU.Fn4 Selector String options Page (Effect (Promise Unit))
foreign import _click :: FU.Fn2 Selector Page (Effect (Promise Unit))
foreign import _waitForNavigation :: forall options. FU.Fn2 options Page (Effect (Promise Unit))
foreign import _getLocationHref :: FU.Fn1 Page (Effect (Promise String))
foreign import _unsafeEvaluateOnNewDocument :: FU.Fn2 String Page (Effect (Promise Foreign))
foreign import _unsafeEvaluateStringFunction :: FU.Fn2 String Page (Effect (Promise Foreign))
foreign import _unsafePageEval :: FU.Fn3 Selector String Page (Effect (Promise Foreign))
foreign import _unsafePageEvalAll :: FU.Fn3 Selector String Page (Effect (Promise Foreign))
foreign import _keyboardDown :: forall options. FU.Fn3 KeyboardKey options Page (Effect (Promise Unit))
foreign import _keyboardPress :: forall options. FU.Fn3 KeyboardKey options Page (Effect (Promise Unit))
foreign import _keyboardSendCharacter :: FU.Fn2 String Page (Effect (Promise Unit))
foreign import _keyboardType :: forall options. FU.Fn3 String options Page (Effect (Promise Unit))
foreign import _keyboardUp :: forall options. FU.Fn3 KeyboardKey options Page (Effect (Promise Unit))
foreign import _setUserAgent :: FU.Fn2 String Page (Effect (Promise Unit))
foreign import _bringToFront :: FU.Fn1 Page (Effect (Promise Unit))