forked from wikimedia/mediawiki-services-mathoid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmathjax.js
54 lines (44 loc) · 1.42 KB
/
mathjax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var system = require('system'),
page = require('webpage').create(),
server = require('webserver').create(),
service;
var wait = function (checkFunction, readyFunction) {
if (checkFunction()) {
readyFunction();
} else {
setTimeout(function () {
wait(checkFunction, readyFunction);
}, 250)
}
};
page.onConsoleMessage = function (msg) {
console.log('PhantomJS Message:', msg);
};
page.onError = function (msg) {
console.log('PhantomJS Message [Error]:', msg);
};
page.onCallback = function (data) {
console.log('MathJax Loaded');
};
page.open('index.html');
service = server.listen(system.env.PORT || 6000, function (req, res) {
var randId = parseInt(Math.random() * 1000000, 10),
equation = req.post.math;
console.log('Server Request (' + randId + '):', equation);
page.evaluate(function (opts) {
window.engine.compileEquation(opts.equation, opts.id);
}, {equation: equation, id: randId});
wait(function () {
return page.evaluate(function (id) {
return document.getElementById('output-' + id) != null;
}, randId);
}, function () {
var out = page.evaluate(function (id) {
return document.getElementById('output-' + id).value;
}, randId);
res.statusCode = 200;
res.headers = {'Content-Type': 'application/json'};
res.write(out);
res.close()
});
});