-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (44 loc) · 1.49 KB
/
index.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
var screenshotSelector = exports.screenshotSelector = function(path, selector, cb) {
if (typeof cb != 'function') {
cb = () => { };
}
return function(nightmare) {
function _screenshot(url, rect) {
nightmare.goto(url)
.screenshot(path, rect)
.run(cb);
}
function _evaluate(url) {
nightmare.goto(url)
.evaluate(function(_selector) {
var _element = document.querySelector(_selector);
if (_element) {
var rect = _element.getBoundingClientRect();
return {
x: Math.round(rect.left),
y: Math.round(rect.top),
width: Math.round(rect.width),
height: Math.round(rect.height)
};
}
}, selector)
.run((err, rect) => {
if (err) {
cb(err);
} else if (!rect) {
cb(new Error('invalid selector' + selector));
} else {
_screenshot(url, rect);
}
});
}
nightmare.url()
.run(function(err, url) {
if (err) {
cb(err);
} else {
_evaluate(url);
}
});
};
};