1

I'm fetching an website's content via phantomjs by including jquery with the page. Now i have to write them to a file via program. For that i used the following code

page.onLoadFinished = (function(status) {
    if (status === 'success') {
        page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', function() {
             page.evaluate(function() {

                var mkdirp = require('mkdirp');
                mkdirp(counter+'_folder', function(err) { 
                    var html = $('pre[data-language="html"]').html();
                    var js   = $('pre[data-language="js"]').html();
                    var css  = $('pre[data-language="css"]').html();    
                    var fs = require('fs');
                    fs.writeFile(counter+"_folder/"+"fiddle.html", html, function(err) {}); 
                    fs.writeFile(counter+"_folder/"+"fiddle.css", css, function(err) {}); 
                    fs.writeFile(counter+"_folder/"+"fiddle.js", js, function(err) {}); 
                    console.log("******* "+counter+" *************");
                });
            });
        });
    }
});

page.open(url[counter]);

Now what happening is inside evalute method when I'm using require the program is getting stopped there showing error cannot find variable require. Any idea why this is appering?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Sahasrangshu Guha
  • 672
  • 2
  • 11
  • 29

1 Answers1

1

page.evaluate() is the sandboxed page context. It has no access to require, page, phantom ...

Furthermore, mkdirp is a node module which will not work with PhantomJS. If you want to use PhantomJS from node, you will have to use a bridge like phantom. See also: Use a node module from casperjs

Using that bridge, you have to pass the variables to the outside and save it from there:

page.open(url, function(){
    var mkdirp = require('mkdirp');
    mkdirp(counter+'_folder', function(err) { 
        page.evaluate(function() {
            var html = $('pre[data-language="html"]').html();
            var js   = $('pre[data-language="js"]').html();
            var css  = $('pre[data-language="css"]').html();    
            return [html, js, css];
        }, function(result){
            var fs = require('fs');
            fs.writeFile(counter+"_folder/"+"fiddle.html", stuff[0], function(err) {}); 
            fs.writeFile(counter+"_folder/"+"fiddle.css", stuff[1], function(err) {}); 
            fs.writeFile(counter+"_folder/"+"fiddle.js", stuff[2], function(err) {}); 
            console.log("******* "+counter+" *************");
        });
    });
});

Note: PhantomJS' fs module doesn't have a writeFile function. Node and PhantomJS have different execution environments.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222