-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage.js
More file actions
55 lines (44 loc) · 1.81 KB
/
Copy pathcoverage.js
File metadata and controls
55 lines (44 loc) · 1.81 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
const puppeteer = require("puppeteer");
const fs = require('fs')
const urlToCover = 'https://www.collishop.be/e/nl/cs/clarysse-2-delige-handdoekenset-microfiber-blauw-702054'
const fileName = 'pdp';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
//Start sending raw DevTools Protocol commands are sent using `client.send()`
//First off enable the necessary "Domains" for the DevTools commands we care about
const client = await page.target().createCDPSession();
await client.send("Page.enable");
await client.send("DOM.enable");
await client.send("CSS.enable");
const inlineStylesheetIndex = new Set();
client.on("CSS.styleSheetAdded", stylesheet => {
const { header } = stylesheet;
if (header.isInline || header.sourceURL === "" || header.sourceURL.startsWith("blob:")) {
inlineStylesheetIndex.add(header.styleSheetId);
}
});
//Start tracking CSS coverage
await client.send("CSS.startRuleUsageTracking");
await page.goto(urlToCover);
// const content = await page.content();
// console.log(content);
const rules = await client.send("CSS.takeCoverageDelta");
const usedRules = rules.coverage.filter(rule => {
return rule.used;
});
const slices = [];
for (const usedRule of usedRules) {
// console.log(usedRule.styleSheetId)
if (inlineStylesheetIndex.has(usedRule.styleSheetId)) {
continue;
}
const stylesheet = await client.send("CSS.getStyleSheetText", {
styleSheetId: usedRule.styleSheetId
});
slices.push(stylesheet.text.slice(usedRule.startOffset, usedRule.endOffset));
}
fs.writeFile(`${fileName}.css`, slices.join(""))
await page.close();
await browser.close();
})();