javascript - Manipulate http response body in CapserJS -


i using casperjs 1.1.0-beta3 combined phantomjs 1.9.8.

how can edit response body content before phantomjs parses , renders it?

something like:

casper.on("resource.received", function(response) {    response.body.add("<h1>bla</h1>"); } 

you can't this. phantomjs not expose body, cannot change it. there some tricks when receive javascript, cannot applied type of resource.

it seems receive html resource.

it page resource

if html resource page resource, can abort request using resource.requested event handler, use __utils__.sendajax retrieve page source, change content based on rules , load current content.

var done = false; casper.on("page.resource.requested", function(req, network){     if (req.url === "someurl") {         network.abort();         var content = this.evaluate(function(url){             return __utils__.sendajax(url, "get");         }, req.url);         content = content.replace("somehtml", "myhtml");         this.page.setcontent(content.replace("href=\"/", "href=\"http://domain.tld/path/"), req.url); // fix urls         done = true;     } }); casper.start(someurltomanipulate).waitfor(function check(){     return done; }, function then(){     // further processing }).run(); 

it other resource

other resources requested using javascript (such templates). in cases, can't anything, because have reproduce code handles such response.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -