简体   繁体   English

PhantomJS有控制台吗?

[英]Is there a console for PhantomJS?

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

But it is a bit crazy, having to write commands in a file, on one line only, which is then read, and deleted, and the output being in the terminal. 但它有点疯狂,不得不在一行文件中编写命令,然后读取并删除,输出在终端中。

I want a console like... 我想要像...这样的控制台

$ phantomjs --console
phantom> console.log(typeof $)
[Object Function]
phantom> console.log("happy days")
happy days
phantom> 

Is there anything like this? 有这样的事吗?

EDIT: Starting to understand why they did it in such a crazy way... 编辑:开始明白为什么他们以这种疯狂的方式做到了......

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

No really, how does it work? 不,真的,它是如何工作的?

I will answer that question with a question. 我将用一个问题回答这个问题。 How do you communicate with a process that doesn't support shared memory, sockets, FIFOs, or standard input? 如何与不支持共享内存,套接字,FIFO或标准输入的进程通信?

Well, there's one thing PhantomJS does support, and that's opening webpages. 好吧,PhantomJS确实支持一件事,那就是打开网页。 In fact, it's really good at opening web pages. 事实上,它非常适合打开网页。 So we communicate with PhantomJS by spinning up an instance of ExpressJS, opening Phantom in a subprocess, and pointing it at a special webpage that turns socket.io messages into alert() calls. 因此,我们通过启动ExpressJS实例与PhantomJS进行通信,在子进程中打开Phantom,并将其指向一个特殊的网页,将socket.io消息转换为alert()调用。 Those alert() calls are picked up by Phantom and there you go! 那些alert()调用是由Phantom接收的,你去了!

The communication itself happens via James Halliday's fantastic dnode library, which fortunately works well enough when combined with browserify to run straight out of PhantomJS's pidgin Javascript environment. 通信本身通过詹姆斯Halliday的美妙发生dnode库,与结合所幸的作品不够好browserify直跑出来PhantomJS的洋泾浜JavaScript环境中。

If you'd like to hack on phantom, please do! 如果你想破解幽灵,请做! You can run the tests with cake test or npm test, and rebuild the coffeescript/browserified code with cake build. 您可以使用cake test或npm test运行测试,并使用cake build重建coffeescript / browserified代码。 You might need to npm install -g coffeescript for cake to work. 您可能需要npm install -g coffeescript才能使用cake。

There is an interactive mode (REPL) since version 1.5 almost a year ago. 从一年前的版本1.5开始,有一种交互模式(REPL)。 You just need to launch PhantomJS without any argument and it will immediately start in REPL mode . 您只需要在没有任何参数的情况下启动PhantomJS,它将立即以REPL模式启动。

Well, I ended up writing a wrapper script for the console script I originally linked to: https://github.com/gr2m/phantomjs-console 好吧,我最后为我最初链接到的控制台脚本编写了一个包装脚本: https//github.com/gr2m/phantomjs-console

It is a messy way of doing it, but actually works exactly as I want. 这是一种混乱的方式,但实际上完全按照我的意愿工作。 Turns out, that phantomjs has plans to handle stdin/stdout but it is not yet implemented. 事实证明,phantomjs计划处理stdin / stdout,但它还没有实现。 When it is implemented, this crazy method of interacting will become obsolete, and a new, simple script will be able to act as console. 当它实现时,这种疯狂的交互方法将变得过时,一个新的,简单的脚本将能够充当控制台。

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM