blob: 6a6afe324358cae61b3aea2d18bd4412b453ed0b [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
Paul Lewis51474192020-01-09 16:02:22 +00005import {AuditController, Events, RuntimeSettings} from './AuditsController.js'; // eslint-disable-line no-unused-vars
6
7export class StatusView {
Patrick Hulcea087f622018-05-18 00:37:53 +00008 /**
Paul Lewis51474192020-01-09 16:02:22 +00009 * @param {!AuditController} controller
Patrick Hulcea087f622018-05-18 00:37:53 +000010 */
11 constructor(controller) {
12 this._controller = controller;
13
Blink Reformat4c46d092018-04-07 15:32:37 +000014 this._statusView = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000015 this._statusHeader = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000016 this._progressWrapper = null;
17 this._progressBar = null;
18 this._statusText = null;
Connor Clark99508362019-08-20 19:52:23 +000019 this._cancelButton = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000020
Patrick Hulcea087f622018-05-18 00:37:53 +000021 this._inspectedURL = '';
Blink Reformat4c46d092018-04-07 15:32:37 +000022 this._textChangedAt = 0;
Paul Lewiscf2ef222019-11-22 14:55:35 +000023 this._fastFactsQueued = FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000024 this._currentPhase = null;
25 this._scheduledTextChangeTimeout = null;
26 this._scheduledFastFactTimeout = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000027
28 this._dialog = new UI.Dialog();
Patrick Hulce8d387f12018-05-29 18:54:54 +000029 this._dialog.setDimmed(true);
30 this._dialog.setCloseOnEscape(false);
Patrick Hulcea087f622018-05-18 00:37:53 +000031 this._dialog.setOutsideClickCallback(event => event.consume(true));
32 this._render();
Blink Reformat4c46d092018-04-07 15:32:37 +000033 }
34
Patrick Hulcea087f622018-05-18 00:37:53 +000035 _render() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000036 const dialogRoot = UI.createShadowRootWithCoreStyles(this._dialog.contentElement, 'audits/auditsDialog.css');
37 const auditsViewElement = dialogRoot.createChild('div', 'audits-view vbox');
Blink Reformat4c46d092018-04-07 15:32:37 +000038
Patrick Hulcea087f622018-05-18 00:37:53 +000039 const cancelButton = UI.createTextButton(ls`Cancel`, this._cancel.bind(this));
40 const fragment = UI.Fragment.build`
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000041 <div class="audits-view vbox">
Patrick Hulcea087f622018-05-18 00:37:53 +000042 <h2 $="status-header">Auditing your web page\u2026</h2>
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000043 <div class="audits-status vbox" $="status-view">
44 <div class="audits-progress-wrapper" $="progress-wrapper">
45 <div class="audits-progress-bar" $="progress-bar"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000046 </div>
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000047 <div class="audits-status-text" $="status-text"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000048 </div>
49 ${cancelButton}
50 </div>
51 `;
Blink Reformat4c46d092018-04-07 15:32:37 +000052
Patrick Hulcea087f622018-05-18 00:37:53 +000053 auditsViewElement.appendChild(fragment.element());
Patrick Hulcea087f622018-05-18 00:37:53 +000054
55 this._statusView = fragment.$('status-view');
56 this._statusHeader = fragment.$('status-header');
57 this._progressWrapper = fragment.$('progress-wrapper');
58 this._progressBar = fragment.$('progress-bar');
59 this._statusText = fragment.$('status-text');
John Emau69c38762019-11-25 18:17:47 -080060 // Use StatusPhases array index as progress bar value
Paul Lewis51474192020-01-09 16:02:22 +000061 UI.ARIAUtils.markAsProgressBar(this._progressBar, 0, StatusPhases.length - 1);
Connor Clark99508362019-08-20 19:52:23 +000062 this._cancelButton = cancelButton;
John Emau463280d2019-07-19 00:37:40 +000063 UI.ARIAUtils.markAsStatus(this._statusText);
Patrick Hulcea087f622018-05-18 00:37:53 +000064
65 this._dialog.setDefaultFocusedElement(cancelButton);
66 this._dialog.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
67 this._dialog.setMaxContentSize(new UI.Size(500, 400));
Blink Reformat4c46d092018-04-07 15:32:37 +000068 }
69
Patrick Hulcea087f622018-05-18 00:37:53 +000070 _reset() {
Blink Reformat4c46d092018-04-07 15:32:37 +000071 this._resetProgressBarClasses();
72 clearTimeout(this._scheduledFastFactTimeout);
73
74 this._textChangedAt = 0;
Paul Lewiscf2ef222019-11-22 14:55:35 +000075 this._fastFactsQueued = FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000076 this._currentPhase = null;
77 this._scheduledTextChangeTimeout = null;
78 this._scheduledFastFactTimeout = null;
79 }
80
81 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000082 * @param {!Element} dialogRenderElement
Blink Reformat4c46d092018-04-07 15:32:37 +000083 */
Patrick Hulcea087f622018-05-18 00:37:53 +000084 show(dialogRenderElement) {
85 this._reset();
86 this.updateStatus(ls`Loading\u2026`);
Blink Reformat4c46d092018-04-07 15:32:37 +000087
Peter Marshall3e4e5692019-12-09 17:48:04 +010088 const parsedURL = Common.ParsedURL.fromString(this._inspectedURL);
Patrick Hulcea087f622018-05-18 00:37:53 +000089 const pageHost = parsedURL && parsedURL.host;
90 const statusHeader = pageHost ? ls`Auditing ${pageHost}` : ls`Auditing your web page`;
Connor Clark99508362019-08-20 19:52:23 +000091 this._renderStatusHeader(statusHeader);
Patrick Hulcea087f622018-05-18 00:37:53 +000092 this._dialog.show(dialogRenderElement);
93 }
94
Connor Clark99508362019-08-20 19:52:23 +000095 /**
96 * @param {string=} statusHeader
97 */
98 _renderStatusHeader(statusHeader) {
99 this._statusHeader.textContent = `${statusHeader}\u2026`;
100 }
101
Patrick Hulcea087f622018-05-18 00:37:53 +0000102 hide() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000103 if (this._dialog.isShowing()) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000104 this._dialog.hide();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000105 }
Patrick Hulcea087f622018-05-18 00:37:53 +0000106 }
107
108 /**
109 * @param {string=} url
110 */
111 setInspectedURL(url = '') {
112 this._inspectedURL = url;
Blink Reformat4c46d092018-04-07 15:32:37 +0000113 }
114
115 /**
116 * @param {?string} message
117 */
118 updateStatus(message) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000119 if (!message || !this._statusText) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000120 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000121 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000122
123 if (message.startsWith('Cancel')) {
124 this._commitTextChange(Common.UIString('Cancelling\u2026'));
125 clearTimeout(this._scheduledFastFactTimeout);
126 return;
127 }
128
129 const nextPhase = this._getPhaseForMessage(message);
Paul Lewis51474192020-01-09 16:02:22 +0000130 const nextPhaseIndex = StatusPhases.indexOf(nextPhase);
131 const currentPhaseIndex = StatusPhases.indexOf(this._currentPhase);
Blink Reformat4c46d092018-04-07 15:32:37 +0000132 if (!nextPhase && !this._currentPhase) {
133 this._commitTextChange(Common.UIString('Lighthouse is warming up\u2026'));
134 clearTimeout(this._scheduledFastFactTimeout);
John Emau69c38762019-11-25 18:17:47 -0800135 } else if (nextPhase && (!this._currentPhase || currentPhaseIndex < nextPhaseIndex)) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000136 this._currentPhase = nextPhase;
John Emau69c38762019-11-25 18:17:47 -0800137 const text = this._getMessageForPhase(nextPhase);
138 this._scheduleTextChange(text);
Blink Reformat4c46d092018-04-07 15:32:37 +0000139 this._scheduleFastFactCheck();
140 this._resetProgressBarClasses();
141 this._progressBar.classList.add(nextPhase.progressBarClass);
John Emau69c38762019-11-25 18:17:47 -0800142 UI.ARIAUtils.setProgressBarValue(this._progressBar, nextPhaseIndex, text);
Blink Reformat4c46d092018-04-07 15:32:37 +0000143 }
144 }
145
Patrick Hulcea087f622018-05-18 00:37:53 +0000146 _cancel() {
Paul Lewis51474192020-01-09 16:02:22 +0000147 this._controller.dispatchEventToListeners(Events.RequestAuditCancel);
Patrick Hulcea087f622018-05-18 00:37:53 +0000148 }
149
Blink Reformat4c46d092018-04-07 15:32:37 +0000150 /**
Paul Lewis51474192020-01-09 16:02:22 +0000151 * @param {!StatusPhases} phase
Blink Reformat4c46d092018-04-07 15:32:37 +0000152 * @return {string}
153 */
154 _getMessageForPhase(phase) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000155 if (phase.message) {
Mandy Chenef16e332019-09-08 05:02:45 +0000156 return phase.message;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000157 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000158
Paul Lewis51474192020-01-09 16:02:22 +0000159 const deviceType = RuntimeSettings.find(item => item.setting.name === 'audits.device_type').setting.get();
160 const throttling = RuntimeSettings.find(item => item.setting.name === 'audits.throttling').setting.get();
Paul Lewiscf2ef222019-11-22 14:55:35 +0000161 const match = LoadingMessages.find(item => {
Blink Reformat4c46d092018-04-07 15:32:37 +0000162 return item.deviceType === deviceType && item.throttling === throttling;
163 });
164
Mandy Chenef16e332019-09-08 05:02:45 +0000165 return match ? match.message : ls`Lighthouse is loading your page`;
Blink Reformat4c46d092018-04-07 15:32:37 +0000166 }
167
168 /**
169 * @param {string} message
Paul Lewis51474192020-01-09 16:02:22 +0000170 * @return {?StatusPhases}
Blink Reformat4c46d092018-04-07 15:32:37 +0000171 */
172 _getPhaseForMessage(message) {
Paul Lewiscf2ef222019-11-22 14:55:35 +0000173 return StatusPhases.find(phase => message.startsWith(phase.statusMessagePrefix));
Blink Reformat4c46d092018-04-07 15:32:37 +0000174 }
175
176 _resetProgressBarClasses() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000177 if (!this._progressBar) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000178 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000179 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000180
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000181 this._progressBar.className = 'audits-progress-bar';
Blink Reformat4c46d092018-04-07 15:32:37 +0000182 }
183
184 _scheduleFastFactCheck() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000185 if (!this._currentPhase || this._scheduledFastFactTimeout) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000186 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000187 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000188
189 this._scheduledFastFactTimeout = setTimeout(() => {
190 this._updateFastFactIfNecessary();
191 this._scheduledFastFactTimeout = null;
192
193 this._scheduleFastFactCheck();
194 }, 100);
195 }
196
197 _updateFastFactIfNecessary() {
198 const now = performance.now();
Paul Lewiscf2ef222019-11-22 14:55:35 +0000199 if (now - this._textChangedAt < fastFactRotationInterval) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000200 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000201 }
202 if (!this._fastFactsQueued.length) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000203 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000204 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000205
206 const fastFactIndex = Math.floor(Math.random() * this._fastFactsQueued.length);
207 this._scheduleTextChange(ls`\ud83d\udca1 ${this._fastFactsQueued[fastFactIndex]}`);
208 this._fastFactsQueued.splice(fastFactIndex, 1);
209 }
210
211 /**
212 * @param {string} text
213 */
214 _commitTextChange(text) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000215 if (!this._statusText) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000216 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000217 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000218 this._textChangedAt = performance.now();
219 this._statusText.textContent = text;
220 }
221
222 /**
223 * @param {string} text
224 */
225 _scheduleTextChange(text) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000226 if (this._scheduledTextChangeTimeout) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000227 clearTimeout(this._scheduledTextChangeTimeout);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000228 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000229
230 const msSinceLastChange = performance.now() - this._textChangedAt;
Paul Lewiscf2ef222019-11-22 14:55:35 +0000231 const msToTextChange = minimumTextVisibilityDuration - msSinceLastChange;
Blink Reformat4c46d092018-04-07 15:32:37 +0000232
233 this._scheduledTextChangeTimeout = setTimeout(() => {
234 this._commitTextChange(text);
235 }, Math.max(msToTextChange, 0));
236 }
237
238 /**
239 * @param {!Error} err
Blink Reformat4c46d092018-04-07 15:32:37 +0000240 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000241 renderBugReport(err) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000242 console.error(err);
243 clearTimeout(this._scheduledFastFactTimeout);
244 clearTimeout(this._scheduledTextChangeTimeout);
245 this._resetProgressBarClasses();
246 this._progressBar.classList.add('errored');
247
248 this._commitTextChange('');
Paul Irishf2659072019-03-24 19:08:52 +0000249 this._statusText.createChild('p').createTextChild(Common.UIString('Ah, sorry! We ran into an error.'));
Paul Lewiscf2ef222019-11-22 14:55:35 +0000250 if (KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000251 const message = Common.UIString(
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000252 'Try to navigate to the URL in a fresh Chrome profile without any other tabs or extensions open and try again.');
Blink Reformat4c46d092018-04-07 15:32:37 +0000253 this._statusText.createChild('p').createTextChild(message);
254 } else {
Paul Irishf2659072019-03-24 19:08:52 +0000255 this._renderBugReportBody(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000256 }
257 }
258
259 /**
Connor Clark99508362019-08-20 19:52:23 +0000260 * @param {string} statusHeader
261 * @param {string} text
262 */
263 renderText(statusHeader, text) {
264 this._renderStatusHeader(statusHeader);
265 this._commitTextChange(text);
266 }
267
268 /**
269 * @param {boolean} show
270 */
271 toggleCancelButton(show) {
272 this._cancelButton.style.visibility = show ? 'visible' : 'hidden';
273 }
274
275 /**
Blink Reformat4c46d092018-04-07 15:32:37 +0000276 * @param {!Error} err
277 * @param {string} auditURL
278 */
Paul Irishf2659072019-03-24 19:08:52 +0000279 _renderBugReportBody(err, auditURL) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000280 const issueBody = `
Paul Irishf2659072019-03-24 19:08:52 +0000281${err.message}
Blink Reformat4c46d092018-04-07 15:32:37 +0000282\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000283Channel: DevTools
284Initial URL: ${auditURL}
285Chrome Version: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
286Stack Trace: ${err.stack}
Blink Reformat4c46d092018-04-07 15:32:37 +0000287\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000288`;
289 this._statusText.createChild('p').createTextChild(
290 ls`If this issue is reproducible, please report it at the Lighthouse GitHub repo.`);
291 this._statusText.createChild('code', 'monospace').createTextChild(issueBody.trim());
Blink Reformat4c46d092018-04-07 15:32:37 +0000292 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000293}
Blink Reformat4c46d092018-04-07 15:32:37 +0000294
Paul Lewiscf2ef222019-11-22 14:55:35 +0000295/** @const */
296export const fastFactRotationInterval = 6000;
297
298/** @const */
299export const minimumTextVisibilityDuration = 3000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000300
301/** @type {!Array.<!RegExp>} */
Tim van der Lippec96ccd92019-11-29 16:23:54 +0000302const KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000303 /PARSING_PROBLEM/,
304 /DOCUMENT_REQUEST/,
305 /READ_FAILED/,
306 /TRACING_ALREADY_STARTED/,
307 /^You must provide a url to the runner/,
308 /^You probably have multiple tabs open/,
309];
310
John Emau69c38762019-11-25 18:17:47 -0800311/** @typedef {{message: string, progressBarClass: string}} */
Paul Lewiscf2ef222019-11-22 14:55:35 +0000312export const StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000313 {
314 id: 'loading',
315 progressBarClass: 'loading',
316 statusMessagePrefix: 'Loading page',
Blink Reformat4c46d092018-04-07 15:32:37 +0000317 },
318 {
319 id: 'gathering',
320 progressBarClass: 'gathering',
Mandy Chenef16e332019-09-08 05:02:45 +0000321 message: ls`Lighthouse is gathering information about the page to compute your score.`,
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000322 statusMessagePrefix: 'Gathering',
Blink Reformat4c46d092018-04-07 15:32:37 +0000323 },
324 {
325 id: 'auditing',
326 progressBarClass: 'auditing',
Mandy Chenef16e332019-09-08 05:02:45 +0000327 message: ls`Almost there! Lighthouse is now generating your report.`,
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000328 statusMessagePrefix: 'Auditing',
Blink Reformat4c46d092018-04-07 15:32:37 +0000329 }
330];
331
332/** @typedef {{message: string, deviceType: string, throttling: string}} */
Tim van der Lippec96ccd92019-11-29 16:23:54 +0000333const LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000334 {
335 deviceType: 'mobile',
336 throttling: 'on',
Mandy Chenef16e332019-09-08 05:02:45 +0000337 message: ls`Lighthouse is loading your page with throttling to measure performance on a mobile device on 3G.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000338 },
339 {
340 deviceType: 'desktop',
341 throttling: 'on',
Mandy Chenef16e332019-09-08 05:02:45 +0000342 message: ls`Lighthouse is loading your page with throttling to measure performance on a slow desktop on 3G.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000343 },
344 {
345 deviceType: 'mobile',
346 throttling: 'off',
Mandy Chenef16e332019-09-08 05:02:45 +0000347 message: ls`Lighthouse is loading your page with mobile emulation.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000348 },
349 {
350 deviceType: 'desktop',
351 throttling: 'off',
Mandy Chenef16e332019-09-08 05:02:45 +0000352 message: ls`Lighthouse is loading your page.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000353 },
354];
355
Tim van der Lippec96ccd92019-11-29 16:23:54 +0000356const FastFacts = [
Paul Lewiscf2ef222019-11-22 14:55:35 +0000357 ls
358`1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].`,
Connor Clarkc4170592020-01-13 14:26:54 -0800359 ls`Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]`, ls
Paul Lewiscf2ef222019-11-22 14:55:35 +0000360`By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]`,
361 ls`Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]`, ls
362`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]`,
363 ls`75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]`,
Connor Clarkc4170592020-01-13 14:26:54 -0800364 ls`The average user device costs less than 200 USD. [Source: International Data Corporation]`, ls
Paul Lewiscf2ef222019-11-22 14:55:35 +0000365`19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]`,
366 ls
Paul Lewiscf2ef222019-11-22 14:55:35 +0000367`70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]`,
368 ls
369`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]`,
370 ls
371`As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]`,
372 ls`70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]`, ls
373 `Lighthouse only simulates mobile performance; to measure performance on a real device, try WebPageTest.org [Source: Lighthouse team]`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000374];