forked from alphapapa/solarized-everything-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.js
86 lines (65 loc) · 1.83 KB
/
screenshot.js
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
"use strict";
// * Variables
var page = require('webpage').create(),
system = require('system'),
url, output_filename, size, pageWidth, pageHeight;
pageWidth = 1000;
// A shorter height would be nice, but with the enormous page headers
// nowadays (e.g. the stupid one inserted by GitHub unless you are
// logged in), you can't see anything!
pageHeight = 1000;
// ** Args
url = system.args[1];
output_filename = system.args[2];
// * Functions
function insert_css (stylesheet) {
// Insert HTML STYLE element with contents of string STYLESHEET.
var element = document.createElement('style');
element.type = "text/css";
element.innerHTML = stylesheet;
document.getElementsByTagName("head")[0].appendChild(element);
}
function process_page (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
}
else {
// Add stylesheet
page.evaluate(insert_css, stylesheet);
// Take screenshot
window.setTimeout(
function () {
page.render(output_filename);
phantom.exit();
},
200
);
}
}
// * Main
// Fix console output from page.evaluate()
page.onConsoleMessage = function (msg) {
console.log("LOG: page.evaluate: " + msg);
};
// Log all external requests
// page.onResourceRequested = function (requestData, request) {
// console.log('LOG: LOADING RESOURCE: ', requestData['url']);
// };
// ** Check args
page.viewportSize = {
width: pageWidth,
height: pageHeight
};
page.clipRect = {
top: 0,
left: 0,
width: pageWidth,
height: pageHeight
};
// ** Read CSS file from disk
var css_file = system.args[3];
var fs = require("fs");
var stylesheet = fs.read(css_file);
// ** Load and render page
page.open(url, process_page);