-
Notifications
You must be signed in to change notification settings - Fork 16
/
phantomjs.node_cli.txt
278 lines (226 loc) · 18.4 KB
/
phantomjs.node_cli.txt
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
PHANTOMJS
phantomjs [FILE...] #Command line (Node module 1.9.7)
#PhantomJS 2 uses ES5 (not ES6)
#Runs JavaScript FILE (or REPL), but spawned in an invisible WebKit browser (does not
#use Node.js)
#Used for mainly two reasons:
# - automation of web navigation
# - testing. However, using SauceLabs is better.
#Prefer using CasperJS directly
#Cannot be used as a Node module, but can require(STR) Node module (and use Node
#modules to emulate Node native modules if needed).
--webdriver URL:PORT #Starts as a Webdriver driver, using Ghostdriver
--webdriver-selenium-grid-hub URL #When using Selenium Grid
--ignore-ssl-errors=BOOL #Déf: "false"
--ssl-protocol=sslv2|3|tlsv1|any
--ssl-certificates-path=PATH
--proxy=ADDRESS:PORT
--proxy-type=http|proxy5|none
--proxy-auth=username:password
--disk-cache=BOOL #Déf: false
--max-disk-cache-size=UINT
phantom #Node module (0.6.3)
#Makes it possible to use PhantomJS WEBPAGE as a Node module.
#Works by communicating with the PhantomJS process with an Express server.
#Requires phantomjs to be in $PATH
#Some differences of API:
# - return values use callbacks instead:
# - WEBPAGE.render(...) -> WEBPAGE.render(..., FUNC(STR))
# - also WEBPAGE.evaluate(), etc.
# - WEBPAGE.VAR[ = VAL] -> WEBPAGE.set(VAR_STR, VAL), WEBPAGE.get(VAR_STR, FUNC(VAL))
# - onResourceRequested and onConsoleMessage work differently
# - many WEBPAGE.* functions are missing
# - some other behavior might differ
PHANTOM.create([ARG_STR...,]function(ph)
{ ph.createPage(FUNC(WEBPAGE)) }) #ARG_STR are command line arguments to phantomjs
PHANTOM.create("flags",{onExit: FUNC()})#Fired at PHANTOM.exit() or PhantomJS crash
GULP-PHANTOM([OBJ]) #Gulp plugin (0.0.6).
#Runs phantomjs on IOSTREAM, and put output in output IOSTREAM2
#OBJ are same options as phantomjs but camelcased, with also:
# - ext STR, e.g. ".txt" to change extension of filenames
# - all phantomjs command line options, camelcased
phantom.exit([NUM]) #Must be called at the end.
/=+===============================+=\
/ : : \
)==: WEBPAGE :==(
\ :_______________________________: /
\=+===============================+=/
WEBPAGE.create() #Returns a PAGE, i.e. a virtual internet "tab"
#Event handler PAGE.onInitialized = FUNC()
PAGE.open(URL_STR[, METHOD_STR[, OBJ]], #Request a page.
FUNC(STR)) #STR is "success|fail". METHOD is "GET" by def. OBJ is POST data.
#Page is loaded but must use PAGE.evaluate() to access its global object.
#Event handler PAGE.onLoadStarted = FUNC() and PAGE.onLoadFinished = FUNC(STR)
PAGE.openUrl(URL_STR[, OBJ][, OBJ2], #Same but uses extra options. OBJ has members:
FUNC(STR)) # - operation METHOD_STR
# - data OBJ3: "POST" data
# - headers OBJ4: sent for only this request. To send for all HTTP request, modify
# PAGE.customHeaders OBJ
#OBJ2 members:
# - javascriptEnabled BOOL (déf: true)
# - loadImages BOOL (déf: true)
# Ca also use phantom ... --load-images=BOOL
# - localToRemoteUrlAccessEnabled (déf: false): enable file:// to http://
# Can also use phantom ... --local-to-remote-url-access=BOOL
# - userAgent STR
# - userName STR, password STR: HTTP authentication
# - XSSAuditingEnabled BOOL (déf: false)
# - webSecurityEnabled BOOL (déf: true), e.g. forbidding cross XHR
# Can also use phantom ... --web-security=BOOL
# - resourceTimeout NUM (déf: undefined): PAGE.onResourceTimeout = FUNC(OBJ) will be
# called, where OBJ has same members as onResourceRequested, and also errorCode NUM
# and errorString STR
#OBJ2 is stored in PAGE.settings (and can be changed before the first PAGE.open())
PAGE.close() #Can use event handler PAGE.onClosing = FUNC(PAGE)
PAGE.reload() #
PAGE.stop() #
PAGE.setContent(HTML_STR, URL_STR) #Sets a fictional page with URL and content HTML.
PAGE.onResourceRequested = #When making HTTP requests.
FUNC(OBJ, OBJ2) #OBJ has members id NUM, method STR, url STR, time DATE and headers OBJ (see below)
#OBJ2 has members:
# - abort()
# - changeUrl(STR)
# - setHeader(VAR_STR, VAL_STR)
PAGE.onResourceReceived = FUNC(OBJ) #When big HTTP requests, will fire several times.
#OBJ has members id NUM, url STR, time DATE, headers OBJ_ARR (OBJ is { name, value }),
#bodySize NUM, contentType STR redirectUrl STR, stage "start|end" (for chunks), status
#NUM, statusText STR
PAGE.onResourceError = FUNC(OBJ) #OBJ has members id, url, errorCode, errorString
PAGE.onNavigationRequested = FUNC(URL, #STR is "LinkClicked|Form[ReS|s]ubmitted|BackOrForward|Reload|Other|Undefined"
STR, BOOL, BOOL2) #BOOL is whether navigationLocked
#BOOL2 is true if comes from main frame.
PAGE.onUrlChanged = FUNC(URL) #
PAGE.render(FILE_STR[, OBJ]) #Save page under FILE_STR. OBJ members are:
# - format STR, among "PNG", "JPG", "GIF", "PDF" (déf: guessed from extension)
# - quality NUM
#Can only be done when rendering done, which is done in the FUNC() of PAGE.open(),
#but not after PAGE.setContent() (needs to use onLoadFinished event handler)
#Rendering details:
# - Can restrict zone to render with PAGE.clipRect = { top, left, width, height }
# - Be careful that changing viewportSize, paperSize and zoomFactor takes time if
# already loaded, so PAGE.render() should wait until it is done.
# - If page does not set background, it will be rendered transparent, unless doing
# document.body.bgColor = STR
# - Does not support Flash, WebGL and CSS-3D
PAGE.renderBase64(STR) #Same but returns as base64 string instead (STR is format), and no PDF.
PAGE.viewportSize #{ width, height } (déf: 400x300)
PAGE.paperSize #Size for rendering PDF, either:
# - { width, height, border }, as "NUMpx|mm|cm|in|px" (déf: viewportSize and border 0)
# - { format, orientation, border }, where format is "A3|A4|A5|Legal|Letter|Tabloid"
# and orientation "portrait|landscape" (déf: "portrait")
PAGE.zoomFactor #Déf: 1.
PAGE.scrollPosition #{ top, left }
PAGE.goBack|Forward()
PAGE.go(INT) #Page WINDOW.history.back|forward|go()
PAGE.canGoBack|Forward #BOOL
PAGE.navigationLocked #If true (déf: false), cannot go to another URL.
PAGE.evaluate(FUNC([ARGS])[, ARGS]) #Evaluate sync. FUNC() in PAGE context/sandbox. Returns FUNC() return value.
#ARGS and return value must be plain OBJ or native type VAL.
PAGE.evaluateAsync(...) #Same but async., and can't use ARGS nor return value.
PAGE.evaluateJavaScript(STR) #e.g. "function() { ... }"
PAGE.includeJs(URL_STR, FUNC()) #Adds a <script> tag in the page and read it.
PAGE.injectJs(FILE, FUNC()) #Same for FILE. If not found $PWD look into phantom|PAGE.libraryPath
#Returns true if success
phantom.injectJs(...) #Same but for all PAGE
PAGE.sendEvent(EVENT_STR[, ARGS]) #Emit a real DOM event among:
# - "mouseup|down|move|[double]click": ARGS are X, Y, null, "left|right|middle",
# MODIFIER
# - "keyup|press|down": ARGS are KEY, null, null, MODIFIER:
# - KEY is PAGE.event.key[STR], where STR is "Semicolon", "B", etc., or
# the STR directly
# - MODIFIER is PAGE.modifier.shift|ctrl|alt|meta|keypad (can be or'd, 0 for none)
PAGE.uploadFile(SELECTOR, FILE[_ARR]) #Uses an <input type="file"> selected by SELECTOR to upload FILE[_ARR].
PAGE.onCallback = FUNC(OBJ) #Allow to communicate with the PAGE context:
# - PAGE context does:
# if (typeof callPhantom === "function")
# { var VAR = callPhantom(OBJ) }
# - onCallback FUNC(OBJ) receives it, and returns VAL (for VAR)
PAGE.cookies #As OBJ_ARR, where OBJ has members name STR, value STR, domain STR, path STR,
#httponly BOOL, secure BOOL, expires DATE
#Cookies can be store in specific file with phantom ... --cookies-file=FILE
PAGE.addCookie(OBJ) #Returns true if success
PAGE.deleteCookie(STR) #STR is name
PAGE.clearCookies()
phantom.* #Same as above, but for all PAGE
phantom.cookiesEnabled #(déf: true)
PAGE.[frameU|u]rl #Current page/frame|main frame document.location.href
PAGE.[frameT|t]itle #Same for document.title
PAGE.[frameP]|plainText #Same for document.documentElement.textContent
PAGE.[frameC|c]ontent #Same for document.documentElement.outerHTML
PAGE.framesName #<frame> name (not <iframe>) as STR_ARR
PAGE.frameName #Same for current frame
PAGE.framesCount #Same as PAGE.framesName.length
PAGE.switchToChildFrame(UINT|STR)
PAGE.switchToFrame(UINT) #Changes current frame but keeps focused frame with PAGE.focusedFrameName and
PAGE.switchToMain|ParentFrame() #PAGE.switchToFocusedFrame()
PAGE.pages #List of popups as PAGE_ARR.
#If PAGE.ownsPages is false (déf: true), popups does not populate it
PAGE.pagesWindowName #PAGE.pages window.name
PAGE.windowName #Page WINDOW.name
PAGE.getPage(STR) #Returns popup PAGE according to WINDOW.name
PAGE.onPageCreated = FUNC() #Popups with window.open()
PAGE.onAlert = FUNC(STR) #
PAGE.onConfirm = FUNC(STR) #FUNC() returns BOOL
PAGE.onPrompt = FUNC(STR, VAL) #VAL is default value
PAGE.onFilePicker = FUNC(FILE_STR[_ARR])#FUNC() returns FILE2_STR[_ARR] (FILE is old one, FILE2 new one)
PAGE.offlineStorageQuota #Max size of WINDOW.localStorage
#Can also use phantom ... --local-storage-quota=VAL
PAGE.offlineStoragePath #DIR storing SQLite databases of WINDOW.localStorage
#Can also use phantom ... --local-storage-path=PATH
PAGE.onConsoleMessage = FUNC(STR, LINE, #Otherwise by default, console is not redirected to current console.
FILE_STR)) #Use function(str) { console.log(str); } to redirect to console, not console.log nor
#console.log.bind( console ) directly.
PAGE.onError = FUNC(STR, OBJ_ARR) #JavaScript execution error. OBJ has members file and line
phantom.onError #Same for all PAGE
/=+===============================+=\
/ : : \
)==: FS :==(
\ :_______________________________: /
\=+===============================+=/
FS.absolute(PATH) #Like Node PATH.resolve()
FS.changeWorkingDirectory(PATH) #
FS.touch(PATH) #
FS.copy[Tree]|move(PATH, PATH2) #
FS.makeDirectory|Tree(PATH) #
FS.remove[Directory|Tree](PATH) #
FS.exists(PATH) #
FS.isAbsolute|Directory|Executable|File|
Link|Readable|Writable(PATH) #
FS.list() #Returns PATH_ARR
FS.size(PATH) #
FS.readLink(STR) #
FS.open(FILE, OBJ) #OBJ has members STR (e.g. "r") and charset STR.
FS.write(FILE, STR, "w|a") #
FS.read(PATH) #Returns STR
/=+===============================+=\
/ : : \
)==: SYSTEM :==(
\ :_______________________________: /
\=+===============================+=/
SYSTEM.args #Command line arguments as STR_ARR (first is script name)
SYSTEM.env #ENVVAR as OBJ
SYSTEM.pid #PID of phantomjs process
SYSTEM.platform #"phantomjs"
SYSTEM.os #OBJ with members architecture, name and version
/=+===============================+=\
/ : : \
)==: WEBSERVER :==(
\ :_______________________________: /
\=+===============================+=/
WEBSERVER.listen(PORT, #Very simple webserver for communication between the PhantomJS script and the webpage.
FUNC(REQUEST, RESPONSE)) #Look at online doc.
/=+===============================+=\
/ : : \
)==: CHILD_PROCESS :==(
\ :_______________________________: /
\=+===============================+=/
CHILD_PROCESS.spawn(COMMAND_STR
, ARGS_ARR[, { encoding: STR }]) #Returns a CP
CHILD_PROCESS.execFile(...
[, FUNC(ERROR, VAL, VAL2)]) #Same but pipe stdout|err to current one, and call FUNC() on exit.
CP.stdout|err #IOSTREAM
CP.exit(NUM)
CP.on("exit", FUNC(NUM))
CP.kill([STR]) #Déf: "SIGTERM"