Using Multiple page.open in Single Script

前端 未结 4 1620
感动是毒
感动是毒 2020-11-27 04:36

My goal is to execute PhantomJS by using:

// adding $op and $er for debugging purposes
exec(\'phantomjs script.js\', $op, $er);
print_r($op);
echo $er;
         


        
4条回答
  •  旧巷少年郎
    2020-11-27 04:48

    Using Queued Processes, sample:

    var page = require('webpage').create();
    
    // Queue Class Helper
    var Queue = function() {
        this._tasks = [];
    };
    Queue.prototype.add = function(fn, scope) {
        this._tasks.push({fn: fn,scope: scope});
        return this;
    };
    Queue.prototype.process = function() {
        var proxy, self = this;
        task = this._tasks.shift();
        if(!task) {return;}
        proxy = {end: function() {self.process();}};
        task.fn.call(task.scope, proxy);
        return this;        
    };
    Queue.prototype.clear = function() {
        this._tasks = []; return this;
    };
    
    // Init pages .....  
    var q = new Queue();       
    
    q.add(function(proxy) {
      page.open(url1, function() {
        // page.evaluate
        proxy.end();
      });            
    });
    
    q.add(function(proxy) {
      page.open(url2, function() {
        // page.evaluate
        proxy.end();
      });            
    });
    
    
    q.add(function(proxy) {
      page.open(urln, function() {
        // page.evaluate
        proxy.end();
      });            
    });
    
    // .....
    
    q.add(function(proxy) {
      phantom.exit()
      proxy.end();
    });
    
    q.process();
    

    I hope this is useful, regards.

提交回复
热议问题