-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 2.25 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
const puppeteer = require('puppeteer');
const extend = require('extend');
const allCSSProperties = require('known-css-properties').all;
module.exports = async function dumpComputedStyles (url, opts){
var opts = opts || {};
var defaultOptions = {
timeout: 60000
}
var options = extend({}, defaultOptions, opts);
//get rid of vendor-specific properties
var cssProperties = allCSSProperties.filter(prop => !prop.startsWith('-'))
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(process.argv[2], {waitUntil: 'load', timeout: options.timeout});
const result = await page.evaluate((cssProperties) => {
try {
function SetToJSON (key, value) {
if (typeof value === 'object' && value instanceof Set) {
return [...value];
}
return value;
}
var extractedProps = {};
cssProperties.forEach(prop => {extractedProps[prop] = new Set()})
document.querySelectorAll('body *').forEach(el =>{
var style = window.getComputedStyle(el,null);
cssProperties.forEach(prop => extractedProps[prop].add(style[prop]))
});
var jsonProps = {};
Object.entries(extractedProps).forEach(entry => {
var usedValues = Array.from(entry[1]);
// do not include unused properties in the result
if (JSON.stringify(usedValues) !== "[null]"){
jsonProps[entry[0]] = usedValues;
}
})
JSON.stringify({
properties: jsonProps,
url: location.href
}, SetToJSON);
return JSON.stringify({properties: jsonProps, url: location.href, title: document.title}, SetToJSON); // return our serialized JSON data
} catch(err) {
console.log("ERROR! " + err.toString());
}
},cssProperties);
await browser.close();
return result;
}