-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstream.js
127 lines (103 loc) · 3.16 KB
/
stream.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
/* global phantom, document, window, btoa */
'use strict';
var system = require('system');
var page = require('webpage').create();
var objectAssign = require('object-assign');
var opts = JSON.parse(system.args[1]);
var log = console.log;
function formatTrace(trace) {
var src = trace.file || trace.sourceURL;
var fn = (trace.function ? ' in function ' + trace.function : '');
return ' → ' + src + ' on line ' + trace.line + fn;
}
console.log = console.error = function () {
system.stderr.writeLine([].slice.call(arguments).join(' '));
};
if (opts.username && opts.password) {
opts.headers = objectAssign({}, opts.headers, {
Authorization: 'Basic ' + btoa(opts.username + ':' + opts.password)
});
}
if (opts.userAgent) {
page.settings.userAgent = opts.userAgent;
}
page.settings.resourceTimeout = (opts.timeout || 60) * 1000;
phantom.cookies = opts.cookies;
phantom.onError = function (err, trace) {
err = err.replace(/\n/g, '');
console.error('PHANTOM ERROR: ' + err + formatTrace(trace[0]));
phantom.exit(1);
};
page.onError = function (err, trace) {
err = err.replace(/\n/g, '');
console.error('WARN: ' + err + formatTrace(trace[0]));
};
page.onResourceError = function (resourceError) {
console.error('WARN: Unable to load resource #' + resourceError.id + ' (' + resourceError.errorString + ') → ' + resourceError.url);
};
page.onResourceTimeout = function (resourceTimeout) {
console.error('Resource timed out #' + resourceTimeout.id + ' (' + resourceTimeout.errorString + ') → ' + resourceTimeout.url);
phantom.exit(1);
};
page.viewportSize = {
width: opts.width,
height: opts.height
};
page.customHeaders = opts.headers || {};
page.zoomFactor = opts.scale;
page.open(opts.url, function (status) {
if (status === 'fail') {
console.error('Couldn\'t load url: ' + opts.url);
phantom.exit(1);
return;
}
if (opts.crop) {
page.clipRect = {
top: 0,
left: 0,
width: opts.width,
height: opts.height
};
}
page.evaluate(function (css, transparent) {
var bgColor = window
.getComputedStyle(document.body)
.getPropertyValue('background-color');
if (!bgColor || bgColor === 'rgba(0, 0, 0, 0)') {
document.body.style.backgroundColor = transparent ? 'transparent' : 'white';
}
if (css) {
var el = document.createElement('style');
el.appendChild(document.createTextNode(css));
document.head.appendChild(el);
}
}, opts.css, opts.transparent);
window.setTimeout(function () {
if (opts.hide) {
page.evaluate(function (els) {
els.forEach(function (el) {
[].forEach.call(document.querySelectorAll(el), function (e) {
e.style.visibility = 'hidden';
});
});
}, opts.hide);
}
if (opts.selector) {
var clipRect = page.evaluate(function (el) {
return document
.querySelector(el)
.getBoundingClientRect();
}, opts.selector);
clipRect.height *= page.zoomFactor;
clipRect.width *= page.zoomFactor;
clipRect.top *= page.zoomFactor;
clipRect.left *= page.zoomFactor;
page.clipRect = clipRect;
}
if (opts.script) {
page.evaluateJavaScript('function () { ' + opts.script + '}');
}
log.call(console, page.renderBase64(opts.format));
phantom.exit();
}, opts.delay * 1000);
});