PhantomJS有控制台吗?

Bil*_*oon 14 javascript console phantomjs

我在github上找到了这个:https://github.com/gr2m/phantomjs-console

但它有点疯狂,不得不在一行文件中编写命令,然后读取并删除,输出在终端中.

我想要像...这样的控制台

$ phantomjs --console
phantom> console.log(typeof $)
[Object Function]
phantom> console.log("happy days")
happy days
phantom> 
Run Code Online (Sandbox Code Playgroud)

有这样的事吗?

编辑:开始明白为什么他们以这种疯狂的方式做到了......

来自PhantomJS-Node:https://github.com/sgentle/phantomjs-node

不,真的,它是如何工作的?

我将用一个问题回答这个问题.如何与不支持共享内存,套接字,FIFO或标准输入的进程通信?

好吧,PhantomJS确实支持一件事,那就是打开网页.事实上,它非常适合打开网页.因此,我们通过旋转一个ExpressJS实例与PhantomJS进行通信,在子进程中打开Phantom,并将其指向一个特殊的网页,将socket.io消息转换为alert()调用.这些alert()电话是由Phantom接听的,你去了!

通信本身通过詹姆斯Halliday的美妙发生dnode 库,与结合所幸的作品不够好 browserify直跑出来PhantomJS的洋泾浜JavaScript环境中.

如果你想破解幽灵,请做!您可以使用cake test或npm test运行测试,并使用cake build重建coffeescript/browserified代码.您可能需要npm install -g coffeescript 蛋糕才能工作.

Ari*_*yat 14

从一年前的版本1.5开始,有一种交互模式(REPL).您只需要在没有任何参数的情况下启动PhantomJS,它将立即以REPL模式启动.

  • 而且似乎REPL中的一个错误会阻止页面打开...所以现在回到我的hackish解决方案...... (2认同)

Bil*_*oon 5

好吧,我最后为我最初链接到的控制台脚本编写了一个包装脚本:https://github.com/gr2m/phantomjs-console

这是一种混乱的方式,但实际上完全按照我的意愿工作.事实证明,phantomjs计划处理stdin/stdout,但它还没有实现.当它实现时,这种疯狂的交互方法将变得过时,一个新的,简单的脚本将能够充当控制台.

#!/usr/bin/env coffee

sys = require "sys"
fs = require "fs"

# stdin = process.openStdin()
# stdin.addListener "data", (d)-> console.log "you entered: [" + d.toString().substring(0, d.length-1) + "]"

readline = require "readline"

spawn = require("child_process").spawn
phantom = spawn("phantomjs", ["phantom_console.coffee", "http://local/"])

rl = readline.createInterface process.stdin, process.stdout
rl.setPrompt 'phantom> '
rl.prompt()

rl.on 'line', (line)->
  if line == "exit"
    phantom.kill()
    rl.close()
  else
    fs.writeFile ".command.js", line
  # rl.prompt()

rl.on 'close', ->
  phantom.kill()
  process.exit(0)

phantom.stdout.on "data", (data) ->
  console.log data+''
  rl.prompt()

phantom.stderr.on "data", (data) ->
  console.log "\nstderr: " + data
  rl.prompt()

phantom.on "exit", (code) ->
  console.log "child process exited with code " + code
Run Code Online (Sandbox Code Playgroud)