-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderer.js
More file actions
96 lines (79 loc) · 2.72 KB
/
renderer.js
File metadata and controls
96 lines (79 loc) · 2.72 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var fs = require('fs');
var casper = require('casper').create();
var system = require('system');
console.error = function () {
system.stderr.write(Array.prototype.join.call(arguments, ' ') + '\n');
};
var url = casper.cli.args[0];
var sizeX = casper.cli.args[1];
var sizeY = casper.cli.args[2];
var clientScripts = casper.cli.args[3];
render(casper, url, sizeX, sizeY, clientScripts);
/**
* addBind polyfills the bind function
*/
function addBind() {
// from this issue: https://github.com/ariya/phantomjs/issues/10522
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
}
function render(casper, url, sizeX, sizeY, clientScripts) {
var imageData;
casper.options.viewportSize = {
'width':sizeX,
'height':sizeY
};
if (clientScripts) {
clientScripts = clientScripts.split(',');
for (var i = 0; i < clientScripts.length; i++) {
casper.options.clientScripts.push(clientScripts[i]);
}
}
casper.on('page.initialized', function() {
this.evaluate(addBind);
});
casper.on('error', function(message) {
console.error('error: ' + message);
});
casper.on('remote.message', function(message) {
console.error('remote message caught: ' + message);
});
casper.start(url, function() {
// scripts to be executed once the page is loaded.
});
casper.then(function() {
imageData = this.evaluate(function() {
var canvasAll = document.querySelectorAll('canvas');
return Array.prototype.map.call(canvasAll, function(canvas) {
return canvas.toDataURL('image/png', 0.7);
});
});
});
casper.run(function () {
console.log(JSON.stringify(imageData)); // for it to be available in stdout.
casper.exit();
});
}