Commit 133310d8 133310d814d79db08c3982ee4af31d0a71813b8c by Nicolas Perriault

Initial import.

0 parents
1 Copyright (c) 2011 Nicolas Perriault
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is furnished
8 to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 THE SOFTWARE.
1 # Casper.js
2
3 Casper is a navigation utility for [PhantomJS](http://www.phantomjs.org/).
4
5 More documentation to come soon, I swear. If you just can't wait, here's a sample script:
6
7 phantom.injectJs('casper.js');
8
9 // User defined functions
10 function q() {
11 document.querySelector('input[name="q"]').setAttribute('value', '%term%');
12 document.querySelector('form[name="f"]').submit();
13 }
14
15 function getLinks() {
16 return Array.prototype.map.call(document.querySelectorAll('h3.r a'), function(e) {
17 return e.getAttribute('href');
18 });
19 }
20
21 // Casper suite
22 var links = [];
23 var casper = new phantom.Casper()
24 .start('http://google.fr/')
25 .thenEvaluate(q, {
26 term: 'casper',
27 })
28 .then(function(self) {
29 links = self.evaluate(getLinks);
30 })
31 .thenEvaluate(q, {
32 term: 'homer',
33 })
34 .then(function(self) {
35 links = links.concat(self.evaluate(getLinks));
36 })
37 .run(function(self) {
38 self.echo(JSON.stringify({
39 result: self.result,
40 links: links
41 }, null, ' '));
42 self.exit();
43 })
44 ;
45
46 Run it:
47
48 $ phantomjs example.js
49 {
50 "result": {
51 "log": [
52 {
53 "level": "info",
54 "space": "phantom",
55 "message": "Starting…",
56 "date": "Mon Sep 05 2011 16:10:56 GMT+0200 (CEST)"
57 },
58 {
59 "level": "info",
60 "space": "phantom",
61 "message": "Running suite: 4 steps",
62 "date": "Mon Sep 05 2011 16:10:56 GMT+0200 (CEST)"
63 },
64 {
65 "level": "info",
66 "space": "phantom",
67 "message": "Step 1/4: http://www.google.fr/ (HTTP 301)",
68 "date": "Mon Sep 05 2011 16:10:57 GMT+0200 (CEST)"
69 },
70 {
71 "level": "info",
72 "space": "phantom",
73 "message": "Step 1/4: done in 1259ms.",
74 "date": "Mon Sep 05 2011 16:10:57 GMT+0200 (CEST)"
75 },
76 {
77 "level": "info",
78 "space": "phantom",
79 "message": "Step 2/4: http://www.google.fr/search?sclient=psy&hl=fr&site=&source=hp&q=casper&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301)",
80 "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)"
81 },
82 {
83 "level": "info",
84 "space": "phantom",
85 "message": "Step 2/4: done in 2145ms.",
86 "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)"
87 },
88 {
89 "level": "info",
90 "space": "phantom",
91 "message": "Step 3/4: http://www.google.fr/search?sclient=psy&hl=fr&site=&source=hp&q=casper&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301)",
92 "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)"
93 },
94 {
95 "level": "info",
96 "space": "phantom",
97 "message": "Step 3/4: done in 2390ms.",
98 "date": "Mon Sep 05 2011 16:10:58 GMT+0200 (CEST)"
99 },
100 {
101 "level": "info",
102 "space": "phantom",
103 "message": "Step 4/4: http://www.google.fr/search?sclient=psy&hl=fr&source=hp&q=homer&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl= (HTTP 301)",
104 "date": "Mon Sep 05 2011 16:10:59 GMT+0200 (CEST)"
105 },
106 {
107 "level": "info",
108 "space": "phantom",
109 "message": "Step 4/4: done in 3077ms.",
110 "date": "Mon Sep 05 2011 16:10:59 GMT+0200 (CEST)"
111 },
112 {
113 "level": "info",
114 "space": "phantom",
115 "message": "Done 4 steps in 3077ms.",
116 "date": "Mon Sep 05 2011 16:10:59 GMT+0200 (CEST)"
117 }
118 ],
119 "status": "success",
120 "time": 3077
121 },
122 "links": [
123 "http://fr.wikipedia.org/wiki/Casper_le_gentil_fant%C3%B4me",
124 "http://fr.wikipedia.org/wiki/Casper",
125 "http://casperflights.com/",
126 "http://www.allocine.fr/film/fichefilm_gen_cfilm=13018.html",
127 "/search?q=casper&hl=fr&prmd=ivns&tbm=isch&tbo=u&source=univ&sa=X&ei=cdhkTurpFa364QTB5uGeCg&ved=0CFkQsAQ",
128 "http://www.youtube.com/watch?v=Kuvo0QMiNEE",
129 "http://www.youtube.com/watch?v=W7cW5YlHaeQ",
130 "http://www.imdb.com/title/tt0112642/",
131 "http://blog.caspie.net/",
132 "http://www.casperwy.gov/",
133 "http://www.lequipe.fr/Cyclisme/CyclismeFicheCoureur147.html",
134 "http://homer-simpson-tv.blog4ever.com/",
135 "http://fr.wikipedia.org/wiki/Homer_Simpson",
136 "http://en.wikipedia.org/wiki/Homer",
137 "/search?q=homer&hl=fr&prmd=ivnsb&tbm=isch&tbo=u&source=univ&sa=X&ei=cthkTr73Hefh4QSUmt3UCg&ved=0CEQQsAQ",
138 "http://www.youtube.com/watch?v=Ajd08hgerRo",
139 "http://www.koreus.com/video/homer-simpson-photo-39-ans.html",
140 "http://www.nrel.gov/homer/",
141 "http://www.luds.net/homer.php",
142 "http://www.thesimpsons.com/bios/bios_family_homer.htm",
143 "http://www.homeralaska.org/",
144 "http://homeralaska.com/"
145 ]
146 }
147
148 ## Now what
149
150 Feel free to play with the code and report an issue on github. I'm also reachable [on twitter](https://twitter.com/n1k0).
This diff is collapsed. Click to expand it.
1 phantom.injectJs('casper.js');
2
3 function q() {
4 document.querySelector('input[name="q"]').setAttribute('value', '%term%');
5 document.querySelector('form[name="f"]').submit();
6 }
7
8 function getLinks() {
9 return Array.prototype.map.call(document.querySelectorAll('h3.r a'), function(e) {
10 return e.getAttribute('href');
11 });
12 }
13
14 var links = [];
15 var casper = new phantom.Casper({
16 logLevel: "info",
17 verbose: true
18 })
19 .start('http://google.fr/')
20 .thenEvaluate(q, {
21 term: 'casper',
22 })
23 .then(function(self) {
24 links = self.evaluate(getLinks);
25 })
26 .thenEvaluate(q, {
27 term: 'homer',
28 })
29 .then(function(self) {
30 links = links.concat(self.evaluate(getLinks));
31 })
32 .run(function(self) {
33 self.echo(JSON.stringify({
34 result: self.result,
35 links: links
36 }, null, ' '));
37 self.exit();
38 })
39 ;