forked from bokeh/bokeh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phantomjs_screenshot.js
72 lines (60 loc) · 1.66 KB
/
phantomjs_screenshot.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
var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
var url = system.args[1];
var png = system.args[2];
var wait = system.args[3];
var width = system.args[4];
var height = system.args[5];
var errors = [];
var messages = [];
var resources = [];
page.onError = function(msg, trace) {
errors.push({
msg: msg,
trace: trace.map(function(item) {
return { file: item.file, line: item.line };
}),
});
};
page.onConsoleMessage = function(msg, line, source) {
messages.push({
msg: msg,
line: line,
source: source,
});
};
page.onResourceReceived = function(response) {
if (response.stage === 'end') {
var status = response.status;
if (response.url.slice(0, 7) === "file://") {
var path = response.url.slice(7);
if (!fs.exists(path)) {
response.status = 404;
response.statusText = "NOT FOUND";
resources.push(response);
}
} else if (status && status >= 400) {
resources.push(response);
}
}
};
page.viewportSize = { width: width, height: height };
page.open(url, function(status) {
page.evaluate(function() {
document.body.bgColor = 'white';
});
// TODO: get notified when Bokeh finished rendering
window.setTimeout(function() {
if (png !== undefined) {
page.render(png);
}
console.log(JSON.stringify({
status: status,
errors: errors,
messages: messages,
resources: resources,
}));
phantom.exit();
}, wait);
});