-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
126 lines (111 loc) · 4 KB
/
main.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
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: false });
var fs = require('fs');
var parse = require('./parser.js');
var report = require('./assembleFile.js');
var request = require('request');
var url = 'http://www.dn.se';
nightmare
.goto(url)
.evaluate(function () {
var linkStylesheets = document.querySelectorAll('link[rel=stylesheet]');
var externCss = Array.prototype.map.call(linkStylesheets, function(link) { return link.href; });
return externCss;
})
.end()
.then(function (links) {
return new Promise(function(resolve, reject) {
var cssData = [];
for(var i=0; i < links.length; i++){
request(links[i], function (error, response, body) {
if (!error && response.statusCode == 200){
cssData.push({
link: links[i],
body: body
});
}
if (cssData.length === links.length) {
resolve(cssData);
}
})
}
});
})
.then(function(cssInfos){
for(var i = 0; i < cssInfos.length; i++) {
var cssInfo = cssInfos[i];
var selectors = parse(cssInfo.body);
cssInfo.selectors = selectors;
}
return cssInfos;
})
.then(function(cssInfos){
var sub = Nightmare({ show: false })
return sub.goto(url)
.evaluate(function(data) {
var result = "";
var cssInfos = data.cssInfos;
for (var i = 0; i < cssInfos.length; i++) {
var cssInfo = cssInfos[i];
for (var j = 0; j < cssInfo.selectors.length; j++) {
var selector = cssInfo.selectors[j];
try {
var count = document.querySelectorAll(selector.selector).length;
selector.count = count;
} catch (e) {
}
}
}
return cssInfos;
}, { cssInfos: cssInfos})
.then(function(cssInfos) {
return cssInfos;
})
.catch(function (error) {
console.error(error);
});
})
.then(function(cssInfos){
var result = {
"website": "",
"url": url,
"stylesheets": cssInfos.map(function(css){
var count = css.selectors.length;
var unusedCnt = css.selectors.filter(function(sel){ return sel.count == 0; }).length;
var moderateCnt = css.selectors.filter(function(sel){ return sel.count > 0 && sel.count < 5; }).length;
var heavyCnt = css.selectors.filter(function(sel){ return sel.count >=5; }).length;
return {
"link":css.link,
"statistics": [
{
"title": "Unused",
"color": "#ef3c79",
"percentage": (unusedCnt / count) * 100
},
{
"title": "Moderately Used",
"color": "#ba65c9",
"percentage": (moderateCnt / count) * 100
},
{
"title": "Heavily Used",
"percentage": (heavyCnt / count) * 100,
"color": "#acec00"
}
],
"obsoletedselectors": css.selectors
};
}),
};
fs.writeFile('./data/data.json', JSON.stringify(result), function(err) {
if(err) {
return console.log(err);
}
console.log("Done");
});
return true;
})
.catch(function (error) {
console.error(error);
})
;