-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (161 loc) · 5.84 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const config = require('./config.js');
const puppeteer = require("puppeteer");
const fs = require('node:fs');
const path = require('path');
const { Document, ImageRun, Packer, Paragraph, PageBreak, TextRun } = require('docx');
const sizeOf = require('image-size');
const sharp = require('sharp');
/* screenshot */
const RemoveExtension = (file) =>{
return file.substring(0, file.lastIndexOf("."))
}
const GetURLName = (file) =>{
return (new URL(file)).hostname.replace("www.", "");
}
const TakeScreenshots = async (page) =>{
/* creating screenshot dir if not exists, if exist deleting prev screenshots */
if (fs.existsSync(config.dist_screenshot_path)) {
fs.rmSync(config.dist_screenshot_path, {recursive: true})
}
fs.mkdirSync(config.dist_screenshot_path);
/* html file screenshots */
const htmlFiles = fs.readdirSync(config.src_path).filter( ( elm ) => elm.match(/.*\.(html?)/ig));
for (const file of htmlFiles) {
try {
await page.goto(`file:${path.join(__dirname, config.src_path, file)}`);
await page.screenshot({ path: config.dist_screenshot_path + RemoveExtension(file) +'.png', fullPage: true });
} catch (err) {
throw err
}
}
/* url screenshots from json file */
const jsonFiles = fs.readdirSync(config.src_path).filter( ( elm ) => elm.match(/.*\.(json?)/ig));
for (const file of jsonFiles) {
const fileContent = fs.readFileSync(path.join(__dirname, config.src_path, file), "utf8");
const jsonContent = JSON.parse(fileContent);
const lists = Object.values(jsonContent)
for (const list of lists) {
if(Array.isArray(list)){
for (const url of list) {
try {
await page.goto(url);
await page.screenshot({ path: config.dist_screenshot_path + GetURLName(url) +'.png', fullPage: true });
} catch (err) {
throw err
}
}
}
}
}
}
/* Doc */
const CreateDoc = async () =>{
if (fs.existsSync(config.dist_screenshot_path)) {
const filenames = fs.readdirSync(config.dist_screenshot_path).filter( ( elm ) => elm.match(/.*\.(png?)/ig));
const docObject = {
sections: [
{
properties: {
page: {
margin: {
top: config.doc_margin,
right: config.doc_margin,
bottom: config.doc_margin,
left: config.doc_margin,
},
},
},
children: [],
},
],
}
for (const file of filenames) {
const dimensions = await sizeOf(config.dist_screenshot_path+file)
const aspectRatio = dimensions.width / dimensions.height;
const width = config.doc_image_width;
const height = width / aspectRatio;
const max_height = config.doc_image_max_height * (dimensions.width / width);
docObject.sections[0].children.push(
new Paragraph({
children: [
new TextRun({
text: RemoveExtension(file),
font: config.doc_font,
size: config.doc_font_size
})]
}))
if((dimensions.height <= max_height)){
docObject.sections[0].children.push(
new Paragraph({
children: [
new ImageRun({
data: fs.readFileSync(config.dist_screenshot_path+file),
transformation: {
width: width,
height: height
},
}),
new PageBreak()
],
}))
}
else{
/* Cropping image if too big */
let remaining_height = dimensions.height;
let crop_top = 0;
let crop_height = max_height;
for(let i=0; remaining_height > 0; i++){
const originalImage = config.dist_screenshot_path+file;
const outputImage = config.dist_screenshot_path+RemoveExtension(file)+'_'+i+'.png';
const cropped_file = await sharp(originalImage).extract({ width: dimensions.width, height: parseInt(crop_height), left: 0, top: parseInt(crop_top) }).toFile(outputImage)
.then(function(new_file_info) {
docObject.sections[0].children.push(
new Paragraph({
children: [
new ImageRun({
data: fs.readFileSync(outputImage),
transformation: {
width: width,
height: crop_height * (width/dimensions.width)
},
}),
new PageBreak()
],
}))
})
.catch(function(err) { console.log("An error occured while cropping image");});
remaining_height -= max_height;
crop_height = remaining_height >= 0 && remaining_height < max_height ? remaining_height : max_height;
crop_top = crop_top + max_height;
}
}
}
const doc = new Document(docObject)
const date = new Date()
if (!fs.existsSync(config.dist_screenshot_doc_path)) {
fs.mkdirSync(config.dist_screenshot_doc_path);
}
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync(config.dist_screenshot_doc_path+'Screenshots_'+date.toISOString().slice(0, 10)+'.docx', buffer);
});
}
}
/* puppeteer */
const runApp = () =>{
puppeteer
.launch({
defaultViewport: {
width: config.screenshot_width,
height: config.screenshot_min_height
},
})
.then(async (browser) => {
const page = await browser.newPage();
await TakeScreenshots(page); //function to take screenshots
if(config.enable_doc_creation){
await CreateDoc(); //function to create doc file of all the screenshots
}
await browser.close();
});
}
runApp();