fixes #122 - allow downloads to be triggered by more than just GET requests
Showing
1 changed file
with
48 additions
and
0 deletions
samples/pageweight.js
0 → 100644
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 | }); |
-
Please register or sign in to post a comment