-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
69 lines (61 loc) · 1.96 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var viewer;
var $ = function (x) { return [].slice.call(document.querySelectorAll(x)) };
var PNG = require('node-png').PNG;
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var makeImgDataGetter = function (canvas) {
return function (img) {
canvas.setAttribute('width', img.width)
canvas.setAttribute('height', img.height)
var context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
return context.getImageData(0, 0, img.width, img.height);
}
}
var canvas = document.createElement("canvas");
var getImageData = makeImgDataGetter(canvas);
function getPageImage() {
var id = viewer.getPageId()
var num = viewer.getPageNumber()
var img = $('#viewerCanvas img').filter(function (el) {
return (new RegExp(id)).test(el.src);
})[0];
if (!img) return null;
var res = getImageData(img);
res.id = id;
res.num = num;
return res;
}
var prevId;
function nextPage() {
var img = getPageImage();
if (img) {
var png = new PNG({ width: img.width, height: img.height})
var file = path.join(outdir, img.num+'-'+img.id+'.png')
png.data = img.data;
png.pack().pipe(fs.createWriteStream(file));
}
prevId = viewer.getPageId();
viewer.nextPage();
if (viewer.getPageId() === prevId)
alert('Done')
else
window.setTimeout(nextPage, 1000)
// window.setTimeout(nextPage, 700 + Math.floor(Math.random()*1000))
}
var args = require('nw.gui').App.argv;
var bookid = args[0];
if (!bookid) {
process.stderr.write('\nMissing <book-id> argument:\n')
process.stderr.write('Usage:\n\tnw . <book-id>\n# See Supported Identifiers at https://developers.google.com/books/docs/viewer/developers_guide\n')
process.exit(1);
}
var outdir = path.resolve(args[1] || '.');
mkdirp.sync(outdir);
google.load("books", "0");
google.setOnLoadCallback(function initialize() {
viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
viewer.load(bookid);
setTimeout(nextPage, 3000);
})