forked from amir20/phantomjs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim.coffee
78 lines (57 loc) · 2.23 KB
/
shim.coffee
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
# Require gets overwritten by browserify, so we have to reimplement it from scratch - boo :(
webpage = core_require('webpage');
shoe = require('shoe');
dnode = require('dnode');
[port] = phantom.args
# controlPage = webpage.create()
fnwrap = (target) -> -> target.apply this, arguments
# Descend into objects with dotted keys
descend = (op, obj, key, val) ->
cur = obj
keys = key.split '.'
cur = cur[keys.shift()] while keys.length > 1
cur[keys[0]] = val if op is 'set'
cur[keys[0]]
mkwrap = (src, pass=[], special={}) ->
obj =
set: (key, val, cb=->) ->
#Fnwrap so PhantomJS doesn't segfault when it tries to call the callback
val = fnwrap val if typeof val is "function"
cb descend 'set', src, key, val
get: (key, cb) -> cb descend 'get', src, key
for k in pass
do (k) ->
obj[k] = (args...) ->
# This idempotent tomfoolery is required to stop PhantomJS from segfaulting
args[i] = fnwrap arg for arg, i in args when typeof arg is 'function'
src[k] args...
for own k of special
obj[k] = special[k]
obj
pageWrap = (page) -> mkwrap page,
['open','close','includeJs','sendEvent','release','uploadFile','close','goBack','goForward']
injectJs: (js, cb=->) -> cb page.injectJs js
evaluate: (fn, cb=(->), args...) -> cb page.evaluate.apply(page, [fn].concat(args))
render: (file, cb=->) -> page.render file; cb()
renderBase64: (type, cb=->) -> cb page.renderBase64 type
setHeaders: (headers, cb=->) -> page.customHeaders = headers; cb()
setContent: (html, url, cb=->) ->
page.onLoadFinished = (status) ->
page.onLoadFinished = null
cb status
page.setContent html, url
setViewportSize: (width, height, cb=->) ->
page.viewportSize = {width:width, height:height}; cb()
_phantom = mkwrap phantom,
['exit'],
injectJs: (js, cb=->) -> cb phantom.injectJs js
getCookies: (cb=->) -> cb(phantom.cookies)
addCookie: (name, value, domain, cb=->) ->
cookie = {name:name, value:value, domain:domain}
cb(phantom.addCookie(cookie))
clearCookies: (cb=->) -> cb phantom.clearCookies()
createPage: (cb) -> cb pageWrap webpage.create()
stream = shoe('http://localhost:' + port + '/dnode')
d = dnode _phantom
d.pipe stream
stream.pipe d