Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Custom binary name name #1

Merged
merged 3 commits into from

1 participant

@sethmcl
Owner

This patch allows the user to specify where to find the phantomjs
executable, rather than forcing the use of the 'phantomjs' executable
which is in the path.

For example, let's say you have two versions of phantomjs installed at
these locations:

/home/user/tools/phantomjs/v1.3/bin/phantomjs
/home/user/tools/phantomjs/v1.6/bin/phantomjs

You can choose whether you want to use version 1.3 or 1.6 when calling
phantom.create() with a new, optional, parameter:

  phantom.create(cb, '/home/user/tools/phantomjs/v1.6/bin/phantomjs')

If this parameter is not specified, then a default of 'phantomjs' will
be used (which will just use whichever phantomjs binary is in your
path).

Example usage

// using the new parameter
var pathToPhantomJsBinary = '/home/user/tools/phantomjs/v1.6/bin/phantomjs';

phantom.create(function(ph) {
  return ph.createPage(function(page) {
    return page.open(loadUrl, function(status) {
      return page.evaluate((function() {
        return document.title;
      }), function(result) {
        return ph.exit();
      });
    });
  });
}, pathToPhantomJsBinary);

// default behavior still works
phantom.create(function(ph) {
  return ph.createPage(function(page) {
    return page.open(loadUrl, function(status) {
      return page.evaluate((function() {
        return document.title;
      }), function(result) {
        return ph.exit();
      });
    });
  });
});
added some commits
@sethmcl Ignoring node_modules 2bd1aea
@sethmcl This patch allows the user to specify where to find the phantomjs
executable, rather than forcing the use of the 'phantomjs' executable
which is in the path.

For example, let's say you have two versions of phantomjs installed at
these locations:

`/home/user/tools/phantomjs/v1.3/bin/phantomjs`
`/home/user/tools/phantomjs/v1.6/bin/phantomjs`

You can choose whether you want to use version 1.3 or 1.6 when calling
phantom.create() with a new, optional, parameter:

```javascript
  phantom.create(cb, '/home/user/tools/phantomjs/v1.6/bin/phantomjs')
```

If this parameter is not specified, then a default of 'phantomjs' will
be used (which will just use whichever phantomjs binary is in your
path).

__Example usage__
```javascript
// using the new parameter
var pathToPhantomJsBinary = '/home/user/tools/phantomjs/v1.6/bin/phantomjs';

phantom.create(function(ph) {
  return ph.createPage(function(page) {
    return page.open(loadUrl, function(status) {
      return page.evaluate((function() {
        return document.title;
      }), function(result) {
        return ph.exit();
      });
    });
  });
}, pathToPhantomJsBinary);

// default behavior still works
phantom.create(function(ph) {
  return ph.createPage(function(page) {
    return page.open(loadUrl, function(status) {
      return page.evaluate((function() {
        return document.title;
      }), function(result) {
        return ph.exit();
      });
    });
  });
});
```
68afdd6
@sethmcl Removing console.log() statement which was being used for debugging 6f482ad
@sethmcl sethmcl merged commit 272f6e4 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 10, 2012
  1. Ignoring node_modules

    authored
Commits on Jul 11, 2012
  1. This patch allows the user to specify where to find the phantomjs

    authored
    executable, rather than forcing the use of the 'phantomjs' executable
    which is in the path.
    
    For example, let's say you have two versions of phantomjs installed at
    these locations:
    
    `/home/user/tools/phantomjs/v1.3/bin/phantomjs`
    `/home/user/tools/phantomjs/v1.6/bin/phantomjs`
    
    You can choose whether you want to use version 1.3 or 1.6 when calling
    phantom.create() with a new, optional, parameter:
    
    ```javascript
      phantom.create(cb, '/home/user/tools/phantomjs/v1.6/bin/phantomjs')
    ```
    
    If this parameter is not specified, then a default of 'phantomjs' will
    be used (which will just use whichever phantomjs binary is in your
    path).
    
    __Example usage__
    ```javascript
    // using the new parameter
    var pathToPhantomJsBinary = '/home/user/tools/phantomjs/v1.6/bin/phantomjs';
    
    phantom.create(function(ph) {
      return ph.createPage(function(page) {
        return page.open(loadUrl, function(status) {
          return page.evaluate((function() {
            return document.title;
          }), function(result) {
            return ph.exit();
          });
        });
      });
    }, pathToPhantomJsBinary);
    
    // default behavior still works
    phantom.create(function(ph) {
      return ph.createPage(function(page) {
        return page.open(loadUrl, function(status) {
          return page.evaluate((function() {
            return document.title;
          }), function(result) {
            return ph.exit();
          });
        });
      });
    });
    ```
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 15 deletions.
  1. +3 −0  .gitignore
  2. +8 −6 phantom.coffee
  3. +16 −9 phantom.js
