blob: 98cfc67a31356b43c0d9885240ede1516a6f1efa [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Patrick Hulcea087f622018-05-18 00:37:53 +00005Audits2.StatusView = class {
6 /**
7 * @param {!Audits2.AuditController} controller
8 */
9 constructor(controller) {
10 this._controller = controller;
11
Blink Reformat4c46d092018-04-07 15:32:37 +000012 this._statusView = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000013 this._statusHeader = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000014 this._progressWrapper = null;
15 this._progressBar = null;
16 this._statusText = null;
17
Patrick Hulcea087f622018-05-18 00:37:53 +000018 this._inspectedURL = '';
Blink Reformat4c46d092018-04-07 15:32:37 +000019 this._textChangedAt = 0;
Patrick Hulcea087f622018-05-18 00:37:53 +000020 this._fastFactsQueued = Audits2.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000021 this._currentPhase = null;
22 this._scheduledTextChangeTimeout = null;
23 this._scheduledFastFactTimeout = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000024
25 this._dialog = new UI.Dialog();
26 this._dialog.setOutsideClickCallback(event => event.consume(true));
27 this._render();
Blink Reformat4c46d092018-04-07 15:32:37 +000028 }
29
Patrick Hulcea087f622018-05-18 00:37:53 +000030 _render() {
31 const dialogRoot = UI.createShadowRootWithCoreStyles(this._dialog.contentElement, 'audits2/audits2Dialog.css');
32 const auditsViewElement = dialogRoot.createChild('div', 'audits2-view vbox');
Blink Reformat4c46d092018-04-07 15:32:37 +000033
Patrick Hulcea087f622018-05-18 00:37:53 +000034 const cancelButton = UI.createTextButton(ls`Cancel`, this._cancel.bind(this));
35 const fragment = UI.Fragment.build`
36 <div class="audits2-view vbox">
37 <h2 $="status-header">Auditing your web page\u2026</h2>
38 <div class="audits2-status vbox" $="status-view">
39 <div class="audits2-progress-wrapper" $="progress-wrapper">
40 <div class="audits2-progress-bar" $="progress-bar"></div>
41 </div>
42 <div class="audits2-status-text" $="status-text"></div>
43 </div>
44 ${cancelButton}
45 </div>
46 `;
Blink Reformat4c46d092018-04-07 15:32:37 +000047
Patrick Hulcea087f622018-05-18 00:37:53 +000048 auditsViewElement.appendChild(fragment.element());
49 auditsViewElement.tabIndex = 0;
50
51 this._statusView = fragment.$('status-view');
52 this._statusHeader = fragment.$('status-header');
53 this._progressWrapper = fragment.$('progress-wrapper');
54 this._progressBar = fragment.$('progress-bar');
55 this._statusText = fragment.$('status-text');
56
57 this._dialog.setDefaultFocusedElement(cancelButton);
58 this._dialog.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
59 this._dialog.setMaxContentSize(new UI.Size(500, 400));
Blink Reformat4c46d092018-04-07 15:32:37 +000060 }
61
Patrick Hulcea087f622018-05-18 00:37:53 +000062 _reset() {
Blink Reformat4c46d092018-04-07 15:32:37 +000063 this._resetProgressBarClasses();
64 clearTimeout(this._scheduledFastFactTimeout);
65
66 this._textChangedAt = 0;
Patrick Hulcea087f622018-05-18 00:37:53 +000067 this._fastFactsQueued = Audits2.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000068 this._currentPhase = null;
69 this._scheduledTextChangeTimeout = null;
70 this._scheduledFastFactTimeout = null;
71 }
72
73 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000074 * @param {!Element} dialogRenderElement
Blink Reformat4c46d092018-04-07 15:32:37 +000075 */
Patrick Hulcea087f622018-05-18 00:37:53 +000076 show(dialogRenderElement) {
77 this._reset();
78 this.updateStatus(ls`Loading\u2026`);
Blink Reformat4c46d092018-04-07 15:32:37 +000079
Patrick Hulcea087f622018-05-18 00:37:53 +000080 const parsedURL = this._inspectedURL.asParsedURL();
81 const pageHost = parsedURL && parsedURL.host;
82 const statusHeader = pageHost ? ls`Auditing ${pageHost}` : ls`Auditing your web page`;
83 this._statusHeader.textContent = `${statusHeader}\u2026`;
84 this._dialog.show(dialogRenderElement);
85 }
86
87 hide() {
88 if (this._dialog.isShowing())
89 this._dialog.hide();
90 }
91
92 /**
93 * @param {string=} url
94 */
95 setInspectedURL(url = '') {
96 this._inspectedURL = url;
Blink Reformat4c46d092018-04-07 15:32:37 +000097 }
98
99 /**
100 * @param {?string} message
101 */
102 updateStatus(message) {
103 if (!message || !this._statusText)
104 return;
105
106 if (message.startsWith('Cancel')) {
107 this._commitTextChange(Common.UIString('Cancelling\u2026'));
108 clearTimeout(this._scheduledFastFactTimeout);
109 return;
110 }
111
112 const nextPhase = this._getPhaseForMessage(message);
113 if (!nextPhase && !this._currentPhase) {
114 this._commitTextChange(Common.UIString('Lighthouse is warming up\u2026'));
115 clearTimeout(this._scheduledFastFactTimeout);
116 } else if (nextPhase && (!this._currentPhase || this._currentPhase.order < nextPhase.order)) {
117 this._currentPhase = nextPhase;
118 this._scheduleTextChange(this._getMessageForPhase(nextPhase));
119 this._scheduleFastFactCheck();
120 this._resetProgressBarClasses();
121 this._progressBar.classList.add(nextPhase.progressBarClass);
122 }
123 }
124
Patrick Hulcea087f622018-05-18 00:37:53 +0000125 _cancel() {
126 this._controller.dispatchEventToListeners(Audits2.Events.RequestAuditCancel);
127 }
128
Blink Reformat4c46d092018-04-07 15:32:37 +0000129 /**
Patrick Hulcea087f622018-05-18 00:37:53 +0000130 * @param {!Audits2.StatusView.StatusPhases} phase
Blink Reformat4c46d092018-04-07 15:32:37 +0000131 * @return {string}
132 */
133 _getMessageForPhase(phase) {
134 if (phase.message)
135 return Common.UIString(phase.message);
136
Patrick Hulcea087f622018-05-18 00:37:53 +0000137 const deviceType = Audits2.RuntimeSettings.find(item => item.setting.name === 'audits2.device_type').setting.get();
138 const throttling = Audits2.RuntimeSettings.find(item => item.setting.name === 'audits2.throttling').setting.get();
139 const match = Audits2.StatusView.LoadingMessages.find(item => {
Blink Reformat4c46d092018-04-07 15:32:37 +0000140 return item.deviceType === deviceType && item.throttling === throttling;
141 });
142
143 return match ? ls`${match.message}` : ls`Lighthouse is loading your page`;
144 }
145
146 /**
147 * @param {string} message
Patrick Hulcea087f622018-05-18 00:37:53 +0000148 * @return {?Audits2.StatusView.StatusPhases}
Blink Reformat4c46d092018-04-07 15:32:37 +0000149 */
150 _getPhaseForMessage(message) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000151 return Audits2.StatusView.StatusPhases.find(phase => message.startsWith(phase.statusMessagePrefix));
Blink Reformat4c46d092018-04-07 15:32:37 +0000152 }
153
154 _resetProgressBarClasses() {
155 if (!this._progressBar)
156 return;
157
158 this._progressBar.className = 'audits2-progress-bar';
159 }
160
161 _scheduleFastFactCheck() {
162 if (!this._currentPhase || this._scheduledFastFactTimeout)
163 return;
164
165 this._scheduledFastFactTimeout = setTimeout(() => {
166 this._updateFastFactIfNecessary();
167 this._scheduledFastFactTimeout = null;
168
169 this._scheduleFastFactCheck();
170 }, 100);
171 }
172
173 _updateFastFactIfNecessary() {
174 const now = performance.now();
Patrick Hulcea087f622018-05-18 00:37:53 +0000175 if (now - this._textChangedAt < Audits2.StatusView.fastFactRotationInterval)
Blink Reformat4c46d092018-04-07 15:32:37 +0000176 return;
177 if (!this._fastFactsQueued.length)
178 return;
179
180 const fastFactIndex = Math.floor(Math.random() * this._fastFactsQueued.length);
181 this._scheduleTextChange(ls`\ud83d\udca1 ${this._fastFactsQueued[fastFactIndex]}`);
182 this._fastFactsQueued.splice(fastFactIndex, 1);
183 }
184
185 /**
186 * @param {string} text
187 */
188 _commitTextChange(text) {
189 if (!this._statusText)
190 return;
191 this._textChangedAt = performance.now();
192 this._statusText.textContent = text;
193 }
194
195 /**
196 * @param {string} text
197 */
198 _scheduleTextChange(text) {
199 if (this._scheduledTextChangeTimeout)
200 clearTimeout(this._scheduledTextChangeTimeout);
201
202 const msSinceLastChange = performance.now() - this._textChangedAt;
Patrick Hulcea087f622018-05-18 00:37:53 +0000203 const msToTextChange = Audits2.StatusView.minimumTextVisibilityDuration - msSinceLastChange;
Blink Reformat4c46d092018-04-07 15:32:37 +0000204
205 this._scheduledTextChangeTimeout = setTimeout(() => {
206 this._commitTextChange(text);
207 }, Math.max(msToTextChange, 0));
208 }
209
210 /**
211 * @param {!Error} err
Blink Reformat4c46d092018-04-07 15:32:37 +0000212 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000213 renderBugReport(err) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000214 console.error(err);
215 clearTimeout(this._scheduledFastFactTimeout);
216 clearTimeout(this._scheduledTextChangeTimeout);
217 this._resetProgressBarClasses();
218 this._progressBar.classList.add('errored');
219
220 this._commitTextChange('');
221 this._statusText.createTextChild(Common.UIString('Ah, sorry! We ran into an error: '));
222 this._statusText.createChild('em').createTextChild(err.message);
Patrick Hulcea087f622018-05-18 00:37:53 +0000223 if (Audits2.StatusView.KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 const message = Common.UIString(
225 'Try to navigate to the URL in a fresh Chrome profile without any other tabs or ' +
226 'extensions open and try again.');
227 this._statusText.createChild('p').createTextChild(message);
228 } else {
Patrick Hulcea087f622018-05-18 00:37:53 +0000229 this._renderBugReportLink(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000230 }
231 }
232
233 /**
234 * @param {!Error} err
235 * @param {string} auditURL
236 */
237 _renderBugReportLink(err, auditURL) {
238 const baseURI = 'https://github.com/GoogleChrome/lighthouse/issues/new?';
239 const title = encodeURI('title=DevTools Error: ' + err.message.substring(0, 60));
240
241 const issueBody = `
242**Initial URL**: ${auditURL}
243**Chrome Version**: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
244**Error Message**: ${err.message}
245**Stack Trace**:
246\`\`\`
247${err.stack}
248\`\`\`
249 `;
250 const body = '&body=' + encodeURIComponent(issueBody.trim());
251 const reportErrorEl = UI.XLink.create(
252 baseURI + title + body, Common.UIString('Report this bug'), 'audits2-link audits2-report-error');
253 this._statusText.appendChild(reportErrorEl);
254 }
255};
256
257
258/** @type {!Array.<!RegExp>} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000259Audits2.StatusView.KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000260 /PARSING_PROBLEM/,
261 /DOCUMENT_REQUEST/,
262 /READ_FAILED/,
263 /TRACING_ALREADY_STARTED/,
264 /^You must provide a url to the runner/,
265 /^You probably have multiple tabs open/,
266];
267
268/** @typedef {{message: string, progressBarClass: string, order: number}} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000269Audits2.StatusView.StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000270 {
271 id: 'loading',
272 progressBarClass: 'loading',
273 statusMessagePrefix: 'Loading page',
274 order: 10,
275 },
276 {
277 id: 'gathering',
278 progressBarClass: 'gathering',
279 message: 'Lighthouse is gathering information about the page to compute your score.',
280 statusMessagePrefix: 'Retrieving',
281 order: 20,
282 },
283 {
284 id: 'auditing',
285 progressBarClass: 'auditing',
286 message: 'Almost there! Lighthouse is now generating your own special pretty report!',
287 statusMessagePrefix: 'Evaluating',
288 order: 30,
289 }
290];
291
292/** @typedef {{message: string, deviceType: string, throttling: string}} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000293Audits2.StatusView.LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000294 {
295 deviceType: 'mobile',
296 throttling: 'on',
297 message: 'Lighthouse is loading your page with throttling to measure performance on a mobile device on 3G.',
298 },
299 {
300 deviceType: 'desktop',
301 throttling: 'on',
302 message: 'Lighthouse is loading your page with throttling to measure performance on a slow desktop on 3G.',
303 },
304 {
305 deviceType: 'mobile',
306 throttling: 'off',
307 message: 'Lighthouse is loading your page with mobile emulation.',
308 },
309 {
310 deviceType: 'desktop',
311 throttling: 'off',
312 message: 'Lighthouse is loading your page.',
313 },
314];
315
Patrick Hulcea087f622018-05-18 00:37:53 +0000316Audits2.StatusView.FastFacts = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000317 '1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].',
318 'Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]',
319 'BBC has seen a loss of 10% of their users for every extra second of page load [Source: WPO Stats]',
320 'By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]',
321 'Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]',
322 'If a site takes >1 second to become interactive, users lose attention, and their perception of completing the page task is broken [Source: Google Developers Blog]',
323 '75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]',
324 'The average user device costs less than 200 USD. [Source: International Data Corporation]',
325 '53% of all site visits are abandoned if page load takes more than 3 seconds [Source: Google DoubleClick blog]',
326 '19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]',
327 '14 seconds is the average time a mobile web page takes to load on a 4G connection [Source: Google DoubleClick blog]',
328 '70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]',
329 'As page load time increases from one second to seven seconds, the probability of a mobile site visitor bouncing increases 113%. [Source: Think with Google]',
330 'As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]',
331 '70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]',
332 'Lighthouse only simulates mobile performance; to measure performance on a real device, try WebPageTest.org [Source: Lighthouse team]',
333];
334
335/** @const */
Patrick Hulcea087f622018-05-18 00:37:53 +0000336Audits2.StatusView.fastFactRotationInterval = 6000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000337/** @const */
Patrick Hulcea087f622018-05-18 00:37:53 +0000338Audits2.StatusView.minimumTextVisibilityDuration = 3000;