-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathjavascript.js
315 lines (272 loc) · 9.12 KB
/
javascript.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//[javascript]
// Uncomment the previous line for testing on webpagetest.org
// Please, refer to instructions for adding a custom metric in almanac.js.
function fetchWithTimeout(url) {
var controller = new AbortController();
setTimeout(() => {
controller.abort();
}, 5000);
return fetch(url, { signal: controller.signal });
}
let getSourceMaps = (async () => {
const $$ = (s) => [...document.querySelectorAll(s)];
const sourcemapRegex = /\/\/[#@] sourceMappingURL=(.+?)$/;
// filter Script requests from $WPT_BODIES
let scripts = $WPT_BODIES
.filter((request) => request.type == "Script")
.map((file) => {
return { url: file.url, body: file.response_body };
});
// search for inline scripts
scripts.push(
...$$("script")
.filter((n) => !n.src)
.map((s) => {
return {
url: location,
body: s.textContent,
};
})
);
let sourcemapURLs = scripts
.map((n) => {
if (n) {
let url = n.body?.match(sourcemapRegex)?.[1];
if (url) {
// Source map URL is relative to stylesheet URL
return new URL(url, n.url);
}
}
})
.filter((url) => !!url);
const results = {
count: sourcemapURLs.length,
ext: {},
};
if (sourcemapURLs.length === 0) {
return results;
}
// fetch JavaScript source files
let js = await Promise.all(
sourcemapURLs.map(async (n) => {
try {
var response = await fetchWithTimeout(n);
var json = await response.json();
} catch (e) {
return;
}
let sources = json.sources;
let base = json.sourceRoot ? new URL(json.sourceRoot, n) : n;
let js = 0;
let ts = 0;
let babel = 0;
sources = sources.map((s) => {
let url = new URL(s, base);
let ext = url.pathname.match(/\.(\w+)$/)?.[1];
if (ext) {
results.ext[ext] = (results.ext[ext] || 0) + 1;
if (ext === "js" || ext === "jsx") {
js++;
}
if (ext === "ts" || ext === "tsx") {
ts++;
}
}
if (url.pathname.match(/@babel/)) {
babel++;
}
return url;
});
if (babel > 0) {
results.babel = true;
}
if (js + ts > 0) {
if (json.sourcesContent) {
// Source is already here, no more requests needed, yay!
return json.sourcesContent;
}
let code = await Promise.all(
sources.map(async (s) => {
try {
let response = await fetchWithTimeout(s);
let text = response.ok ? await response.text() : "";
return text;
} catch (e) {
return "";
}
})
);
return code.join("\n");
}
})
);
js = js.filter((n) => !!n).join("\n");
results.js = {
size: js.length,
stats: {},
};
return results;
})();
return Promise.all([getSourceMaps]).then(([sourceMaps]) =>
JSON.stringify({
ajax_requests: (() => {
// Returns the number of ajax requests per page.
var ajax_apis = ["xmlhttprequest", "fetch", "beacon"];
return window.performance
.getEntriesByType("resource")
.filter((r) => ajax_apis.includes(r.initiatorType))
.reduce((obj, r) => {
obj.total++;
obj[r.initiatorType]++;
return obj;
}, Object.fromEntries([...ajax_apis, "total"].map((api) => [api, 0])));
})(),
// Returns the number of ajax requests using beacon.
beacon_ajax_usage: window.performance
.getEntriesByType("resource")
.filter((r) => r.initiatorType === "beacon").length,
// Returns the number of iframes per page.
iframe: document.getElementsByTagName("iframe").length,
requests_protocol: (() => {
// Returns the percentage of http 2 protocol used.
try {
var ajaxs = window.performance
.getEntriesByType("resource")
.filter((r) =>
["xmlhttprequest", "fetch", "beacon"].includes(r.initiatorType)
);
var resources = window.performance
.getEntriesByType("resource")
.filter(
(r) =>
r.initiatorType !== "xmlhttprequest" &&
r.initiatorType !== "fetch" &&
r.initiatorType !== "beacon"
);
let request_data = {
ajax_h1: ajaxs.filter((r) =>
["http/1.1", "http/1"].includes(r.nextHopProtocol)
).length,
resources_h1: resources.filter((r) =>
["http/1.1", "http/1"].includes(r.nextHopProtocol)
).length,
ajax_h2: ajaxs.filter((r) =>
["h2", "http/2", "http2"].includes(r.nextHopProtocol)
).length,
resources_h2: resources.filter((r) =>
["h2", "http/2", "http2"].includes(r.nextHopProtocol)
).length,
ajax_h3: ajaxs.filter((r) =>
["h3", "http/3", "h3-29"].includes(r.nextHopProtocol)
).length,
resources_h3: resources.filter((r) =>
["h3", "http/3", "h3-29"].includes(r.nextHopProtocol)
).length,
};
return request_data;
} catch (e) {
return null;
}
})(),
web_component_specs: (() => {
var getNodeName = (el) => el.nodeName.toLowerCase();
var elements_with_hyphen = Array.from(
document.getElementsByTagName("*")
).filter((e) => e.nodeName.includes("-"));
var unique_elements_with_hyphen = [
...new Set(elements_with_hyphen.map((e) => e.nodeName)),
].map((element) => {
return elements_with_hyphen.find((e) => e.nodeName === element);
});
var web_components = unique_elements_with_hyphen.filter((e) =>
customElements.get(e.nodeName.toLowerCase())
);
// Checking which web comp spec is used by found web_components (template, shadow_dom, custom_elements)
var shadow_roots = web_components.filter((e) => e.shadowRoot);
var template = web_components.filter((e) =>
Array.from(e.children).some(
(r) => r.nodeName.toLowerCase() === "template"
)
);
var web_component_specs = {
custom_elements: web_components.map(getNodeName),
shadow_roots: shadow_roots.map(getNodeName),
template: template.map(getNodeName),
};
return web_component_specs;
})(),
script_tags: (() => {
let script_tags = Array.from(document.querySelectorAll("script"));
let script_data = {
total: script_tags.length,
async: script_tags.filter((tag) => tag.async).length,
defer: script_tags.filter((tag) => tag.defer).length,
crossorigin: script_tags.filter((tag) => tag.crossorigin).length,
integrity: script_tags.filter((tag) => tag.integrity).length,
nomodule: script_tags.filter((tag) => tag.nomodule).length,
nonce: script_tags.filter((tag) => tag.nonce).length,
referrerpolicy: script_tags.filter((tag) => tag.referrerpolicy).length,
src: script_tags.filter((tag) => tag.src).length,
inline: script_tags.filter((tag) => !tag.src).length,
type_module: script_tags.filter((tag) => tag.type == "module").length,
async_and_defer: script_tags.filter((tag) => tag.async && tag.defer)
.length,
defer_without_src: script_tags.filter((tag) => tag.defer && !tag.src)
.length,
//deprecated attribute adoption
charset: script_tags.filter((tag) => tag.charset).length,
language: script_tags.filter((tag) => tag.hasAttribute("language"))
.length,
};
return script_data;
})(),
noscript_tags: (() => {
let noscript_data = {
total: document.querySelectorAll("noscript").length,
};
return noscript_data;
})(),
sourceMaps,
bundler: (() => {
const bundler = {
webpack: !(
typeof webpackJsonp === "undefined" &&
typeof webpackChunk === "undefined"
),
parcel: !(typeof parcelRequire === "undefined"),
};
return Object.entries(bundler)
.filter((n) => n[1])
.map((n) => n[0]);
})(),
document: (() => {
const documentHtml = $WPT_BODIES
.filter((request) => request.type == "Document")
.map((n) => {
return n.response_body;
});
if (documentHtml.length) {
const htmlElement = document.createElement("html");
htmlElement.innerHTML = documentHtml[0];
const elements = htmlElement.querySelectorAll("*").length;
const links = htmlElement.querySelectorAll("link").length;
const stylesheets = htmlElement.querySelectorAll(
"link[rel='stylesheet']"
).length;
const inlineStyles = htmlElement.querySelectorAll("style").length;
const scripts = htmlElement.querySelectorAll("script[src]").length;
const inlineScripts = htmlElement.querySelectorAll("script:not([src])").length;
return {
length: documentHtml[0].length,
elements,
links,
stylesheets,
inlineStyles,
scripts,
inlineScripts,
};
}
return null;
})(),
})
);