forked from amir20/phantomjs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom.js
132 lines (114 loc) · 3.5 KB
/
phantom.js
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
// Generated by CoffeeScript 1.6.2
(function() {
var child, dnode, http, phanta, shoe, startPhantomProcess, wrap,
__slice = [].slice;
dnode = require('dnode');
http = require('http');
shoe = require('shoe');
child = require('child_process');
phanta = [];
startPhantomProcess = function(binary, port, args) {
var ps;
ps = child.spawn(binary, args.concat([__dirname + '/shim.js', port]));
ps.stdout.on('data', function(data) {
return console.log("phantom stdout: " + data);
});
ps.stderr.on('data', function(data) {
if (data.toString('utf8').match(/No such method.*socketSentData/)) {
return;
}
return console.warn("phantom stderr: " + data);
});
ps.on('exit', function(code, signal) {
console.assert(signal == null, "signal killed phantomjs: " + signal);
return console.assert(code === 0, "abnormal phantomjs exit code: " + code);
});
return ps;
};
process.on('exit', function() {
var phantom, _i, _len, _results;
_results = [];
for (_i = 0, _len = phanta.length; _i < _len; _i++) {
phantom = phanta[_i];
_results.push(phantom.exit());
}
return _results;
});
wrap = function(ph) {
ph._createPage = ph.createPage;
return ph.createPage = function(cb) {
return ph._createPage(function(page) {
page._evaluate = page.evaluate;
page.evaluate = function() {
var args, cb, fn;
fn = arguments[0], cb = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
return page._evaluate.apply(page, [fn.toString(), cb].concat(args));
};
return cb(page);
});
};
};
module.exports = {
create: function() {
var arg, args, cb, httpServer, options, phantom, sock, _i, _len, _ref, _ref1;
args = [];
options = {};
for (_i = 0, _len = arguments.length; _i < _len; _i++) {
arg = arguments[_i];
switch (typeof arg) {
case 'function':
cb = arg;
break;
case 'string':
args.push(arg);
break;
case 'object':
options = arg;
}
}
if ((_ref = options.binary) == null) {
options.binary = 'phantomjs';
}
if ((_ref1 = options.port) == null) {
options.port = 12300;
}
phantom = null;
httpServer = http.createServer();
httpServer.listen(options.port);
httpServer.on('listening', function() {
var ps;
ps = startPhantomProcess(options.binary, options.port, args);
return ps.on('exit', function(code) {
var p;
httpServer.close();
if (phantom) {
phantom && phantom.onExit && phantom.onExit();
return phanta = (function() {
var _j, _len1, _results;
_results = [];
for (_j = 0, _len1 = phanta.length; _j < _len1; _j++) {
p = phanta[_j];
if (p !== phantom) {
_results.push(p);
}
}
return _results;
})();
}
});
});
sock = shoe(function(stream) {
var d;
d = dnode();
d.on('remote', function(phantom) {
wrap(phantom);
phanta.push(phantom);
return typeof cb === "function" ? cb(phantom) : void 0;
});
d.pipe(stream);
return stream.pipe(d);
});
return sock.install(httpServer, '/dnode');
}
};
}).call(this);