-
Notifications
You must be signed in to change notification settings - Fork 4
/
crawl.js
107 lines (92 loc) · 3.4 KB
/
crawl.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
module.exports = function (spooky, targetDir, imageFormat, imageQuality, waitTime, maxIndex) {
spooky.then([{ baseDir: targetDir,
format: imageFormat,
quality: imageQuality,
waitTime: waitTime,
maxIndex: maxIndex
}, function () {
var captureFrame = function (casper, imageName) {
var filename = baseDir + '/' + imageName + '.' + format;
casper.capture(filename, undefined, {
format: format,
quality: quality
});
}
var padWithZeroes = function (num) {
var numDigits = maxIndex.toString().length;
if (num <= maxIndex) {
num = (new Array(numDigits).join('0') + num).slice(-numDigits);
}
return num;
};
var getAvailableFragments = function (casper) {
return casper.evaluate(function () {
return Reveal.availableFragments();
});
};
var getSlideIndices = function (casper) {
return casper.evaluate(function () {
return Reveal.getIndices();
});
};
var getFragmentIndex = function (casper) {
return getSlideIndices(casper).f || 0;
};
var forceRepaint = function (casper) {
casper.evaluate(function () {
// Hack from http://stackoverflow.com/questions/3485365/how-can-i-force-webkit-to-redraw-repaint-to-propagate-style-changes
var curSlide = document.getElementsByClassName('present')[0];
curSlide.style.display = 'none';
curSlide.offsetHeight;
curSlide.style.display = 'block';
});
};
var hasFragments = function (casper) {
var fragments = getAvailableFragments(casper);
return fragments.prev || fragments.next;
};
var hasNextFragment = function (casper) {
var fragments = getAvailableFragments(casper);
return fragments.next;
};
var nextSlide = function (casper) {
casper.evaluate(function () {
Reveal.next();
});
};
var nextFragment = function (casper) {
casper.evaluate(function () {
Reveal.nextFragment();
});
forceRepaint(casper);
};
var isLastSlide = function (casper) {
return casper.evaluate(function () {
return Reveal.isLastSlide();
});
};
var isPresDone = function (casper) {
return isLastSlide(casper) && !hasNextFragment(casper);
};
var advancePres = function (casper) {
if (hasNextFragment(casper)) {
nextFragment(casper);
} else {
nextSlide(casper);
}
};
var advance = function (casper, waitTime, frameId) {
casper.wait(waitTime, function () {
captureFrame(casper, padWithZeroes(frameId));
frameId++;
advancePres(casper);
if (!isPresDone(casper)) {
advance(casper, waitTime, frameId);
} else {
captureFrame(casper, padWithZeroes(frameId));
}
});
}
advance(this, waitTime, 0);
}]);
}