Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
1e8f2c59
...
1e8f2c594104c60926385b5b4b6f23de30ad938f
authored
2011-10-09 23:25:42 +0200
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added new sample: googlematch.js
1 parent
a3408692
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
samples/googlematch.js
samples/googlematch.js
0 → 100644
View file @
1e8f2c5
/**
* Takes provided terms passed as arguments and query google for the number of
* estimated results each have.
*
* Usage:
* $ phantomjs samples/googlematch.js nicolas chuck borris
* nicolas: 69600000
* chuck: 49500000
* borris: 2370000
* winner is "nicolas" with 69600000 results
*/
phantom
.
injectJs
(
'casper.js'
);
phantom
.
Casper
.
extend
({
fetchScore
:
function
()
{
return
this
.
evaluate
(
function
()
{
var
result
=
document
.
querySelector
(
'#resultStats'
).
innerText
;
return
Number
(
/Environ
([
0-9
\s]{1,})
.*/
.
exec
(
result
)[
1
].
replace
(
/
\s
/g
,
''
));
});
}
});
var
casper
=
new
phantom
.
Casper
({
verbose
:
true
}),
terms
=
phantom
.
args
,
scores
=
[],
i
=
0
;
if
(
terms
.
length
<
2
)
{
casper
.
log
(
'usage: phantomjs googlematch.js term1, term2 [, term3]...'
).
exit
();
}
casper
.
start
(
"http://google.fr/"
);
casper
.
repeat
(
terms
.
length
,
function
(
self
)
{
self
.
then
((
function
(
casper
,
i
)
{
return
function
(
self
)
{
self
.
fill
(
'form[name=f]'
,
{
q
:
terms
[
i
]
},
true
);
};
})(
self
,
i
));
self
.
then
((
function
(
casper
,
i
)
{
return
function
(
self
)
{
var
term
=
terms
[
i
],
score
=
self
.
fetchScore
();
scores
.
push
({
term
:
term
,
score
:
score
});
self
.
echo
(
term
+
': '
+
score
);
};
})(
self
,
i
));
i
++
;
});
casper
.
run
(
function
(
self
)
{
scores
.
sort
(
function
(
a
,
b
)
{
return
a
.
score
-
b
.
score
;
});
var
winner
=
scores
[
scores
.
length
-
1
];
self
.
echo
(
'winner is "'
+
winner
.
term
+
'" with '
+
winner
.
score
+
' results'
)
self
.
exit
();
});
Please
register
or
sign in
to post a comment