英文:
Pass complete code to phantomjs
问题
我正在使用Go编写一个服务,并使用phantomJS从给定的URL生成图像。从我的Go程序中,我使用Exec
来启动phantomJS二进制文件。这个方法很好用,但现在我正在寻找一种方法来传递完整的代码,以及运行二进制文件的调用。我的代码如下:
var args = require('system').args;
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = {
width: 1920,
height: 1080
};
page.open("http://www.url2fetch.com", function (status) {
var base64 = page.renderBase64('PNG');
console.log(base64);
phantom.exit();
});
除了要抓取的URL之外,这段代码永远不会改变。所以我的具体问题是:是否可能启动phantomJS并将上述代码作为参数传递,以便可以通过一次调用执行它。原因是:我不想将phantomjs脚本与我的Go程序打包在一起。
英文:
I'm writing a service in Go and I'm using phantomJS to generate an image from a given url. From my Go program, I'm using Exec
to start the phantomJS binary. This works fine, but now I'm looking for a way to pass the complete code, together with the call which runs the binary. My code is as follows:
var args = require('system').args;
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = {
width: 1920,
height: 1080
};
page.open("http://www.url2fetch.com", function (status) {
var base64 = page.renderBase64('PNG');
console.log(base64);
phantom.exit();
});
This never changes. Except the url to fetch :). So my concrete question is: is it possible to start phantomJS and pass the code above as a parameter so it can be executed with one call. Reason for this: I don't want to package the phantomjs script together with my Go program.
答案1
得分: 5
PhantomJS有一个交互式版本,你可以通过stdin传递代码,但是由于一个bug(版本1.9.x和2.0.0),无法打开任何页面,因此这个版本实际上无法使用。
没有办法将脚本传递给PhantomJS进行解释。你至少需要一个文件。你可以选择:
-
创建一个模板文件,将其与可执行文件一起发布,并使用该文件和额外的(URL)参数调用PhantomJS,或者
-
从Go中生成文件到某个临时目录,并让PhantomJS使用它。
如果你有理由相信会利用写入时间与使用时间之间的漏洞,那么你应该选择第一种方法。
为了使第一种情况完整,你可以在PhantomJS中使用system.args
来读取从Go传递的命令行选项,比如URL。
英文:
PhantomJS has an interactive version where you can pass code in through stdin, but there is a bug (versions 1.9.x and 2.0.0) because of which no pages can be opened which makes this effectively unusable.
There is no way to pass a script into PhantomJS to be interpreted. You need to have at least one file. You can either
-
create a template file which you ship with your executable and call PhantomJS with that file and an additional (URL) parameter or
-
you generate the file from go into some temporary directory and let PhantomJS use it.
If you have reason to believe that a time-of-write vs. time-of-use vulnerability will be exploited, then you should go with variant 1.
To make the first case complete, you can use system.args
in PhantomJS to read the passed commandline options from go such as the URL.
答案2
得分: 1
如果你正在使用Linux,你可以将这段代码放在PhantomJS脚本的开头。
#!/usr/bin/env phantomjs
这意味着当调用这个文件时,就像通过终端调用它一样。
$ phantomjs script.js args
英文:
If you are using Linux, you can put this code in the beginning of the PhantomJS script.
#!/usr/bin/env phantomjs
This means, when this file is called, it's like it was called by the terminal.
$ phantomjs script.js args
答案3
得分: -2
phantomjs的交互版本目前已经完全失效。
因此,如果您不想在项目中添加额外的文件,最干净的方法(如果您只是分发一个脚本而不是一个目录捆绑包)是在临时目录中生成一个随机命名的js文件,运行它,然后在运行结束后删除它。
英文:
The interactive version of phantomjs is completely screwed as of writing.
Due to this, if you don't want an extra file in your project, the cleanest way (if you're just distributing a script and not a directory bundle) is to generate a randomly-named js file in the temp directory, run it, and delete it after.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论