OLD | NEW |
1 #!mojo:js_content_handler | 1 #!mojo:js_content_handler |
2 // Demonstrate use of the Mojo network service to load a URL. To run this | 2 // Demonstrate use of the Mojo network service to load a URL. To run this |
3 // application with mojo_shell, set DIR to be the absolute path for this | 3 // application with mojo_shell, set DIR to be the absolute path for this |
4 // directory, then: | 4 // directory, then: |
5 // mojo_shell "file://$DIR/wget.js http://www.google.com" | 5 // mojo_shell "file://$DIR/wget.js http://www.google.com" |
6 // Substitute any URL for google.com. To keep the noise down, this app | 6 // Substitute any URL for google.com. To keep the noise down, this app |
7 // only displays the number of bytes read and a little http header info. | 7 // only displays the number of bytes read and a little http header info. |
8 | 8 |
9 define("main", [ | 9 define("main", [ |
10 "console", | 10 "console", |
(...skipping 11 matching lines...) Expand all Loading... |
22 initialize(args) { | 22 initialize(args) { |
23 if (args.length != 2) { | 23 if (args.length != 2) { |
24 console.log("Expected URL argument"); | 24 console.log("Expected URL argument"); |
25 return; | 25 return; |
26 } | 26 } |
27 | 27 |
28 var netService = this.shell.connectToService( | 28 var netService = this.shell.connectToService( |
29 "mojo:network_service", NetworkService); | 29 "mojo:network_service", NetworkService); |
30 | 30 |
31 var urlLoader; | 31 var urlLoader; |
32 netService.createURLLoader(function(x){urlLoader = x;}); | 32 netService.createURLLoader(function(urlLoaderProxy) { |
| 33 urlLoader = urlLoaderProxy; |
| 34 }); |
33 | 35 |
34 var urlRequest = new URLRequest({ | 36 var urlRequest = new URLRequest({ |
35 url: args[1], | 37 url: args[1], |
36 method: "GET", | 38 method: "GET", |
37 auto_follow_redirects: true | 39 auto_follow_redirects: true |
38 }); | 40 }); |
39 | 41 |
40 var app = this; | 42 var app = this; |
41 urlLoader.start(urlRequest).then(function(result) { | 43 urlLoader.start(urlRequest).then(function(result) { |
42 console.log("url => " + result.response["url"]); | 44 console.log("url => " + result.response["url"]); |
43 console.log("status_line => " + result.response["status_line"]); | 45 console.log("status_line => " + result.response["status_line"]); |
44 console.log("mime_type => " + result.response["mime_type"]); | 46 console.log("mime_type => " + result.response["mime_type"]); |
45 | 47 |
46 core.drainData(result.response.body).then( | 48 core.drainData(result.response.body).then( |
47 function(result) { | 49 function(result) { |
48 console.log("read " + result.buffer.byteLength + " bytes"); | 50 console.log("read " + result.buffer.byteLength + " bytes"); |
49 }) | 51 }) |
50 .then(function() { | 52 .then(function() { |
51 app.quit(); | 53 app.quit(); |
52 }); | 54 }); |
53 }); | 55 }); |
54 } | 56 } |
55 } | 57 } |
56 | 58 |
57 return WGet; | 59 return WGet; |
58 }); | 60 }); |
OLD | NEW |