How can I force PhantomJS to wait until MathJax is finished?

ぐ巨炮叔叔 提交于 2019-12-06 01:22:11

Try the following:

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();
      }
    );
  });
});

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

Try this:

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
  });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!