Commit 63f5f3be 63f5f3be8fbc6db1f80adc1fc9fde85fffa38b85 by Nicolas Perriault

fixes #122 - allow downloads to be triggered by more than just GET requests

1 parent ab761929
1 /**
2 * Check out sizes of resources loaded on a given web page.
3 *
4 * Usage:
5 * $ casperjs samples/pageweight.js <url>
6 */
7 var casper = require('casper').create();
8 var size = 0;
9
10 function kb(size) {
11 return Math.round(size / 1024 * 100) / 100;
12 }
13
14 casper.on('resource.received', function(r) {
15 if (r.stage !== "end") {
16 return;
17 }
18 var rSize;
19 r.headers.forEach(function(h) {
20 console.log(h.name);
21 });
22 if (r.bodySize) {
23 rSize = ~~r.bodySize;
24 } else {
25 try {
26 rSize = ~~r.headers.filter(function(h) {
27 return h.name.toLowerCase() === "content-length";
28 }).pop().value;
29 } catch (e) {
30 // try actual content length
31 if (this.getCurrentUrl() === r.url) {
32 rSize = casper.page.content.length;
33 }
34 }
35 }
36 this.echo(r.url + ': ' + rSize);
37 size += rSize || 0;
38 });
39
40 casper.on('load.finished', function(status) {
41 this.echo('Total: ' + kb(size) + 'KB').exit();
42 });
43
44 casper.start(casper.cli.get(0));
45
46 casper.run(function() {
47
48 });