pageweight.js
1.08 KB
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
/**
* Check out sizes of resources loaded on a given web page.
*
* Usage:
* $ casperjs samples/pageweight.js <url>
*/
var casper = require('casper').create();
var size = 0;
function kb(size) {
return Math.round(size / 1024 * 100) / 100;
}
casper.on('resource.received', function(r) {
if (r.stage !== "end") {
return;
}
var rSize;
r.headers.forEach(function(h) {
console.log(h.name);
});
if (r.bodySize) {
rSize = ~~r.bodySize;
} else {
try {
rSize = ~~r.headers.filter(function(h) {
return h.name.toLowerCase() === "content-length";
}).pop().value;
} catch (e) {
// try actual content length
if (this.getCurrentUrl() === r.url) {
rSize = casper.page.content.length;
}
}
}
this.echo(r.url + ': ' + rSize);
size += rSize || 0;
});
casper.on('load.finished', function(status) {
this.echo('Total: ' + kb(size) + 'KB').exit();
});
casper.start(casper.cli.get(0));
casper.run(function() {
});