-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
64 lines (56 loc) · 1.94 KB
/
test.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
const zlib = require('zlib');
const puppeteer = require('puppeteer');
const ecoindex = require('./ecoindex.js');
(async () => {
var url = 'http://www.ecoindex.fr';
var browser = await puppeteer.launch();
var page = await browser.newPage();
// get number of external requests
var req = 0;
await page.on('request', request => {
if (!request.url().startsWith('data:')) {
req++;
}
});
// get total uncompressed size
var size = 0;
await page.on('response', response => {
if (response.ok()) {
switch (response.headers()['content-encoding']) {
case 'br':
response.buffer().then(buffer => {
zlib.brotliCompress(buffer, function (_, result) {
size += result.length;
});
});
break;
case 'gzip':
response.buffer().then(buffer => {
zlib.gzip(buffer, function (_, result) {
size += result.length;
});
});
break;
case 'deflate':
response.buffer().then(buffer => {
zlib.deflate(buffer, function (_, result) {
size += result.length;
});
});
break;
default:
response.buffer().then(buffer => {
size += buffer.length;
});
break;
}
}
});
await page.goto(url);
// get number of DOM elements
var dom = await page.evaluate(() => document.querySelectorAll('*').length);
await browser.close();
// get ecoindex
var result = ecoindex.getEcoindex(dom, req, Math.round(size / 1024));
console.log(result);
})();