forked from amir20/phantomjs-node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
phantom.js
97 lines (88 loc) · 2.69 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
// Generated by CoffeeScript 1.3.3
(function() {
var child, dnode, express, phanta, startPhantomProcess, wrap,
__slice = [].slice;
dnode = require('dnode');
express = require('express');
child = require('child_process');
phanta = [];
startPhantomProcess = function(port, args) {
var ps;
ps = child.spawn('phantomjs', 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;
}
if (data.toString('utf8').match(/ApplePersistence=YES/)) {
return;
}
return console.warn("phantom stderr: " + data);
});
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, fn;
fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return page._evaluate.apply(page, [fn.toString()].concat(__slice.call(args)));
};
return cb(page);
});
};
};
module.exports = {
create: function() {
var app, appServer, args, cb, io, phantom, ps, server, _i;
args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
app = express();
app.use(express["static"](__dirname));
appServer = app.listen();
server = dnode();
phantom = null;
ps = startPhantomProcess(appServer.address().port, args);
ps.on('exit', function(code) {
var p;
appServer.close();
return phanta = (function() {
var _j, _len, _results;
_results = [];
for (_j = 0, _len = phanta.length; _j < _len; _j++) {
p = phanta[_j];
if (p !== phantom) {
_results.push(p);
}
}
return _results;
})();
});
io = {
log: null,
'client store expiration': 0
};
return server.listen(appServer, {
io: io
}, function(obj, conn) {
phantom = conn.remote;
wrap(phantom);
phanta.push(phantom);
return typeof cb === "function" ? cb(phantom) : void 0;
});
}
};
}).call(this);