|
| 1 | +/* global phantom */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var assign = require('object-assign'); |
| 5 | +var log = console.log; |
| 6 | +var opts = JSON.parse(phantom.args[0]); |
| 7 | +var page = require('webpage').create(); |
| 8 | +var system = require('system'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Format trace |
| 12 | + * |
| 13 | + * @param {Object} trace |
| 14 | + * @api private |
| 15 | + */ |
| 16 | + |
| 17 | +function format(trace) { |
| 18 | + var src = trace.file || trace.sourceURL; |
| 19 | + var fn = (trace.function ? ' in function ' + trace.function : ''); |
| 20 | + return '→ ' + src + ' on line ' + trace.line + fn; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Make sure phantom never outputs to stdout |
| 25 | + */ |
| 26 | + |
| 27 | +console.log = console.error = function () { |
| 28 | + system.stderr.writeLine([].slice.call(arguments).join(' ')); |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * Add HTTP auth |
| 33 | + */ |
| 34 | + |
| 35 | +if (opts.username && opts.password) { |
| 36 | + opts.customHeaders = assign({}, opts.customHeaders, { |
| 37 | + 'Authorization': 'Basic ' + btoa(opts.username + ':' + opts.password) |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Add cookies |
| 43 | + */ |
| 44 | + |
| 45 | +opts.cookies.forEach(function (cookie) { |
| 46 | + if (!phantom.addCookie(cookie)) { |
| 47 | + console.error('Couldn\'t add cookie: ' + cookie).trim(); |
| 48 | + phantom.exit(1); |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +/** |
| 53 | + * Handle PhantomJS errors |
| 54 | + */ |
| 55 | + |
| 56 | +phantom.onError = function (err, trace) { |
| 57 | + console.error([ |
| 58 | + 'PHANTOM ERROR: ' + err, |
| 59 | + format(trace[0]) |
| 60 | + ].join('\n').trim()); |
| 61 | + |
| 62 | + phantom.exit(1); |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Handle page errors |
| 67 | + */ |
| 68 | + |
| 69 | +page.onError = function (err, trace) { |
| 70 | + console.error([ |
| 71 | + 'WARN: ' + err, |
| 72 | + format(trace[0]) |
| 73 | + ].join('\n').trim()); |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Set viewport size |
| 78 | + */ |
| 79 | + |
| 80 | +page.viewportSize = { |
| 81 | + width: opts.width, |
| 82 | + height: opts.height |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * Set custom headers |
| 87 | + */ |
| 88 | + |
| 89 | +page.customHeaders = opts.customHeaders || {}; |
| 90 | + |
| 91 | +/** |
| 92 | + * Open page |
| 93 | + */ |
| 94 | + |
| 95 | +page.open(opts.url, function (status) { |
| 96 | + if (status === 'fail') { |
| 97 | + console.error('Couldn\'t load url'); |
| 98 | + phantom.exit(1); |
| 99 | + } |
| 100 | + |
| 101 | + if (opts.crop) { |
| 102 | + page.clipRect = { |
| 103 | + top: 0, |
| 104 | + left: 0, |
| 105 | + width: opts.width, |
| 106 | + height: opts.height |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + if (opts.selector) { |
| 111 | + page.clipRect = page.evaluate(function (el) { |
| 112 | + return document |
| 113 | + .querySelector(el) |
| 114 | + .getBoundingClientRect(); |
| 115 | + }, opts.selector); |
| 116 | + } |
| 117 | + |
| 118 | + page.evaluate(function () { |
| 119 | + var bgColor = window |
| 120 | + .getComputedStyle(document.body) |
| 121 | + .getPropertyValue('background-color'); |
| 122 | + |
| 123 | + if (!bgColor || bgColor === 'rgba(0, 0, 0, 0)') { |
| 124 | + document.body.style.backgroundColor = 'white'; |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + window.setTimeout(function () { |
| 129 | + log.call(console, page.renderBase64('png')); |
| 130 | + phantom.exit(); |
| 131 | + }, opts.delay * 1000); |
| 132 | +}); |
0 commit comments