-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeIMG.js
More file actions
76 lines (61 loc) · 2.19 KB
/
makeIMG.js
File metadata and controls
76 lines (61 loc) · 2.19 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
module.paths.push('C:\\Users\\Ellen\\AppData\\Roaming\\npm\\node_modules');
const puppeteer = require('puppeteer');
var fs = require('fs');
var path = require('path');
const directory = 'C:\\Users\\Ellen\\Documents\\0 ugent\\Thesis\\github off\\scripts\\web'
function makeIMG(name) {
console.log('generating img for ' + name + '...');
(async() => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({
width: 960,
height: 1080,
deviceScaleFactor: 1//5
});
await page.goto('file://' + directory + '/' + name + '.html')
async function screenshotDOMElement(selector, padding = 0) {
const rect = await page.evaluate(selector => {
const element = document.querySelector(selector);
const {x, y, width, height} = element.getBoundingClientRect();
return {left: x, top: y, width, height, id: element.id};
}, selector);
return await page.screenshot({
path: 'img/'+name+'.png',
clip: {
x: rect.left*2 - padding,
y: rect.top*2 - padding,
width: rect.width + padding,
height: rect.height + padding
}
});
}
await screenshotDOMElement('body', 16);
await browser.close()
})()
}
if (process.argv.length === 2) {
console.log('generate all files in /web\n');
fs.readdir(directory, function(err, files) {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach(function(file, index) {
var filepath = path.join(directory, file);
fs.stat(filepath, function(error, stat) {
var extension = file.slice(-4);
if (stat.isFile() && extension === 'html') {
var name = file.slice(0, -5);
makeIMG(name);
}
})
});
});
} else {
for (var i = 2; i < process.argv.length; i++) {
console.log(process.argv[i]);
var name = process.argv[i];
makeIMG(name);
}
}