-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
149 lines (129 loc) · 4.52 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
const os = require('os');
const exec = require('child_process').exec;
const puppeteer = require('puppeteer-extra');
const req = require('request-promise');
const fs = require('fs');
const bowserjr = require('@dp6/penguin-datalayer-core');
let resultsArray = [];
/* PDF Make */
const pdfMake = require('pdfmake');
const fonts = require('./pdf_configs/pdfMake_fonts.json');
const printer = new pdfMake(fonts);
let docDefinition = require('./pdf_configs/docDefinition.json');
/* PDF Make */
// Add stealth plugin and use defaults (all tricks to hide puppeteer usage).
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker');
puppeteer.use(AdblockerPlugin({ useCache: false }));
const config_file = process.argv.slice(2)[0]
? process.argv.slice(2)[0]
: new Error(
'File not specified as start param. Please inform the filenamePDF with its extension as --file in start script.'
);
// Defining export extension based on export option extracted from command line params.
let filenamePDF = `${new Date().getTime()}.pdf`;
const configFile = require(`./config/${config_file}`);
async function runAfterGTMDebug(config, page, browser, filenamePDF) {
await page.on('load', async () => {
if (page.url() === config.url) {
await page.evaluate(async () => {
//Validate first hits.
for (let elem of window.dataLayer) {
await bowser(elem);
}
window.dataLayer.push_c = window.dataLayer.push;
window.dataLayer.push = function (obj) {
window.dataLayer.push_c(obj);
bowser(obj);
};
});
if (config.browserClose) {
config.time ? await page.waitFor(config.time) : await page.waitFor(0);
await browser.close();
}
} else {
fs.appendFileSync(filenamePDF, `Path : ${page.url()}\n`, (err) => {
if (err) throw err;
});
}
});
await page.goto(config.url);
}
async function browserCrawler(config) {
const schema = require(`./schema/${config.schema_name[0]}`);
const browser = await puppeteer.launch({ headless: false });
let page = await browser.newPage();
// Configure the navigation timeout
await page.setDefaultNavigationTimeout(0);
if (config.gtmPreviewModeURL) await page.goto(config.gtmPreviewModeURL);
if (config.mobile_enabled === true) {
await page.emulate(iPhone);
} else {
await page.setViewport({ width: 1366, height: 768 });
}
await page.exposeFunction('bowser', (event) => {
bowserjr.validate(schema, event, function (result) {
resultsArray.push(result[0]);
});
});
await runAfterGTMDebug(config, page, browser, filenamePDF);
let pdfCreate = () => {
bowserjr.validate(schema, {}, function (result) {
resultsArray.push(result[0]);
});
resultsArray.forEach((resultObject) => {
if (resultObject) {
resultObject.dataLayerObject = resultObject.dataLayerObject.replace(/(\r\n|\n|\r)/gm, '');
resultObject.dataLayerObject = resultObject.dataLayerObject.replace(/\s/g, '');
resultObject.dataLayerObject = resultObject.dataLayerObject.split(',').join(' ');
docDefinition.content[7].table.body.push([
{
text: `${resultObject.status}`,
alignment: 'center',
},
{
text: `${resultObject.message}`,
alignment: 'center',
},
{
text: `${resultObject.dataLayerObject}`,
},
]);
}
});
/* Função Obrigatoria */
pdfMake.tableLayouts = {
exampleLayout: {
hLineWidth: function (i, node) {
if (i === 0 || i === node.table.body.length) {
return 0;
}
return i === node.table.headerRows ? 2 : 1;
},
vLineWidth: function (i) {
return 0;
},
hLineColor: function (i) {
return i === 1 ? 'black' : '#aaa';
},
paddingLeft: function (i) {
return i === 0 ? 0 : 8;
},
paddingRight: function (i, node) {
return i === node.table.widths.length - 1 ? 0 : 8;
},
},
};
/* ****************** */
const pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream(`results/${filenamePDF}`));
pdfDoc.end();
};
browser.on('disconnected', pdfCreate);
}
configFile.validator.forEach(async (config) => {
console.log('Iniciando validação...');
await browserCrawler(config);
console.log('Validação finalizada.');
});