スタック・オーバーフローに参加する
680万人以上のプログラマーが集まるスタック・オーバーフローに参加しませんか?
簡単な登録後、すぐにご利用いただけます。
登録

I was wondering how we could set cli parameters inside our code and not by placing them at the end of our command like this:

casperjs casper_tor.js --proxy=127.0.0.1:9050 --proxy-type=socks5

I've tested things like that but it didn't work:

var casper=require('casper').create();

casper.cli.options["proxy"] = "127.0.0.1:9050";
casper.cli.options["proxy-type"] = "socks5";
...
casper.run();

What I'm trying to achieve is to set new proxies inside my code and to scrap my new ip address from whatsmyip.com to check that everything is right (I'm writing bots that will frequently change their proxies).

share|improve this question
1  
You can add a config array in create but proxy and proxy-type are phantomjs options. – Brice Favre Jan 31 '14 at 10:07
    
Thank you! Do you know how I can access them? – Julien Le Coupanec Jan 31 '14 at 11:12

This is not strictly an answer to your question, but to the more general:

How can I write a single script that will be run by CasperJS using specific CLI options?

There is no clean solution using a single script file, because the "shebang" line #!/bin/... is very limited. In fact, on most OS it only supports a single argument after the interpreter name.

The "proper" solution is of course using more than one script, usually a bash script that will execute your CasperJS script with the proper options.

But...

There is a very old  trick  horrible hack that addresses this problem, the polyglot script. It involves misusing language features to write a file that is a valid script in two (or more) interpreters, doing two different things.

In this case the script will first be read by Bash, because of the shebang line. The script will direct Bash to execute CasperJS with specific options on the script itself and then terminate. CasperJS will skip over the line aimed for Bash and run the rest of the script.

JavaScript version

#!/bin/sh
//bin/true; exec casperjs --proxy=127.0.0.1:8003 test "$0" "$@"

(rest of JavaScript file)

The trick here is that // starts a comment in Javascript, while in Bash it's just part of the first line of code.

CoffeeScript version

#!/bin/sh
""""exec casperjs --proxy=127.0.0.1:8003 test "$0" "$@" #"""

(rest of CoffeeScript file)

The trick here is that """" is skipped over by Bash, because it's just two empty strings, while in CoffeeScript it opens a multiline string that swallows the first line of code.

share|improve this answer
    
excellent snippet – Christian M. Raymonds Jun 26 '15 at 8:23

this works

casper = require('casper').create({
        pageSettings: {
            proxy: 'http://localhost:3128'
         }
});
share|improve this answer
    
CasperJS official documentation doesn't show proxy. Sure it works? – 3zzy Feb 4 at 10:46
up vote 3 down vote accepted

I needed CasperJS to run inside a node environment. So I have set up Spooky and the good news is that you can set one inside your code like this:

var spooky = new Spooky({
        child: {
            proxy: '192.128.101.42:9001',
            /* ... */
        },
        /* ... */
    },
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.