-
Notifications
You must be signed in to change notification settings - Fork 3
/
phantomjs.el
298 lines (252 loc) · 9.86 KB
/
phantomjs.el
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
;;; phantomjs.el --- handle phantomjs -*- lexical-binding: t -*-
;; Copyright (C) 2012 Nic Ferrier
;; Author: Nic Ferrier <nferrier@ferrier.me.uk>
;; Maintainer: Nic Ferrier <nferrier@ferrier.me.uk>
;; Created: 12 April 2012
;; Keywords: lisp
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is code for running phantomjs as a child process of Emacs.
;;; Code:
(require 'url)
(eval-when-compile (require 'cl))
(defgroup phantomjs nil
"Customizations for phantomjs handling."
:group 'applications)
(defcustom phantomjs-home "~/phantomjs"
"The location of phantomjs."
:group 'phantomjs
:type '(string))
(defvar phantomjs--proc
(make-hash-table)
"Hash of name->process object.")
(eval-after-load 'phantomjs
(assert (file-exists-p (expand-file-name phantomjs-home))))
(defun phantomjs--sentinel (proc status)
"The sentinel for the phantomjs stuff.
Mostly exists for calling the end process callback."
(cond
((member status '("finished\n"
"killed\n"
"exited abnormally with code 255\n"))
(message "phantomjs got finished signal")
(when (functionp (process-get proc :phantomjs-end-callback))
(funcall (process-get proc :phantomjs-end-callback))))
(t
(message "phantom unexpected status: %s" status))))
;;;###autoload
(defun phantomjs-run (name callback &rest scripts)
(interactive)
"Run phantomjs process with NAME.
CALLBACK is called when it completes.
SCRIPTS is a list of scripts to be passed to the process."
;; need to check phantomjs--proc for name clash
(let ((proc
(apply
'start-process
(append
(list
(symbol-name name)
(concat "* phantomjs-" (symbol-name name) " *")
(expand-file-name (format "%s/bin/phantomjs" phantomjs-home)))
(loop for script in scripts
collect (if (string-match "^http:.*" script)
script
(expand-file-name script)))))))
(process-put proc :phantomjs-end-callback callback)
(set-process-sentinel proc 'phantomjs--sentinel)
(puthash name proc phantomjs--proc)))
(defconst phantomjs--base
(file-name-directory (or load-file-name buffer-file-name))
"The base directory for the phantomjs elisp files.")
(defconst phantomjs--config (concat phantomjs--base "config.js")
"The path to the phantom config file.
A default is included in the package but you could `let' bind
this to override it if necessary.")
;;;###autoload
(defun phantomjs-server (name port complete-callback)
(interactive)
"Run a phantomjs process with NAME and a webserver on PORT.
COMPLETE-CALLBACK is called when it completes.
The webserver running on PORT is used to send commands to the
phantomjs instance using a special protocol.
Returns the process object representing the connection to
phantomjs."
;; need to check phantomjs--proc for name clash
(let* ((server-start-args
(list
(symbol-name name)
(concat "* phantomjs-" (symbol-name name) " *")
(expand-file-name (format "%s/bin/phantomjs" phantomjs-home))
(format "--config=%s"
(expand-file-name phantomjs--config))
(expand-file-name "ghostweb.js" phantomjs--base)
(format "%d" port)))
(proc
(apply 'start-process server-start-args)))
(message "phantomjs server start args %s" server-start-args)
(process-put proc :phantomjs-end-callback complete-callback)
(process-put proc :phantomjs-web-port port)
(process-put proc :phantomjs-lock nil)
(set-process-sentinel proc 'phantomjs--sentinel)
(puthash name proc phantomjs--proc)
proc))
(defcustom phantomjs-callback-debug t
"Whether phantomjs web server callbacks should do debug messages or not.
The messages go to *Messages*."
:group 'phantomjs
:type '(boolean))
(defun phantomjs--callback (status args)
(let (proc
(http-status (save-match-data
(re-search-forward "HTTP/1.1 \\([0-9]+\\) .*" nil 't)
(match-string 1))))
(when phantomjs-callback-debug
(message "phantomjs--callback got status %s" http-status))
(when (string-to-number http-status)
(cond
((listp args)
;; The callback is for a specific user supplied callback.
(setq proc (cadr args))
(funcall (car args) http-status proc))
((processp args)
(setq proc args))
(t
(when phantomjs-callback-debug
(message "phantomjs--callback unknown callback type"))))
;; Unset the lock
(when proc
(when phantomjs-callback-debug
(message "phantomjs--callback releasing the lock"))
(process-put proc :phantomjs-lock nil)
(when phantomjs-callback-debug
(message "phantomjs--callback released the lock: %s %s"
(process-get proc :phantomjs-lock)
proc))))))
(defun phantomjs--wait-for (proc &optional no-grab)
"Wait for lock on PROC to turn off, then immediately set it.
We set it on behalf of the caller, who is waiting to obtain the
lock.
With optional NO-GRAB don't take the lock."
(while (process-get proc :phantomjs-lock)
;; (message "phantomjs--wait-for waiting for lock %s" proc)
(sit-for 0.5))
;; (message "phantomjs--wait-lock released")
(unless no-grab
(process-put proc :phantomjs-lock t)))
(defun phantomjs-open (proc url &optional callback)
"Open URL in PROCESS, the phantomjs instance.
If CALLBACK is specified and a function then, when the url is
opened call CALLBACK in the same way as with `url-retrieve'.
If the CALLBACK is not specified then attempt to wait on a signal
from the process."
(assert (process-get proc :phantomjs-web-port))
(let ((port (process-get proc :phantomjs-web-port))
(url-request-extra-headers
`(("command" . "open")
("commandarg" . ,url))))
(cond
((functionp callback)
(url-retrieve
(format "http://localhost:%d" port)
'phantomjs--callback
(list (list callback proc))))
(t
(phantomjs--wait-for proc)
;; (message "phantomjs-open got lock %s" proc)
(url-retrieve
(format "http://localhost:%d" port)
'phantomjs--callback
(list proc))))))
(defun phantomjs-call (proc javascript &optional callback)
"Call JAVASCRIPT in PROCESS, the phantomjs instance.
When the Javascript completes call CALLBACK in the same way as
with `url-retrieve'."
(assert (process-get proc :phantomjs-web-port))
(let ((port (process-get proc :phantomjs-web-port))
(url-request-extra-headers
`(("command" . "call")
("commandarg" . ,javascript))))
(cond
((functionp callback)
(url-retrieve
(format "http://localhost:%d" port)
'phantomjs--callback
(list (list callback proc))))
(t
;; (message "phantomjs-call waiting for lock %s" proc)
(phantomjs--wait-for proc)
(url-retrieve
(format "http://localhost:%d" port)
'phantomjs--callback
(list proc))))))
(defun phantomjs-exit (proc &optional callback)
"Make PROCESS, the phantomjs instance, exit.
When the exit is acknowledged call CALLBACK in the same way as
with `url-retrieve'."
(assert (process-get proc :phantomjs-web-port))
(let ((port (process-get proc :phantomjs-web-port))
(url-request-extra-headers
`(("command" . "exit"))))
(cond
((functionp callback)
(url-retrieve
(format "http://localhost:%d" port)
'phantomjs--callback
(list (list callback proc))))
(t
;; (message "phantomjs-exit waiting for lock %s" proc)
(phantomjs--wait-for proc)
(url-retrieve
(format "http://localhost:%d" port)
'phantomjs--callback
(list proc))))))
;; Test
(eval-when-compile (require 'ert))
(ert-deftest phantomjs-server ()
"Test running a server.
This test is a little dangerous, there is a lot of waiting in it.
It may get stuck... you can always ctrl-G your way out of it."
(let (finished
blocking
recorded-lock
(server (phantomjs-server 'test 5100
(lambda ()
(setq finished t)))))
(sleep-for 10)
(should-not (process-get server :phantomjs-lock))
(phantomjs-open
server
(concat "file://"
(expand-file-name "test.html" phantomjs--base)))
(should (process-get server :phantomjs-lock))
;; Do it the blocking way, this library does the blocking
(phantomjs-call server "document.getElementById('title').id")
(should (process-get server :phantomjs-lock))
;; Wait for the lock but don't take it
(phantomjs--wait-for server t)
(should-not (process-get server :phantomjs-lock))
;; A sequence of two blocking calls
(phantomjs-call server "document.getElementById('title').id")
(phantomjs-call server "document.getElementById('subtitle').id")
;; Wait for the lock but don't take it
(phantomjs--wait-for server t)
(should-not (process-get server :phantomjs-lock))
(phantomjs-exit server)
(should (process-get server :phantomjs-lock))
(phantomjs--wait-for server t)
(should-not (process-get server :phantomjs-lock))
;; we should test `finished' here
))
(provide 'phantomjs)
;;; phantom.el ends here