我如何强制PhantomJS等到MathJax完成? [英] How can I force PhantomJS to wait until MathJax is finished?

查看:252
本文介绍了我如何强制PhantomJS等到MathJax完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PhantomJS 预渲染 MathJax html文件。例如,假设在 math.html 我有:

I'm trying to pre-render a MathJax html file using PhantomJS. For example, suppose in math.html I have:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="MathJax/MathJax.js"></script>
    <script src="ConfigMathJax.js"></script>
  </head>
  <body>
    <span class="math">\(e = m c^2\)</span>
  </body>
</html>

我的(损坏的)渲染脚本目前看起来像:

My (broken) render script currently looks like:

var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
page.open(system.args[1], function () {
    page.evaluate(function(){
      var flag = false;
      MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      MathJax.Hub.Queue(function(){
        console.log(page.content);
        phantom.exit();
      });
    });
});

尝试将页面写入标准输出并退出< a href =https://github.com/mathjax/mathjax-docs/wiki/Event-when-typesetting-is-done%3F-Rescaling-after-rendering ... =nofollow noreferrer> 从队列中调用MathJax render命令。但似乎我在页面的上下文而不是顶级幻影上下文。找不到变量 page ReferenceError:找不到变量:page

I've attempted to write the page to standard out and exit after the MathJax render command is called from the queue. But it seems I'm in the "page"'s context rather than the top-level Phantom context. The variable page can't be found: ReferenceError: Can't find variable: page.

我用 setTimeout 代替使用标志:

var page = require('webpage').create();                                                  
var system = require('system');                                                          
var fs = require('fs');                                                                  
page.open(system.args[1], function () {                                                  
    page.evaluate(function(){                                                            
      MathJax.Hub.Queue(["Typeset",MathJax.Hub]);                                        
    });                                                                                  
    setTimeout(function(){                                                               
      console.log(page.content);                                                         
      phantom.exit();                                                                    
    },10000);                                                                            
}); 

然后我得到所需的输出,但当然等待时间 10000 ms取决于内容。

then I get the desired output, but of course the wait time 10000ms will depend on the content.

如何让PhantomJS知道MathJax已完成渲染?

How can I let PhantomJS know that MathJax is done rendering?

这是沙箱问题吗?

推荐答案

请尝试以下方法:

var page = require('webpage').create();
var system = require('system');                                                          
var fs = require('fs');
page.open(system.args[1], function () {
  page.evaluate(function () {
    MathJax.Hub.Queue(
      ["Typeset",MathJax.Hub],
      function () {
        console.log(page.content);
        phantom.exit();
      }
    );
  });
});

这将排队控制台输出和 phantom.exit()在排版发生后立即进行调用。我还没有测试过代码,但这是用MathJax进程同步的方法。

This will queue the console output and phantom.exit() calls to occur immediately after the typesetting occurs. I haven't tested the code, but this is the way to synchronize something with MathJax's process.

UPDATE

试试这个:

var page = require('webpage').create();
var system = require('system');                                                          
var fs = require('fs');
page.open(system.args[1], function () {
  page.onAlert = function (msg) {
    if (msg === "MathJax Done") {
      console.log(page.content);
    } else if (msg === "MathJax Timeout") {
      console.log("Timed out waiting for MathJax");
    } else {console.log(msg)}
    phantom.exit();
  };
  page.evaluate(function () {
    MathJax.Hub.Queue(
      ["Typeset",MathJax.Hub],
      [alert,'MathJax Done']
    );
    setTimeout(function () {alert("MathJax Timeout")},10000);  // timeout after 10 seconds
  });
});

这篇关于我如何强制PhantomJS等到MathJax完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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