【问题标题】:Phantomjs / Node.js - Refactoring block of codePhantomjs / Node.js - 重构代码块
【发布时间】:2015-04-02 12:58:29
【问题描述】:

我正在为我工​​作的公司编写一个测试应用程序,使用 Node.js 和 Phantomjs。现在,我的代码的相关部分是这样的:

phantom.create(function (ph) {
        ph.createPage(function (page) {
                page.set('viewportSize', { width: 1920, height: 1080 });
                page.set('settings.javascriptEnabled', true);
                page.set('settings.loadImages', true);
                page.set('settings.localToRemoteUrlAccess', true);
                page.set('settings.userAgent', userAgentStrings[randomInt(0, 5)]);
                page.set('settings.webSecurityEnabled', false);
                page.set('settings.resourceTimeout', 5000);

                page.open(URL, function (status) {
                        if (status == 'success') {
                                page.evaluate(function (result) {
                                        return document.title;
                                }, function (result) {
                                        setTimeout(function () {
                                                log.info('Status: ', status);
                                                ph.exit();
                                        }, 60 * 1000);
                                });
                        } else if (status == 'fail') {
                                log.error('Status: ', status);
                                ph.exit();
                        }
                });
        });
});

我的问题是:有没有办法重构我的代码,使我可以从“phantom.create(...”块外部调用“page.open(...”函数)?

我计划实现 node-cron 并有一段代码用于设置页面的所有选项,而另一段代码将实际用于打开页面。最后,开头部分将由 node-cron 处理,无限重复。

【问题讨论】:

  • 问题是,你为什么要这么做?您认为您是否因此而节省了一些资源,或者您是否认为您提高了响应时间?我认为你不会实现其中任何一个。

标签: node.js refactoring phantomjs


【解决方案1】:

这是一个关于如何执行此操作的简单示例。您只需将幻影对象存储在某处并重用它。请注意,我将其简化,以便您可以重用该概念,但您需要更多的错误处理。

var jobRunner = function() {
  // The phantom object will be stored here
  this.ph;

  // The page object will be stored here
  this.page;
};

jobRunner.prototype.start = function(readyCallback) {
  var self = this;

  phantom.create(function (ph) {
    self.ph = ph;
    self.ph.createPage(function (page) {
      page.set('viewportSize', { width: 1920, height: 1080 });
      page.set('settings.javascriptEnabled', true);
      page.set('settings.loadImages', true);
      page.set('settings.localToRemoteUrlAccess', true);
      page.set('settings.userAgent', userAgentStrings[randomInt(0, 5)]);
      page.set('settings.webSecurityEnabled', false);
      page.set('settings.resourceTimeout', 5000);

      self.page = page;
      readyCallback();
    });
  });
};

jobRunner.prototype.doUrl = function(url) {
  var self = this;
  this.page.open(URL, function (status) {
    if (status == 'success') {
      page.evaluate(function (result) {
        return document.title;
      }, function (result) {
        setTimeout(function () {
          log.info('Status: ', status);
          self.ph.exit();
        }, 60 * 1000);
      });
    } else if (status == 'fail') {
      log.error('Status: ', status);
      self.ph.exit();
    }
  });
}

var CronJob = require('cron').CronJob;
var phantomJob = new jobRunner();

// Wait for Phantom to be ready then start the Cron Job.
phantomJob.start(function() {
  var cron = new CronJob('*/5 * * * * *', function() {
    phantomJob.doUrl("http://yoururl.com");
  });
});

【讨论】:

  • 首先,感谢您的出色回答。现在,您能否详细说明我可能实施的错误处理?你看起来像是我可以借鉴的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多