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
2ec97ff1
...
2ec97ff130aa511170d98538b7b0de01f9d770e4
authored
2012-01-07 21:34:33 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
updated extending samples
1 parent
452e4506
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
22 deletions
samples/extends.coffee
samples/extends.js
samples/extends.coffee
View file @
2ec97ff
Casper
=
require
(
'casper'
).
Casper
articles
=
[]
CasperClass
=
require
(
'casper'
).
Casper
###
Adds two new methods to the Casper prototype: fetchTexts and renderJSON.
###
Casper
Class
.
extend
Casper
.
extend
# Adds a new navigation step for casper; basically it will:
#
# 1. open an url,
...
...
@@ -23,7 +24,7 @@ CasperClass.extend
@
echo
JSON
.
stringify
what
,
null
,
' '
@
exit
()
casper
=
new
Casper
Class
casper
=
new
Casper
loadImages
:
false
loadPlugins
:
false
logLevel
:
"debug"
...
...
samples/extends.js
View file @
2ec97ff
var
articles
=
[];
var
CasperClass
=
require
(
'casper'
).
Casper
;
var
Casper
=
require
(
"casper"
).
Casper
;
/**
* Adds two new methods to the Casper prototype: fetchTexts and renderJSON.
*/
Casper
Class
.
extend
({
Casper
.
extend
({
/**
* Adds a new navigation step for casper; basically it will:
*
...
...
@@ -12,16 +11,13 @@ CasperClass.extend({
* 2. on loaded, will fetch all contents retrieved through the provided
* CSS3 selector and return them in a formatted object.
*/
fetchTexts
:
function
(
location
,
selector
)
{
return
this
.
thenOpen
(
location
,
function
(
self
)
{
var
texts
=
self
.
evaluate
(
function
(
selector
)
{
var
elements
=
document
.
querySelectorAll
(
selector
);
return
Array
.
prototype
.
map
.
call
(
elements
,
function
(
e
)
{
return
e
.
innerText
;
});
},
{
selector
:
selector
});
articles
=
articles
.
concat
(
texts
);
});
fetchTexts
:
function
(
selector
)
{
return
this
.
evaluate
(
function
(
selector
)
{
var
elements
=
document
.
querySelectorAll
(
selector
);
return
Array
.
prototype
.
map
.
call
(
elements
,
function
(
e
)
{
return
e
.
innerText
;
});
},
{
selector
:
selector
});
},
/**
...
...
@@ -32,20 +28,22 @@ CasperClass.extend({
}
});
var
casper
=
new
Casper
Class
({
var
casper
=
new
Casper
({
loadImages
:
false
,
loadPlugins
:
false
,
logLevel
:
"debug"
,
verbose
:
true
});
casper
.
start
()
;
var
articles
=
[]
;
// all article titles are stored in <h3>
casper
.
fetchTexts
(
'http://www.liberation.fr/'
,
'h3'
);
casper
.
start
(
'http://www.liberation.fr/'
,
function
()
{
articles
=
this
.
fetchTexts
(
'h3'
);
});
// all article titles are stored in <h2 class="article">
casper
.
fetchTexts
(
'http://www.lemonde.fr/'
,
'h2.article'
);
casper
.
thenOpen
(
'http://www.lemonde.fr/'
,
function
()
{
articles
.
concat
(
this
.
fetchTexts
(
'h2.article'
));
});
casper
.
run
(
function
(
self
)
{
self
.
renderJSON
(
articles
);
...
...
Please
register
or
sign in
to post a comment