javascript PhantomJS 有控制台吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15040054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:15:43  来源:igfitidea点击:

Is there a console for PhantomJS?

javascriptconsolephantomjs

提问by Billy Moon

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?

Well, there's one thing PhantomJS does support, and that's opening webpages. 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. Those alert()calls are picked up by Phantom and there you go!

The communication itself happens via James Halliday's fantastic dnodelibrary, which fortunately works well enough when combined with browserifyto run straight out of PhantomJS's pidgin Javascript environment.

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. You might need to npm install -g coffeescriptfor cake to work.

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

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

嗯,PhantomJS 支持一件事,那就是打开网页。事实上,它非常擅长打开网页。因此,我们通过启动 ExpressJS 的一个实例,在子进程中打开 Phantom,并将其指向一个将 socket.io 消息转换为alert()调用的特殊网页来与 PhantomJS 进行通信。alert()Phantom 会接听这些电话,然后就可以了!

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

如果你想破解幻影,请做!您可以使用 cake test 或 npm test 运行测试,并使用 cake build 重建咖啡脚本/浏览器代码。你可能需要npm install -g coffeescript蛋糕才能工作。

采纳答案by Ariya Hidayat

There is an interactive mode (REPL) since version 1.5almost a year ago. You just need to launch PhantomJS without any argument and it will immediately start in REPL mode.

从大约一年前的1.5 版开始,就有了交互模式 (REPL) 。你只需要在没有任何参数的情况下启动 PhantomJS,它就会立即以REPL 模式启动。

回答by Billy Moon

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. When it is implemented, this crazy method of interacting will become obsolete, and a new, simple script will be able to act as 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