-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathserver.js
90 lines (69 loc) · 2.05 KB
/
server.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
var server = require('webserver').create(),
system = require('system'),
fs = require('fs'),
port = system.env.PORT || 8080;
var service = server.listen(port, function(request, response) {
if(request.method == 'POST' && request.post.url){
var url = request.post.url;
request_page(url, function(properties, imageuri){
response.statusCode = 200;
response.write(JSON.stringify(properties));
response.write("\n");
response.write(imageuri);
response.close();
})
} else {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.write(fs.read('index.html'));
response.close();
}
});
if(service) console.log("server started - http://localhost:" + server.port);
function request_page(url, callback){
var page = new WebPage();
page.clipRect = { top: 0, left: 0, width: 700, height: 400 };
page.viewportSize = { width: 700, height: 400 };
page.onLoadStarted = function () {
console.log('loading:' + url);
};
page.onLoadFinished = function (status) {
console.log('loaded:' + url);
var properties = {};
properties.title = page.evaluate(function () {
return document.title
});
properties.links = page.evaluate(function () {
return Object.keys(
[].reduce.call(
document.querySelectorAll('a'),
function(memo, a){
if(a.protocol.indexOf('http') === 0) memo[a.href] = true;
return memo;
}
,{})
)
});
properties.link_areas = page.evaluate(function () {
var sizes = [].reduce.call(document.querySelectorAll('a'), function(memo, a){
var bb = a.getBoundingClientRect(),
area = bb.width * bb.height,
href = a.getAttribute('href');
// update the map
if(area){
memo[href] = (memo[href] || 0) + area;
}
return memo;
},{});
return Object.keys(sizes).map(function(url){
return [url, sizes[url]];
});
})
setTimeout(function(){
var imageuri = 'data:image/png;base64,' + page.renderBase64('png');
callback(properties,imageuri);
page.close();
},100)
};
page.open(url);
}