render.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function() {
  2. 'use strict';
  3. var page = require('webpage').create();
  4. var args = require('system').args;
  5. var params = {};
  6. var regexp = /^([^=]+)=([^$]+)/;
  7. args.forEach(function(arg) {
  8. var parts = arg.match(regexp);
  9. if (!parts) { return; }
  10. params[parts[1]] = parts[2];
  11. });
  12. var usage = "url=<url> png=<filename> width=<width> height=<height> renderKey=<key>";
  13. if (!params.url || !params.png || !params.renderKey || !params.domain) {
  14. console.log(usage);
  15. phantom.exit();
  16. }
  17. phantom.addCookie({
  18. 'name': 'renderKey',
  19. 'value': params.renderKey,
  20. 'domain': params.domain,
  21. });
  22. page.viewportSize = {
  23. width: params.width || '800',
  24. height: params.height || '400'
  25. };
  26. var timeoutMs = (parseInt(params.timeout) || 10) * 1000;
  27. var waitBetweenReadyCheckMs = 50;
  28. var totalWaitMs = 0;
  29. page.open(params.url, function (status) {
  30. console.log('Loading a web page: ' + params.url + ' status: ' + status, timeoutMs);
  31. page.onError = function(msg, trace) {
  32. var msgStack = ['ERROR: ' + msg];
  33. if (trace && trace.length) {
  34. msgStack.push('TRACE:');
  35. trace.forEach(function(t) {
  36. msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
  37. });
  38. }
  39. console.error(msgStack.join('\n'));
  40. };
  41. function checkIsReady() {
  42. var panelsRendered = page.evaluate(function() {
  43. var panelCount = document.querySelectorAll('plugin-component').length;
  44. return window.panelsRendered >= panelCount;
  45. });
  46. if (panelsRendered || totalWaitMs > timeoutMs) {
  47. var bb = page.evaluate(function () {
  48. var container = document.getElementsByClassName("dashboard-container")
  49. if (container.length == 0) {
  50. container = document.getElementsByClassName("panel-container")
  51. }
  52. return container[0].getBoundingClientRect();
  53. });
  54. // reset viewport to render full page
  55. page.viewportSize = {
  56. width: bb.width,
  57. height: bb.height
  58. };
  59. page.render(params.png);
  60. phantom.exit();
  61. } else {
  62. totalWaitMs += waitBetweenReadyCheckMs;
  63. setTimeout(checkIsReady, waitBetweenReadyCheckMs);
  64. }
  65. }
  66. setTimeout(checkIsReady, waitBetweenReadyCheckMs);
  67. });
  68. })();