如何在page.evaluate中执行page.render? [英] How to execute page.render inside page.evaluate?

查看:156
本文介绍了如何在page.evaluate中执行page.render?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想渲染我的页面并在evaluate函数中退出PhantomJS,因为我想在触发特定事件时触发它。

I do want to render my page and exit PhantomJS in the evaluate function, because I want to trigger it when a specific event is fired.

我试过了类似这样的事情:

I've tried something like this:

page.evaluate(page, function (page, phantom) {

    //do some stuff on my page

    //i want to execute this in an eventhandler of my page though thats not the problem
    page.render('imgName.png'); 
    page.render('pdfName.pdf');
    phantom.exit();

}, page, phantom);

这在我的例子中不起作用,因为 page.render 似乎未定义。也许有一个参数的序列化器没有序列化obj的函数?

This doesn't work in my example, because page.render seems to be undefined. Maybe there is a serializer for the arguments which is not serializing functions of the obj?

这甚至可能吗?有谁知道我的问题的解决方案?

Is this even possible? Does anyone know a solution for my issue?

我知道我可以在我的评估函数中设置一个while循环并阻止它的终止,并在之后执行此页面渲染内容。我不喜欢这样。

I know I could set a while loop in my evaluate function and prevent its termination and do this page render stuff outside afterwards. I don't like this though.

推荐答案

page.evaluate()是沙盒。您传入的函数在页面上下文中进行评估,并且所有参数都需要序列化才能成功传入。您无法传递幻像进入页面上下文。 文档说明如下:

page.evaluate() is sandboxed. The function that you pass in is evaluated in the page context and all arguments need to be serializable in order the be successfully passed in. You cannot pass page or phantom into page context. The docs say the following:


注意: evaluate 函数的参数和返回值必须是一个简单的原始对象。经验法则:如果它可以通过JSON序列化,那就没问题了。

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

闭包,函数,DOM节点等将 not 工作!

Closures, functions, DOM nodes, etc. will not work!

这是 page.onCallback 适用于:

This is what page.onCallback is for:

page.onCallback = function(data){
    if (data.exit) {
        page.render('imgName.png'); 
        page.render('pdfName.pdf');
        phantom.exit();
    }
};
page.evaluate(page, function () {
    //do some stuff on my page

    //i want to execute this in an eventhandler of my page though thats not the problem
    window.callPhantom({ exit: true });
});

这篇关于如何在page.evaluate中执行page.render?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