/
popup.js
254 lines (221 loc) · 7.69 KB
/
popup.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
(function() {
/* Key for total loading time of page */
var PAGE_LOAD_KEY = "totalTime";
/* Global mutable variables used throughout */
var precedingTime = pageLoadTotalTime = counter = 0;
/* The data that will be displayed to the end user */
/* At some point, share orderOfEvents and eval() on orderOfEvents.calculation */
var orderOfEvents = [{
'key': 'prerequestTime',
'label': 'Pre-request',
'calculation': 'performance.timing.requestStart - performance.timing.navigationStart'
}, {
'key': 'latencyTime',
'label': 'Latency',
'calculation': 'performance.timing.responseStart - performance.timing.requestStart'
}, {
'key': 'serverTime',
'label': 'Server',
'calculation': 'performance.timing.responseEnd - performance.timing.responseStart'
}, {
'key': 'domLoadingTime',
'label': 'DOM Loading',
'calculation': 'performance.timing.domInteractive - performance.timing.responseEnd'
}, {
'key': 'domCompleteTime',
'label': 'DOM Complete',
'calculation': 'performance.timing.domComplete - performance.timing.domInteractive'
}, {
'key': 'loadTime',
'label': 'Load',
'calculation': 'performance.timing.loadEventEnd - performance.timing.domComplete'
}];
chromeExtension = {
/**
* Generate everything.
*
* @private
*/
generate: function () {
chrome.tabs.executeScript(
{
file: 'performance.js'
},
function(result, isException) {
if (isException) {
//console.log("There was an error with counting elements on this page.");
} else {
pageLoadTotalTime = result[0][PAGE_LOAD_KEY];
var dataParent = document.getElementsByClassName('data')[0];
var node = chromeExtension.createRow(PAGE_LOAD_KEY, pageLoadTotalTime, 0, pageLoadTotalTime);
dataParent.appendChild(node);
chromeExtension.incrementCounter();
for (var i in orderOfEvents) {
var key = orderOfEvents[i].key;
var time = result[0][key];
var displayName = orderOfEvents[i].label;
var dataRow = chromeExtension.createRow(displayName, time);
dataParent.appendChild(dataRow);
precedingTime = precedingTime + time;
chromeExtension.incrementCounter();
}
chromeExtension.createRowMouseOverEvent();
}
}
);
},
/**
* Increments global counter
*
* @private
*/
incrementCounter: function() {
counter = counter + 1;
},
/**
* Create a new row with the data passed through
*
* @param {String} key for performance api metric
* @param {Number} value for performance api metric
* @private
*/
createRow: function (key, value) {
var bar, clearfix, barCopy;
var d = document.createElement('div');
d.className = 'performance-row performance-row-' + counter + ' key-' + counter;
var label = document.createElement('span');
label.className = 'performance-label';
label.textContent = key;
var time = document.createElement('span');
time.className = 'performance-time';
time.textContent = value + " ms";
bar = (key === PAGE_LOAD_KEY) ? this.createGraphTotal() : this.createGraph(key, value, counter);
if (document.getElementsByClassName('performance-graph-total')[0]) {
barCopy = this.createChartData(value, counter);
document.getElementsByClassName('performance-graph-total')[0].appendChild(barCopy);
}
d.appendChild(label);
d.appendChild(bar);
d.appendChild(time);
clearfix = this.createClearfixDOMElement();
d.appendChild(clearfix);
return d;
},
/**
* Create a new graph wrapper so that the totals can be passed through
*
* @private
*/
createGraphTotal: function() {
var g = document.createElement('div');
g.className = 'performance-graph-total';
return g;
},
/**
* Create a new graph with the data passed through
*
* @param {String} DOM element name
* @param {Number} current tally of milliseconds passed
* @private
*/
createGraph: function(key, value) {
var g = document.createElement('span');
g.className = 'performance-graph ' + key;
var preTime = this.createPreChartSpacing(counter);
var thisKeyTime = this.createChartData(value, counter);
var clearfix = this.createClearfixDOMElement();
g.appendChild(preTime);
g.appendChild(thisKeyTime);
g.appendChild(clearfix);
return g;
},
/**
* Create an the bar which is a percentage of its parent.
*
* @param {Number} value of the data point
*
* @private
*/
createChartData: function(value) {
var d = document.createElement('span');
d.className = 'performance-graph-key key-' + counter;
d.style.width = (value / pageLoadTotalTime * 100) + '%';
return d;
},
/**
* Create a pre DOM element and assign it to be a percentage
* equal to everything that has been allocated so far.
*
* @private
*/
createPreChartSpacing: function() {
var d = document.createElement('span');
d.className = 'performance-graph-pre key-' + counter;
d.style.width = (precedingTime / pageLoadTotalTime * 100) + '%';
return d;
},
/**
* Create a clearfix DOM element
*
* @private
*/
createClearfixDOMElement: function() {
var c = document.createElement('span');
c.className = 'clearfix';
return c;
},
/**
* Create an onmouseover event on each row
*
* @private
*/
createRowMouseOverEvent: function() {
var explanation = document.getElementsByClassName('explanation')[0];
var preText = "Calc: ";
for (var i = 1; i < (orderOfEvents.length + 1); i++) {
document.getElementsByClassName('performance-row-' + i)[0].onmouseout= function() {
explanation.innerHTML = ' ';
};
}
document.getElementsByClassName('performance-row-1')[0].onmouseover = function() {
explanation.innerHTML = preText + orderOfEvents[0].calculation;
};
document.getElementsByClassName('performance-row-2')[0].onmouseover = function() {
explanation.innerHTML = preText + orderOfEvents[1].calculation;
};
document.getElementsByClassName('performance-row-3')[0].onmouseover = function() {
explanation.innerHTML = preText + orderOfEvents[2].calculation;
};
document.getElementsByClassName('performance-row-4')[0].onmouseover = function() {
explanation.innerHTML = preText + orderOfEvents[3].calculation;
};
document.getElementsByClassName('performance-row-5')[0].onmouseover = function() {
explanation.innerHTML = preText + orderOfEvents[4].calculation;
};
document.getElementsByClassName('performance-row-6')[0].onmouseover = function() {
explanation.innerHTML = preText + orderOfEvents[5].calculation;
};
},
/**
* Execute
*
*/
init: function () {
chrome.tabs.executeScript(
{
file: 'onload-execute.js'
},
function(result, isException) {
if (result) {
chromeExtension.generate();
} else {
//console.log("There was an error retrieving Web Performing API Timing information");
}
});
}
}
window.addEventListener("load", function load(event){
window.removeEventListener("load", load, false);
chromeExtension.init();
},false);
})();