|
1 | 1 | (function() { |
2 | | - 'use strict'; |
| 2 | + 'use strict'; |
3 | 3 |
|
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 = /^([^=]+)=([^$]+)/; |
8 | 8 |
|
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 | + }); |
14 | 16 |
|
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>'; |
16 | 18 |
|
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 | + } |
21 | 23 |
|
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 | + }); |
27 | 29 |
|
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; |
32 | 38 |
|
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); |
36 | 41 |
|
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 | + }; |
39 | 52 |
|
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 | + }); |
50 | 58 |
|
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(); |
55 | 66 | }); |
56 | 67 |
|
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 | + }; |
71 | 73 |
|
| 74 | + setTimeout(function() { |
72 | 75 | page.render(params.png); |
73 | 76 | phantom.exit(); |
74 | | - } else { |
75 | | - totalWaitMs += waitBetweenReadyCheckMs; |
76 | | - setTimeout(checkIsReady, waitBetweenReadyCheckMs); |
77 | | - } |
| 77 | + }, 5); |
| 78 | + } else { |
| 79 | + totalWaitMs += waitBetweenReadyCheckMs; |
| 80 | + setTimeout(checkIsReady, waitBetweenReadyCheckMs); |
78 | 81 | } |
| 82 | + } |
79 | 83 |
|
80 | | - setTimeout(checkIsReady, waitBetweenReadyCheckMs); |
81 | | - }); |
82 | | - })(); |
| 84 | + setTimeout(checkIsReady, waitBetweenReadyCheckMs); |
| 85 | + }); |
| 86 | +})(); |
0 commit comments