/
har-importer.js
205 lines (177 loc) · 7.18 KB
/
har-importer.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Copyright (c) 2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
/*global YSLOW*/
(function () {
var types = {
'text/javascript': 'js',
'text/jscript': 'js',
'application/javascript': 'js',
'application/x-javascript': 'js',
'text/js': 'js',
'text/plain': 'doc',
'text/html': 'doc',
'text/css': 'css',
'image/png': 'image',
'image/jpeg': 'image',
'image/gif': 'image',
'image/x-icon': 'image',
'application/x-shockwave-flash': 'flash',
'text/x-json': 'json',
'text/x-js': 'json',
'application/json': 'json',
'application/x-js': 'json',
'application/xml': 'xml',
'application/xhtml+xml': 'xml',
'application/vnd.mozilla.xul+xml': 'xml',
'text/xml': 'xml',
'text/xul': 'xml',
'application/rdf+xml': 'xml'
},
// distinguish images from css images
setCssImages = function (cset) {
var i, len, comp,
urls = {},
comps = cset.getComponentsByType('css');
// loop all css and build url hash
for (i = 0, len = comps.length; i < len; i += 1) {
urls[comps[i].url] = 1;
}
// loop all images and set as cssimage those with referer
comps = cset.getComponentsByType('image');
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
if (urls[comp.req_headers.referer]) {
comp.type = 'cssimage';
}
}
},
// split html (string) into head and body elements
splitHtml = function (doc, base, html) {
var reHEAD = /<\s*head[^>]*>([\s\S]*)<\s*\/\s*head\s*>/i,
reBODY = /<\s*body[^>]*>([\s\S]*)<\s*\/\s*body\s*>/i,
match = reHEAD.exec(html),
ret = {
head: doc.createElement('div'),
body: doc.createElement('div')
};
base = '<base href="' + base + '" />';
if (match) {
ret.head.innerHTML = base + match[1];
html = html.slice(match.index + match[0].length);
}
match = reBODY.exec(html);
if (match) {
ret.body.innerHTML = base + match[1];
}
return ret;
},
/**
* Import har file
* @param doct host document to support dom level 0 methods
* @param har the har object
*/
run = function (doct, har, ruleset) {
var i, j, page, pageId, entry, comp, doc, len, lenJ, split, status,
content, baseHref, base, onloadTimestamp, cset, html, tDone,
comps, favicons, favicon, type, response, head, body, score,
yscontext = new YSLOW.context(doct),
log = har.log || {},
pages = log.pages || [],
entries = log.entries || [];
// pages loop (first page only for now)
// TODO: think of a way to analyze all pages
for (i = 0, len = (pages.length ? 1 : 0); i < len; i += 1) {
page = pages[i];
pageId = page.id;
tDone = page.pageTimings.onLoad || 0;
onloadTimestamp = new Date((new Date(page.startedDateTime))
.getTime() + tDone);
comps = [];
// page entries loop
for (j = 0, lenJ = entries.length; j < lenJ; j += 1) {
entry = entries[j];
response = entry.response;
status = parseInt(response.status, 10);
content = response.content;
type = (content.mimeType || '').toLowerCase();
idx = type.indexOf(';');
if (idx > -1) {
type = type.slice(0, idx);
}
type = types[type];
if (status === 301 || status === 302) {
type = 'redirect';
}
if (!type || status === 204 || entry.pageref !== pageId) {
continue;
}
// get page info
// TODO: find a better way to get multiple docs
if (!baseHref && type === 'doc') {
baseHref = entry.request.url;
html = content.text;
base = YSLOW.util.makeAbsoluteUrl('', baseHref);
doc = doct.createElement('div');
doc.innerHTML = '<base href="' + base + '" />' + html;
cset = new YSLOW.ComponentSet(doc, onloadTimestamp);
}
comps.push({
type: type,
href: entry.request.url,
entry: entry
});
}
for (j = 0, lenJ = comps.length; j < lenJ; j += 1) {
comp = comps[j];
cset.addComponent(comp.href, comp.type, baseHref, {
entry: comp.entry,
comp: comp
}
);
}
}
// set favicon
favicons = YSLOW.peeler.findFavicon(doc, baseHref);
comps = cset.components;
for (i = 0, len = favicons.length; i < len; i += 1) {
favicon = favicons[i];
for (j = 0, lenJ = comps.length; j < lenJ; j += 1) {
comp = comps[j];
if (comp.url === favicon.href) {
comp.type = 'favicon';
}
}
}
// refinement
split = splitHtml(doct, base, html);
head = split.head;
body = split.body;
setCssImages(cset);
cset.inline = YSLOW.util.getInlineTags(null, head, body);
cset.domElementsCount = YSLOW.util.countDOMElements(doc);
cset.cookies = cset.doc_comp.cookie;
cset.components = YSLOW.util.setInjected(doc,
cset.components, cset.doc_comp.body);
// run analysis
yscontext.component_set = cset;
YSLOW.controller.lint(doc, yscontext, ruleset || 'ydefault');
yscontext.result_set.url = baseHref;
score = yscontext.PAGE.overallScore;
yscontext.PAGE.t_done = tDone;
yscontext.collectStats();
return {
score: Math.round(score),
resultSet: yscontext.result_set,
componentSet: cset,
grade: YSLOW.util.prettyScore(score),
url: baseHref,
context: yscontext
};
};
// expose to YSLOW
YSLOW.harImporter = {
run: run
};
}());