forked from amir20/phantomjs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom.js
97 lines (87 loc) · 2.46 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
(function() {
var PORT, child, dnode, express, listenOnSomePort, phanta, startPhantomProcess, wrap;
dnode = require('dnode');
express = require('express');
child = require('child_process');
listenOnSomePort = function(app) {
try {
// Use a random port. Node 0.6.* no longer throws exceptions within 'listen'.
app.listen();
return app.address().port;
} catch (err) {
if (err.code === "EADDRINUSE") {
_results.push(startPort++);
} else {
throw err;
}
}
};
phanta = [];
startPhantomProcess = function(port) {
var ps;
ps = child.spawn('phantomjs', [__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);
});
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(fn, cb) {
return page._evaluate(fn.toString(), cb);
};
return cb(page);
});
};
};
module.exports = {
create: function(cb) {
var app, phantom, port, ps, server;
app = express.createServer();
app.use(express.static(__dirname));
port = listenOnSomePort(app);
server = dnode();
phantom = null;
ps = startPhantomProcess(port);
ps.on('exit', function(code) {
var p;
app.close();
return phanta = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = phanta.length; _i < _len; _i++) {
p = phanta[_i];
if (p !== phantom) _results.push(p);
}
return _results;
})();
});
return server.listen(app, {
io: {
log: null
}
}, function(obj, conn) {
phantom = conn.remote;
wrap(phantom);
phanta.push(phantom);
return typeof cb === "function" ? cb(phantom) : void 0;
});
}
};
}).call(this);