dongli4711 2015-05-28 19:36
浏览 85
已采纳

将完整的代码传递给phantomjs

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.

  • 写回答

3条回答 默认 最新

  • dsgfdgh14569 2015-05-28 20:13
    关注

    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

    1. create a template file which you ship with your executable and call PhantomJS with that file and an additional (URL) parameter or

    2. 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条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)