Repository navigation
Expand file tree
/
Copy pathhar-parser.js
More file actions
executable file
·131 lines (117 loc) · 4.06 KB
/
Copy pathhar-parser.js
File metadata and controls
executable file
·131 lines (117 loc) · 4.06 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
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
const parseHar = (harContents) => {
// Load file (requires a web server, I'm using browsersync)
const harEntries = harContents.log.entries;
const harEntriesKeys = Object.keys(harEntries);
// Table to put contents into
const $resultsTable = document.getElementById('output');
// Add table headings
const $tableHeadWrapper = document.createElement('thead');
const $tableHead = document.createElement('tr');
$tableHeadWrapper.append($tableHead);
const tableHeadings = [
'File Name',
'3rd Party',
'URL',
'Status',
'Type',
'Priority',
'Initiator',
'File Size',
'File Size in Bytes',
'Total Time',
];
for (let i = 0; i < tableHeadings.length; i++) {
const tableHeading = tableHeadings[i];
const $currentTh = document.createElement('th');
$currentTh.textContent = tableHeading;
$tableHead.append($currentTh);
}
$resultsTable.append($tableHeadWrapper);
// Add results
const $tableBody = document.createElement('tbody');
for (let i = 0; i < harEntriesKeys.length; i++) {
const currentHarEntry = harEntries[harEntriesKeys[i]];
const $currentRow = document.createElement('tr');
/**
* Data to pull
*/
// Get filename
const explodedUrl = currentHarEntry.request.url.split('/');
let fileName = explodedUrl[explodedUrl.length - 1].split('?')[0];
// When there's nothing after the last slash we need to get the value before the last slash
if (fileName === '') {
fileName = explodedUrl[explodedUrl.length - 2];
}
// Figure out if file is 3rd Party
let isThirdPartyUrl = 'true';
if (currentHarEntry.request.url.match(/http[s]:\/\/[\w.]+redhat\.com/g)) {
isThirdPartyUrl = 'false';
}
// Figure out the initiator (if we can)
let initiator = '';
if (typeof currentHarEntry._initiator === 'object') {
if (currentHarEntry._initiator.type === 'other') {
initiator = currentHarEntry._initiator.type;
}
else if (currentHarEntry._initiator.type === 'script') {
if (typeof currentHarEntry._initiator.stack.callFrames[0] !== 'undefined'
&& currentHarEntry._initiator.stack.callFrames[0].url !== 'undefined'
) {
initiator = currentHarEntry._initiator.stack.callFrames[0].url;
}
else {
initiator = '??';
}
}
else if (typeof currentHarEntry._initiator.url === 'string') {
initiator = currentHarEntry._initiator.url;
}
else {
console.log(currentHarEntry.request.url, currentHarEntry._initiator);
}
}
// Get size and create human-friendly size
const sizeInBytes = parseInt(currentHarEntry.response.content.size);
let fileSize = '0B';
if (sizeInBytes < 1024) {
fileSize = `${ sizeInBytes }B`;
}
else if (sizeInBytes >= 1024 ) {
fileSize = `${ Math.round(sizeInBytes / 1024) }KB`;
}
// Get total time
// Google doesn't seem to be counting the SSL time? Not sure why, I'm gonna
const timingKeys = Object.keys(currentHarEntry.timings);
let totalTime = 0;
for (let i = 0; i < timingKeys.length; i++) {
totalTime += currentHarEntry.timings[timingKeys[i]];
}
const columns = [
fileName,
isThirdPartyUrl,
currentHarEntry.request.url,
currentHarEntry.response.status,
currentHarEntry._resourceType,
currentHarEntry._priority,
initiator,
fileSize,
sizeInBytes,
totalTime / 1000,
];
for (let j = 0; j < columns.length; j++) {
const currentColumnValue = columns[j];
const $currentTd = document.createElement('td');
$currentTd.textContent = currentColumnValue;
$currentRow.append($currentTd);
}
$tableBody.append($currentRow);
}
$resultsTable.append($tableBody);
};
let request = new XMLHttpRequest();
request.open('GET', 'access.stage.redhat.com.json');
request.responseType = 'json';
request.send();
request.onload = () => {
parseHar(request.response);
};