text: // Generated by CoffeeScript 1.6.3
  (function() {
    var EventEmitter, phantom,
      __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
      __hasProp = {}.hasOwnProperty,
      __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
      __slice = [].slice,
      __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  
    EventEmitter = require('events').EventEmitter;
  
    phantom = require('phantom');
  
    module.exports.Browser = (function(_super) {
      __extends(Browser, _super);
  
      function Browser(options, fn) {
        var _base, _base1, _base2, _base3, _base4;
        this.options = options != null ? options : {};
        if (fn == null) {
          fn = (function() {});
        }
        this.open = __bind(this.open, this);
        this.onPageAlert = __bind(this.onPageAlert, this);
        this.onPageConsoleMessage = __bind(this.onPageConsoleMessage, this);
        this.onPageError = __bind(this.onPageError, this);
        this.onResourceReceive = __bind(this.onResourceReceive, this);
        this.onResourceRequested = __bind(this.onResourceRequested, this);
        this.init = __bind(this.init, this);
        this.evaluate = __bind(this.evaluate, this);
        this.debug = __bind(this.debug, this);
        if ((_base = this.options).exlude_domains == null) {
          _base.exlude_domains = [];
        }
        if ((_base1 = this.options).verbose == null) {
          _base1.verbose = false;
        }
        if ((_base2 = this.options).user_agent == null) {
          _base2.user_agent = 'node';
        }
        if ((_base3 = this.options).base_url == null) {
          _base3.base_url = '';
        }
        if ((_base4 = this.options).auto_screenshot == null) {
          _base4.auto_screenshot = false;
        }
        this.debug('init');
        this.init(fn);
      }
  
      Browser.prototype.debug = function() {
        var args;
        args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
        if (this.options.verbose) {
          return console.log.apply(console, ["Browser>"].concat(__slice.call(args)));
        }
      };
  
      Browser.prototype.evaluate = function() {
        var args, fn, script, _ref;
        script = arguments[0], fn = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
        this.debug('Evaluating: ', script.toString().replace(/\n/g, ' ').slice(0, 100) + '...');
        return (_ref = this.page).evaluate.apply(_ref, [script, fn].concat(__slice.call(args)));
      };
  
      Browser.prototype.init = function(fn) {
        var _this = this;
        if (fn == null) {
          fn = null;
        }
        return phantom.create(function(ph) {
          _this.ph = ph;
          return _this.ph.createPage(function(page) {
            _this.page = page;
            _this.page.set('viewportSize', {
              width: 1546,
              height: 2048
            });
            _this.page.set('loadImages', false);
            _this.page.set('onConsoleMessage', _this.onPageConsoleMessage);
            _this.page.set('onAlert', _this.onPageAlert);
            _this.page.set('onError', _this.onPageError);
            _this.page.set('onResourceRequested', _this.onResourceRequested);
            _this.page.set('onResourceReceived', _this.onResourceReceived);
            _this.page.set('settings.userAgent', _this.options.user_agent);
            _this.emit('ready');
            if (fn) {
              return fn();
            }
          });
        });
      };
  
      Browser.prototype.onResourceRequested = function(request) {
        var domain;
        domain = request.url.split(/\//)[2];
        if (__indexOf.call(this.options.exclude_domains, domain) < 0) {
          this.debug('Skipping Resource', request.url);
          return false;
        }
      };
  
      Browser.prototype.onResourceReceive = function(response) {
        return this.debug('ResourceResponse>', response.id);
      };
  
      Browser.prototype.onPageError = function(msg, trace) {
        return this.debug('Error> ', msg, trace);
      };
  
      Browser.prototype.onPageConsoleMessage = function(msg, line, source) {
        return this.debug('Console> ', msg, line, source);
      };
  
      Browser.prototype.onPageAlert = function(msg) {
        return this.debug('Alert> ', msg);
      };
  
      Browser.prototype.open = function(path) {
        var url,
          _this = this;
        url = "" + this.options.base_url + path;
        this.debug("url", url);
        return this.page.open(url, function(status) {
          if (!status) {
            return;
          }
          _this.emit('open', path);
          _this.emit("open::" + path);
          _this.debug("opened status", status);
          if (_this.options.auto_screenshot) {
            return _this.page.render("last.png");
          }
        });
      };
  
      return Browser;
  
    })(EventEmitter);
  
  }).call(this);
  