前回 phantomjsのインストールに成功したので、色々触って見ます。
・ちなみにphantomjsってCoffeeScriptいけるんですね!
$ phantomjs Usage: phantomjs [options] script.[js|coffee] [script argument [script argument ...]]
・次からはphantomjsのQuickStartを、CoffeeScriptに書き写しながら進めます。
■説明
・まずはhello world
$ vim hello.coffee console.log "hello, world!" phantom.exit() $ phantomjs hello.coffee hello, world!
・次に同期sleep
$ vim delay.coffee for t in [10..1] console.log t phantom.sleep 1000 console.log 'BLAST OFF' phantom.exit() $ phantomjs delay.coffee 10 9 ... 1 BLAST OFF
・ちなみに非同期sleepは
$ vim adelay.coffee
fibs = [0, 1]
ticker = window.setInterval ->
console.log fibs[fibs.length - 1]
fibs.push fibs[fibs.length - 1] + fibs[fibs.length - 2]
if fibs.length > 10
window.clearInterval ticker
phantom.exit()
, 300
$ phantomjs adelay.coffee
1
1
2
...
21
34
functionオブジェクトが最後の引数ではない場合ここにカンマ何ですね。。・コマンドライン引数は
$ vim arguments.coffee
if phantom.args.length is 0
console.log 'Try to pass some args when invoking this script!'
else
phantom.args.forEach (arg, i) -> console.log "#{i}: #{arg}"
phantom.exit()
$ phantomjs arguments.coffee
Try to pass some args when invoking this script!
$ phantomjs arguments.coffee a b c
0: a
1: b
2: c
次回は、感じのページオープンとdom tree周り経由でCIにつなげたいと思う今日この頃
■参考
phantomjsのQuickStart
■雑感
CoffeeScriptの構文は、あまり違和感が無いな~
CoffeeScriptを習得するには、js -> coffeeの翻訳が一番?
■更新履歴
2011/04/20 http://tech.kayac.com/ を参考にさせて頂き、setIntervalの記載方法を変更しました。
0 件のコメント:
コメントを投稿