Skip to content

Commit 5a53c2d

Browse files
authored
Rendering: Have phantomjs wait a bit before rendering to give fonts a change to load (grafana#22623)
1 parent 917860c commit 5a53c2d

File tree

1 file changed

+68
-64
lines changed

1 file changed

+68
-64
lines changed

tools/phantomjs/render.js

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,86 @@
11
(function() {
2-
'use strict';
2+
'use strict';
33

4-
var page = require('webpage').create();
5-
var args = require('system').args;
6-
var params = {};
7-
var regexp = /^([^=]+)=([^$]+)/;
4+
var page = require('webpage').create();
5+
var args = require('system').args;
6+
var params = {};
7+
var regexp = /^([^=]+)=([^$]+)/;
88

9-
args.forEach(function(arg) {
10-
var parts = arg.match(regexp);
11-
if (!parts) { return; }
12-
params[parts[1]] = parts[2];
13-
});
9+
args.forEach(function(arg) {
10+
var parts = arg.match(regexp);
11+
if (!parts) {
12+
return;
13+
}
14+
params[parts[1]] = parts[2];
15+
});
1416

15-
var usage = "url=<url> png=<filename> width=<width> height=<height> renderKey=<key>";
17+
var usage = 'url=<url> png=<filename> width=<width> height=<height> renderKey=<key>';
1618

17-
if (!params.url || !params.png || !params.renderKey || !params.domain) {
18-
console.log(usage);
19-
phantom.exit();
20-
}
19+
if (!params.url || !params.png || !params.renderKey || !params.domain) {
20+
console.log(usage);
21+
phantom.exit();
22+
}
2123

22-
phantom.addCookie({
23-
'name': 'renderKey',
24-
'value': params.renderKey,
25-
'domain': params.domain,
26-
});
24+
phantom.addCookie({
25+
name: 'renderKey',
26+
value: params.renderKey,
27+
domain: params.domain,
28+
});
2729

28-
page.viewportSize = {
29-
width: params.width || '800',
30-
height: params.height || '400'
31-
};
30+
page.viewportSize = {
31+
width: params.width || '800',
32+
height: params.height || '400',
33+
};
34+
35+
var timeoutMs = (parseInt(params.timeout) || 10) * 1000;
36+
var waitBetweenReadyCheckMs = 50;
37+
var totalWaitMs = 0;
3238

33-
var timeoutMs = (parseInt(params.timeout) || 10) * 1000;
34-
var waitBetweenReadyCheckMs = 50;
35-
var totalWaitMs = 0;
39+
page.open(params.url, function(status) {
40+
console.log('Loading a web page: ' + params.url + ' status: ' + status, timeoutMs);
3641

37-
page.open(params.url, function (status) {
38-
console.log('Loading a web page: ' + params.url + ' status: ' + status, timeoutMs);
42+
page.onError = function(msg, trace) {
43+
var msgStack = ['ERROR: ' + msg];
44+
if (trace && trace.length) {
45+
msgStack.push('TRACE:');
46+
trace.forEach(function(t) {
47+
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
48+
});
49+
}
50+
console.error(msgStack.join('\n'));
51+
};
3952

40-
page.onError = function(msg, trace) {
41-
var msgStack = ['ERROR: ' + msg];
42-
if (trace && trace.length) {
43-
msgStack.push('TRACE:');
44-
trace.forEach(function(t) {
45-
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
46-
});
47-
}
48-
console.error(msgStack.join('\n'));
49-
};
53+
function checkIsReady() {
54+
var panelsRendered = page.evaluate(function() {
55+
var panelCount = document.querySelectorAll('plugin-component').length;
56+
return window.panelsRendered >= panelCount;
57+
});
5058

51-
function checkIsReady() {
52-
var panelsRendered = page.evaluate(function() {
53-
var panelCount = document.querySelectorAll('plugin-component').length;
54-
return window.panelsRendered >= panelCount;
59+
if (panelsRendered || totalWaitMs > timeoutMs) {
60+
var bb = page.evaluate(function() {
61+
var container = document.getElementsByClassName('dashboard-container');
62+
if (container.length == 0) {
63+
container = document.getElementsByClassName('panel-container');
64+
}
65+
return container[0].getBoundingClientRect();
5566
});
5667

57-
if (panelsRendered || totalWaitMs > timeoutMs) {
58-
var bb = page.evaluate(function () {
59-
var container = document.getElementsByClassName("dashboard-container")
60-
if (container.length == 0) {
61-
container = document.getElementsByClassName("panel-container")
62-
}
63-
return container[0].getBoundingClientRect();
64-
});
65-
66-
// reset viewport to render full page
67-
page.viewportSize = {
68-
width: bb.width,
69-
height: bb.height
70-
};
68+
// reset viewport to render full page
69+
page.viewportSize = {
70+
width: bb.width,
71+
height: bb.height,
72+
};
7173

74+
setTimeout(function() {
7275
page.render(params.png);
7376
phantom.exit();
74-
} else {
75-
totalWaitMs += waitBetweenReadyCheckMs;
76-
setTimeout(checkIsReady, waitBetweenReadyCheckMs);
77-
}
77+
}, 5);
78+
} else {
79+
totalWaitMs += waitBetweenReadyCheckMs;
80+
setTimeout(checkIsReady, waitBetweenReadyCheckMs);
7881
}
82+
}
7983

80-
setTimeout(checkIsReady, waitBetweenReadyCheckMs);
81-
});
82-
})();
84+
setTimeout(checkIsReady, waitBetweenReadyCheckMs);
85+
});
86+
})();

0 commit comments

Comments
 (0)