added Casper.extends() static method for easier extensibility
Showing
2 changed files
with
26 additions
and
15 deletions
... | @@ -607,38 +607,37 @@ casper.start('http://foo.bar/', function(self) { | ... | @@ -607,38 +607,37 @@ casper.start('http://foo.bar/', function(self) { |
607 | 607 | ||
608 | ## Extending Casper | 608 | ## Extending Casper |
609 | 609 | ||
610 | Sometimes it can be convenient to add your own methods to the `Casper` class; it's easily doable as illustrated below: | 610 | Sometimes it can be convenient to add your own methods to the `Casper` class; it's easily doable using the `Casper.extend()` method as illustrated below: |
611 | 611 | ||
612 | ``` javascript | 612 | ``` javascript |
613 | phantom.injectJs("path/to/casper.js"); | 613 | phantom.injectJs("path/to/casper.js"); |
614 | 614 | ||
615 | phantom.Casper.prototype.fetchTexts = function(selector) { | 615 | phantom.Casper.extend({ |
616 | fetchTexts: function(selector) { | ||
616 | return this.evaluate(function() { | 617 | return this.evaluate(function() { |
617 | var elements = document.querySelectorAll(selector); | 618 | var elements = document.querySelectorAll('%selector%'); |
618 | return Array.prototype.map.call(elements, function(e) { | 619 | return Array.prototype.map.call(elements, function(e) { |
619 | return e.innerText; | 620 | return e.innerText; |
620 | }); | 621 | }); |
621 | }, { | 622 | }, { |
622 | selector: selector | 623 | selector: selector.replace("'", "\'") |
623 | }); | 624 | }); |
624 | }; | 625 | }, |
625 | 626 | ||
626 | phantom.Casper.prototype.renderJSON = function(what) { | 627 | renderJSON: function(what) { |
627 | return this.echo(JSON.stringify(what, null, ' ')).exit(); | 628 | return this.echo(JSON.stringify(what, null, ' ')).exit(); |
628 | }; | 629 | } |
630 | }); | ||
629 | 631 | ||
630 | var articles = []; | 632 | var articles = []; |
631 | new phantom.Casper() | 633 | |
632 | .start('http://www.liberation.fr/', function(self) { | 634 | new phantom.Casper().start('http://www.liberation.fr/', function(self) { |
633 | articles = self.fetchTexts('h3'); | 635 | articles = self.fetchTexts('h3'); |
634 | }) | 636 | }).thenOpen('http://www.lemonde.fr/', function(self) { |
635 | .thenOpen('http://www.lemonde.fr/', function(self) { | ||
636 | articles.concat(self.fetchTexts('h2.article')); | 637 | articles.concat(self.fetchTexts('h2.article')); |
637 | }) | 638 | }).run(function(self) { |
638 | .run(function(self) { | ||
639 | self.renderJSON(articles); | 639 | self.renderJSON(articles); |
640 | }) | 640 | }); |
641 | ; | ||
642 | ``` | 641 | ``` |
643 | 642 | ||
644 | ## Licensing | 643 | ## Licensing | ... | ... |
... | @@ -495,6 +495,18 @@ | ... | @@ -495,6 +495,18 @@ |
495 | }; | 495 | }; |
496 | 496 | ||
497 | /** | 497 | /** |
498 | * Extends Casper's prototype with provided one. | ||
499 | * | ||
500 | * @param Object proto Prototype methods to add to Casper | ||
501 | */ | ||
502 | phantom.Casper.extend = function(proto) { | ||
503 | if (typeof(proto) !== "object") { | ||
504 | throw "extends() only accept objects"; | ||
505 | } | ||
506 | mergeObjects(phantom.Casper.prototype, proto); | ||
507 | }; | ||
508 | |||
509 | /** | ||
498 | * Casper client-side helpers. | 510 | * Casper client-side helpers. |
499 | */ | 511 | */ |
500 | phantom.Casper.ClientUtils = function() { | 512 | phantom.Casper.ClientUtils = function() { | ... | ... |
-
Please register or sign in to post a comment