View
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules/
+*.swo
+*.swp
View
14 phantom.coffee
@@ -3,8 +3,8 @@ express = require 'express'
child = require 'child_process'
phanta = []
-startPhantomProcess = (port, args) ->
- ps = child.spawn 'phantomjs', args.concat [__dirname+'/shim.js', port]
+startPhantomProcess = (port, args, binaryPath) ->
+ ps = child.spawn binaryPath, args.concat [__dirname+'/shim.js', port]
ps.stdout.on 'data', (data) -> console.log "phantom stdout: #{data}"
ps.stderr.on 'data', (data) ->
@@ -27,18 +27,19 @@ wrap = (ph) ->
-module.exports =
- create: (args..., cb) ->
+module.exports =
+ create: (args..., cb, binaryPath) ->
app = express.createServer()
app.use express.static __dirname
-
appServer = app.listen()
server = dnode()
phantom = null
- ps = startPhantomProcess appServer.address().port, args
+ binaryPath = binaryPath || 'phantomjs'
+
+ ps = startPhantomProcess appServer.address().port, args, binaryPath
ps.on 'exit', (code) ->
appServer.close()
@@ -52,6 +53,7 @@ module.exports =
phantom = conn.remote
wrap phantom
phanta.push phantom
+ phantom.__binaryPath = binaryPath
cb? phantom
View
25 phantom.js
@@ -1,6 +1,7 @@
+// Generated by CoffeeScript 1.3.3
(function() {
var child, dnode, express, phanta, startPhantomProcess, wrap,
- __slice = Array.prototype.slice;
+ __slice = [].slice;
dnode = require('dnode');
@@ -10,14 +11,16 @@
phanta = [];
- startPhantomProcess = function(port, args) {
+ startPhantomProcess = function(port, args, binaryPath) {
var ps;
- ps = child.spawn('phantomjs', args.concat([__dirname + '/shim.js', port]));
+ ps = child.spawn(binaryPath, args.concat([__dirname + '/shim.js', port]));
ps.stdout.on('data', function(data) {
return console.log("phantom stdout: " + data);
});
ps.stderr.on('data', function(data) {
- if (data.toString('utf8').match(/No such method.*socketSentData/)) return;
+ if (data.toString('utf8').match(/No such method.*socketSentData/)) {
+ return;
+ }
return console.warn("phantom stderr: " + data);
});
return ps;
@@ -48,14 +51,15 @@
module.exports = {
create: function() {
- var app, appServer, args, cb, io, phantom, ps, server, _i;
- args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
+ var app, appServer, args, binaryPath, cb, io, phantom, ps, server, _i;
+ args = 3 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 2) : (_i = 0, []), cb = arguments[_i++], binaryPath = arguments[_i++];
app = express.createServer();
- app.use(express.static(__dirname));
+ app.use(express["static"](__dirname));
appServer = app.listen();
server = dnode();
phantom = null;
- ps = startPhantomProcess(appServer.address().port, args);
+ binaryPath = binaryPath || 'phantomjs';
+ ps = startPhantomProcess(appServer.address().port, args, binaryPath);
ps.on('exit', function(code) {
var p;
appServer.close();
@@ -64,7 +68,9 @@
_results = [];
for (_j = 0, _len = phanta.length; _j < _len; _j++) {
p = phanta[_j];
- if (p !== phantom) _results.push(p);
+ if (p !== phantom) {
+ _results.push(p);
+ }
}
return _results;
})();
@@ -79,6 +85,7 @@
phantom = conn.remote;
wrap(phantom);
phanta.push(phantom);
+ phantom.__binaryPath = binaryPath;
return typeof cb === "function" ? cb(phantom) : void 0;
});
}
Something went wrong with that request. Please try again.