-
Notifications
You must be signed in to change notification settings - Fork 4
/
snapshot_generator.js
100 lines (90 loc) · 2.85 KB
/
snapshot_generator.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
/**
* VERY Simple PhantomJS Based Script to snapshot/render ajax pages
*
* @version 1.0
* @see www.klederson.com
* @author Klederson Bueno <[email protected]>
* @licence Creative Commons
*/
var arrayOfUrls = [
"www.flisolcampinas.net/#!/home/",
"www.flisolcampinas.net/#!/sobre/",
"www.flisolcampinas.net/#!/sobre/flisol-campinas/",
"www.flisolcampinas.net/#!/inscreva-se/"
];
// Render Multiple URLs to file
var RenderUrlsToFile, system,fs;
system = require("system");
fs = require("fs");
//define
RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) {
var getFilename, next, page, retrieve, urlIndex, webpage, dest;
urlIndex = 0;
dest = "./snapshots/";
webpage = require("webpage");
page = null;
getFilename = function(url) {
//let's create the snapshot path
var fragment, path, pattern = null;
pattern = new RegExp("#!/(.+)","g");
fragment = pattern.exec(url);
path = (fragment[1].replace("#!/","")).split("/");
//higienize path
if(path[path.length-1] == "")
path = path.slice(0,path.length-1);
//define filename and cleanup path array
var fileName = path.slice(-1) + ".html";
if(path.length > 1) {
path = path.slice(0,path.length-1);
path = path.join("/") + "/";
//create deph of folders
fs.makeTree(path);
fileName = path + fileName;
}
return fileName;
};
next = function(status, url, file) {
page.close();
callbackPerUrl(status, url, file);
return retrieve();
};
retrieve = function() {
var url;
if (urls.length > 0) {
url = urls.shift();
urlIndex++;
page = webpage.create();
page.viewportSize = {
width: 800,
height: 600
};
page.settings.userAgent = "snapshot ro-bot";
return page.open("http://" + url, function(status) {
var file;
file = dest + getFilename(url);
if (status === "success") {
return window.setTimeout((function() {
//page.render(file + ".png");
fs.write(file, page.content,"w");
return next(status, url, file);
}), 200);
} else {
return next(status, url, file);
}
});
} else {
return callbackFinal();
}
};
return retrieve();
};
//calling
RenderUrlsToFile(arrayOfUrls, (function(status, url, file) {
if (status !== "success") {
//return console.log("Unable to render '" + url + "'");
} else {
//return console.log("Rendered '" + url + "' at '" + file + "'");
}
}), function() {
return phantom.exit();
});