-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_shot.js
67 lines (56 loc) · 2.15 KB
/
template_shot.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
var page = require('webpage').create(),
system = require('system'),
fs = require("fs"),
address, selector, images;
if (system.args.length < 4) {
console.log('Usage: ' + system.args[0] + ' <some URL> <some selector> <destination.png>');
phantom.exit();
}
address = system.args[1];
selector = system.args[2].split(':');
output = system.args[3];
images = [];
page.viewportSize = { width: 800, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to access the network!');
} else {
var i, len;
for (i = 0,len = selector.length; i < len; i++) {
var dimetions = page.evaluate(function (selector) {
el = document.querySelector(selector);
if(el == null) {
return null;
}
var curTop = 0, curLeft = 0, obj = el;
do {
curLeft += obj.offsetLeft;
curTop += obj.offsetTop;
} while (obj = obj.offsetParent);
return {
height: el.offsetHeight,
width: el.offsetWidth,
top: curTop,
left: curLeft
}
}, selector[i]);
page.clipRect = { top: dimetions.top, left: dimetions.left, width: dimetions.width, height: dimetions.height };
images.push(page.renderBase64('png'));
}
page.evaluate(function (image_data, selector) {
var i, len;
for (i = 0,len = selector.length; i < len; i++) {
if (image_data[i] == null) continue;
var img = new Image();
img.src = "data:image/png;base64," + image_data[i].toString();
el = document.querySelector(selector[i]);
el.innerHTML = "";
el.appendChild(img);
}
// Delete all script nodes.
// var scripts = document.querySelectorAll('script').delete ??
}, images, selector);
fs.write(output, page.content, 'w');
}
phantom.exit();
});