-
Notifications
You must be signed in to change notification settings - Fork 4
/
capture.js
111 lines (97 loc) · 3.43 KB
/
capture.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
var crawl = require('./crawl.js');
var Spooky = require('spooky');
var path = require('path');
module.exports = function (url, targetDir, resolution, imageQuality, imageFormat, showControls, waitTime, maxIndex, callback) {
var env = process.env;
// forwarding npm@2 and npm@3 modules paths to help Casper find PhantomJS
env.PATH = [
path.join(__dirname, 'node_modules', '.bin'), // npm@3
path.join(__dirname, 'node_modules', 'casperjs', '.bin'), // npm@2
env.PATH
].join(path.delimiter);
var spookyOpts = {
child: {
transport: 'http'
},
casper: {
logLevel: 'debug',
verbose: true,
viewportSize: resolution ? resolution : null
},
child: {
command: require.resolve('casperjs/bin/casperjs'),
spawnOptions: {
env: env
}
}
};
var spooky = new Spooky(spookyOpts, function (err) {
if (err) {
e = new Error('Failed to initialise SpookyJS');
e.details = err;
throw e;
}
spooky.start(url);
testRevealVersion();
if (!resolution) setResolution()
configureReveal();
crawl(spooky, targetDir, imageFormat, imageQuality, waitTime, maxIndex);
spooky.run(function () {
this.emit('finished');
this.exit(0);
});
});
spooky.on('error', function (e, stack) {
console.error(e);
if (stack) {
console.log(stack);
}
});
spooky.on('console', function (line) {
console.log(line);
});
spooky.on('log', function (log) {
if (log.space === 'remote') {
console.log(log.message.replace(/ \- .*/, ''));
}
});
spooky.on('finished', function () {
callback(null);
});
var testRevealVersion = function () {
spooky.then(function () {
var compatible = this.evaluate(function () {
return typeof(Reveal.availableFragments) === 'function'
&& typeof(Reveal.configure) === 'function'
&& typeof(Reveal.getCurrentSlide) === 'function'
&& typeof(Reveal.getIndices) === 'function'
&& typeof(Reveal.isLastSlide) === 'function'
&& typeof(Reveal.next) === 'function'
&& typeof(Reveal.nextFragment) === 'function';
});
if (!compatible) {
this.emit('console', 'This URL is invalid or the version of Reveal.js used in the presentation is too old for Revealation.');
this.die('Fix URL or upgrade Reveal.js', 1);
}
});
}
var setResolution = function () {
spooky.then(function () {
var res = this.evaluate(function () {
return { width: Reveal.getConfig().width,
height: Reveal.getConfig().height };
});
this.emit('console', 'Setting viewport resolution to ' + res.width + 'x' + res.height);
this.viewport(res.width, res.height);
});
}
var configureReveal = function () {
spooky.thenEvaluate([{
showControls: showControls
}, function (showControls) {
Reveal.configure({ controls: showControls,
transition: 'none',
rollingLinks: false });
}]);
}
}