blob: c480e788cdc2729ac5509cc8b9150bbc56e537bd [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
cjamcl@google.comaa1532c2019-05-31 03:01:24 +00005Audits.StatusView = class {
Patrick Hulcea087f622018-05-18 00:37:53 +00006 /**
cjamcl@google.comaa1532c2019-05-31 03:01:24 +00007 * @param {!Audits.AuditController} controller
Patrick Hulcea087f622018-05-18 00:37:53 +00008 */
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;
Connor Clark99508362019-08-20 19:52:23 +000017 this._cancelButton = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000018
Patrick Hulcea087f622018-05-18 00:37:53 +000019 this._inspectedURL = '';
Blink Reformat4c46d092018-04-07 15:32:37 +000020 this._textChangedAt = 0;
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000021 this._fastFactsQueued = Audits.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000022 this._currentPhase = null;
23 this._scheduledTextChangeTimeout = null;
24 this._scheduledFastFactTimeout = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000025
26 this._dialog = new UI.Dialog();
Patrick Hulce8d387f12018-05-29 18:54:54 +000027 this._dialog.setDimmed(true);
28 this._dialog.setCloseOnEscape(false);
Patrick Hulcea087f622018-05-18 00:37:53 +000029 this._dialog.setOutsideClickCallback(event => event.consume(true));
30 this._render();
Blink Reformat4c46d092018-04-07 15:32:37 +000031 }
32
Patrick Hulcea087f622018-05-18 00:37:53 +000033 _render() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000034 const dialogRoot = UI.createShadowRootWithCoreStyles(this._dialog.contentElement, 'audits/auditsDialog.css');
35 const auditsViewElement = dialogRoot.createChild('div', 'audits-view vbox');
Blink Reformat4c46d092018-04-07 15:32:37 +000036
Patrick Hulcea087f622018-05-18 00:37:53 +000037 const cancelButton = UI.createTextButton(ls`Cancel`, this._cancel.bind(this));
38 const fragment = UI.Fragment.build`
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000039 <div class="audits-view vbox">
Patrick Hulcea087f622018-05-18 00:37:53 +000040 <h2 $="status-header">Auditing your web page\u2026</h2>
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000041 <div class="audits-status vbox" $="status-view">
42 <div class="audits-progress-wrapper" $="progress-wrapper">
43 <div class="audits-progress-bar" $="progress-bar"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000044 </div>
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000045 <div class="audits-status-text" $="status-text"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000046 </div>
47 ${cancelButton}
48 </div>
49 `;
Blink Reformat4c46d092018-04-07 15:32:37 +000050
Patrick Hulcea087f622018-05-18 00:37:53 +000051 auditsViewElement.appendChild(fragment.element());
Patrick Hulcea087f622018-05-18 00:37:53 +000052
53 this._statusView = fragment.$('status-view');
54 this._statusHeader = fragment.$('status-header');
55 this._progressWrapper = fragment.$('progress-wrapper');
56 this._progressBar = fragment.$('progress-bar');
57 this._statusText = fragment.$('status-text');
Connor Clark99508362019-08-20 19:52:23 +000058 this._cancelButton = cancelButton;
John Emau463280d2019-07-19 00:37:40 +000059 UI.ARIAUtils.markAsStatus(this._statusText);
Patrick Hulcea087f622018-05-18 00:37:53 +000060
61 this._dialog.setDefaultFocusedElement(cancelButton);
62 this._dialog.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
63 this._dialog.setMaxContentSize(new UI.Size(500, 400));
Blink Reformat4c46d092018-04-07 15:32:37 +000064 }
65
Patrick Hulcea087f622018-05-18 00:37:53 +000066 _reset() {
Blink Reformat4c46d092018-04-07 15:32:37 +000067 this._resetProgressBarClasses();
68 clearTimeout(this._scheduledFastFactTimeout);
69
70 this._textChangedAt = 0;
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000071 this._fastFactsQueued = Audits.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000072 this._currentPhase = null;
73 this._scheduledTextChangeTimeout = null;
74 this._scheduledFastFactTimeout = null;
75 }
76
77 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000078 * @param {!Element} dialogRenderElement
Blink Reformat4c46d092018-04-07 15:32:37 +000079 */
Patrick Hulcea087f622018-05-18 00:37:53 +000080 show(dialogRenderElement) {
81 this._reset();
82 this.updateStatus(ls`Loading\u2026`);
Blink Reformat4c46d092018-04-07 15:32:37 +000083
Patrick Hulcea087f622018-05-18 00:37:53 +000084 const parsedURL = this._inspectedURL.asParsedURL();
85 const pageHost = parsedURL && parsedURL.host;
86 const statusHeader = pageHost ? ls`Auditing ${pageHost}` : ls`Auditing your web page`;
Connor Clark99508362019-08-20 19:52:23 +000087 this._renderStatusHeader(statusHeader);
Patrick Hulcea087f622018-05-18 00:37:53 +000088 this._dialog.show(dialogRenderElement);
89 }
90
Connor Clark99508362019-08-20 19:52:23 +000091 /**
92 * @param {string=} statusHeader
93 */
94 _renderStatusHeader(statusHeader) {
95 this._statusHeader.textContent = `${statusHeader}\u2026`;
96 }
97
Patrick Hulcea087f622018-05-18 00:37:53 +000098 hide() {
99 if (this._dialog.isShowing())
100 this._dialog.hide();
101 }
102
103 /**
104 * @param {string=} url
105 */
106 setInspectedURL(url = '') {
107 this._inspectedURL = url;
Blink Reformat4c46d092018-04-07 15:32:37 +0000108 }
109
110 /**
111 * @param {?string} message
112 */
113 updateStatus(message) {
114 if (!message || !this._statusText)
115 return;
116
117 if (message.startsWith('Cancel')) {
118 this._commitTextChange(Common.UIString('Cancelling\u2026'));
119 clearTimeout(this._scheduledFastFactTimeout);
120 return;
121 }
122
123 const nextPhase = this._getPhaseForMessage(message);
124 if (!nextPhase && !this._currentPhase) {
125 this._commitTextChange(Common.UIString('Lighthouse is warming up\u2026'));
126 clearTimeout(this._scheduledFastFactTimeout);
127 } else if (nextPhase && (!this._currentPhase || this._currentPhase.order < nextPhase.order)) {
128 this._currentPhase = nextPhase;
129 this._scheduleTextChange(this._getMessageForPhase(nextPhase));
130 this._scheduleFastFactCheck();
131 this._resetProgressBarClasses();
132 this._progressBar.classList.add(nextPhase.progressBarClass);
133 }
134 }
135
Patrick Hulcea087f622018-05-18 00:37:53 +0000136 _cancel() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000137 this._controller.dispatchEventToListeners(Audits.Events.RequestAuditCancel);
Patrick Hulcea087f622018-05-18 00:37:53 +0000138 }
139
Blink Reformat4c46d092018-04-07 15:32:37 +0000140 /**
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000141 * @param {!Audits.StatusView.StatusPhases} phase
Blink Reformat4c46d092018-04-07 15:32:37 +0000142 * @return {string}
143 */
144 _getMessageForPhase(phase) {
145 if (phase.message)
Mandy Chenef16e332019-09-08 05:02:45 +0000146 return phase.message;
Blink Reformat4c46d092018-04-07 15:32:37 +0000147
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000148 const deviceType = Audits.RuntimeSettings.find(item => item.setting.name === 'audits.device_type').setting.get();
149 const throttling = Audits.RuntimeSettings.find(item => item.setting.name === 'audits.throttling').setting.get();
150 const match = Audits.StatusView.LoadingMessages.find(item => {
Blink Reformat4c46d092018-04-07 15:32:37 +0000151 return item.deviceType === deviceType && item.throttling === throttling;
152 });
153
Mandy Chenef16e332019-09-08 05:02:45 +0000154 return match ? match.message : ls`Lighthouse is loading your page`;
Blink Reformat4c46d092018-04-07 15:32:37 +0000155 }
156
157 /**
158 * @param {string} message
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000159 * @return {?Audits.StatusView.StatusPhases}
Blink Reformat4c46d092018-04-07 15:32:37 +0000160 */
161 _getPhaseForMessage(message) {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000162 return Audits.StatusView.StatusPhases.find(phase => message.startsWith(phase.statusMessagePrefix));
Blink Reformat4c46d092018-04-07 15:32:37 +0000163 }
164
165 _resetProgressBarClasses() {
166 if (!this._progressBar)
167 return;
168
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000169 this._progressBar.className = 'audits-progress-bar';
Blink Reformat4c46d092018-04-07 15:32:37 +0000170 }
171
172 _scheduleFastFactCheck() {
173 if (!this._currentPhase || this._scheduledFastFactTimeout)
174 return;
175
176 this._scheduledFastFactTimeout = setTimeout(() => {
177 this._updateFastFactIfNecessary();
178 this._scheduledFastFactTimeout = null;
179
180 this._scheduleFastFactCheck();
181 }, 100);
182 }
183
184 _updateFastFactIfNecessary() {
185 const now = performance.now();
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000186 if (now - this._textChangedAt < Audits.StatusView.fastFactRotationInterval)
Blink Reformat4c46d092018-04-07 15:32:37 +0000187 return;
188 if (!this._fastFactsQueued.length)
189 return;
190
191 const fastFactIndex = Math.floor(Math.random() * this._fastFactsQueued.length);
192 this._scheduleTextChange(ls`\ud83d\udca1 ${this._fastFactsQueued[fastFactIndex]}`);
193 this._fastFactsQueued.splice(fastFactIndex, 1);
194 }
195
196 /**
197 * @param {string} text
198 */
199 _commitTextChange(text) {
200 if (!this._statusText)
201 return;
202 this._textChangedAt = performance.now();
203 this._statusText.textContent = text;
204 }
205
206 /**
207 * @param {string} text
208 */
209 _scheduleTextChange(text) {
210 if (this._scheduledTextChangeTimeout)
211 clearTimeout(this._scheduledTextChangeTimeout);
212
213 const msSinceLastChange = performance.now() - this._textChangedAt;
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000214 const msToTextChange = Audits.StatusView.minimumTextVisibilityDuration - msSinceLastChange;
Blink Reformat4c46d092018-04-07 15:32:37 +0000215
216 this._scheduledTextChangeTimeout = setTimeout(() => {
217 this._commitTextChange(text);
218 }, Math.max(msToTextChange, 0));
219 }
220
221 /**
222 * @param {!Error} err
Blink Reformat4c46d092018-04-07 15:32:37 +0000223 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000224 renderBugReport(err) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000225 console.error(err);
226 clearTimeout(this._scheduledFastFactTimeout);
227 clearTimeout(this._scheduledTextChangeTimeout);
228 this._resetProgressBarClasses();
229 this._progressBar.classList.add('errored');
230
231 this._commitTextChange('');
Paul Irishf2659072019-03-24 19:08:52 +0000232 this._statusText.createChild('p').createTextChild(Common.UIString('Ah, sorry! We ran into an error.'));
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000233 if (Audits.StatusView.KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000234 const message = Common.UIString(
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000235 '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 +0000236 this._statusText.createChild('p').createTextChild(message);
237 } else {
Paul Irishf2659072019-03-24 19:08:52 +0000238 this._renderBugReportBody(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000239 }
240 }
241
242 /**
Connor Clark99508362019-08-20 19:52:23 +0000243 * @param {string} statusHeader
244 * @param {string} text
245 */
246 renderText(statusHeader, text) {
247 this._renderStatusHeader(statusHeader);
248 this._commitTextChange(text);
249 }
250
251 /**
252 * @param {boolean} show
253 */
254 toggleCancelButton(show) {
255 this._cancelButton.style.visibility = show ? 'visible' : 'hidden';
256 }
257
258 /**
Blink Reformat4c46d092018-04-07 15:32:37 +0000259 * @param {!Error} err
260 * @param {string} auditURL
261 */
Paul Irishf2659072019-03-24 19:08:52 +0000262 _renderBugReportBody(err, auditURL) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000263 const issueBody = `
Paul Irishf2659072019-03-24 19:08:52 +0000264${err.message}
Blink Reformat4c46d092018-04-07 15:32:37 +0000265\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000266Channel: DevTools
267Initial URL: ${auditURL}
268Chrome Version: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
269Stack Trace: ${err.stack}
Blink Reformat4c46d092018-04-07 15:32:37 +0000270\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000271`;
272 this._statusText.createChild('p').createTextChild(
273 ls`If this issue is reproducible, please report it at the Lighthouse GitHub repo.`);
274 this._statusText.createChild('code', 'monospace').createTextChild(issueBody.trim());
Blink Reformat4c46d092018-04-07 15:32:37 +0000275 }
276};
277
278
279/** @type {!Array.<!RegExp>} */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000280Audits.StatusView.KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000281 /PARSING_PROBLEM/,
282 /DOCUMENT_REQUEST/,
283 /READ_FAILED/,
284 /TRACING_ALREADY_STARTED/,
285 /^You must provide a url to the runner/,
286 /^You probably have multiple tabs open/,
287];
288
289/** @typedef {{message: string, progressBarClass: string, order: number}} */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000290Audits.StatusView.StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000291 {
292 id: 'loading',
293 progressBarClass: 'loading',
294 statusMessagePrefix: 'Loading page',
295 order: 10,
296 },
297 {
298 id: 'gathering',
299 progressBarClass: 'gathering',
Mandy Chenef16e332019-09-08 05:02:45 +0000300 message: ls`Lighthouse is gathering information about the page to compute your score.`,
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000301 statusMessagePrefix: 'Gathering',
Blink Reformat4c46d092018-04-07 15:32:37 +0000302 order: 20,
303 },
304 {
305 id: 'auditing',
306 progressBarClass: 'auditing',
Mandy Chenef16e332019-09-08 05:02:45 +0000307 message: ls`Almost there! Lighthouse is now generating your report.`,
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000308 statusMessagePrefix: 'Auditing',
Blink Reformat4c46d092018-04-07 15:32:37 +0000309 order: 30,
310 }
311];
312
313/** @typedef {{message: string, deviceType: string, throttling: string}} */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000314Audits.StatusView.LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000315 {
316 deviceType: 'mobile',
317 throttling: 'on',
Mandy Chenef16e332019-09-08 05:02:45 +0000318 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 +0000319 },
320 {
321 deviceType: 'desktop',
322 throttling: 'on',
Mandy Chenef16e332019-09-08 05:02:45 +0000323 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 +0000324 },
325 {
326 deviceType: 'mobile',
327 throttling: 'off',
Mandy Chenef16e332019-09-08 05:02:45 +0000328 message: ls`Lighthouse is loading your page with mobile emulation.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000329 },
330 {
331 deviceType: 'desktop',
332 throttling: 'off',
Mandy Chenef16e332019-09-08 05:02:45 +0000333 message: ls`Lighthouse is loading your page.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000334 },
335];
336
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000337Audits.StatusView.FastFacts = [
Mandy Chenef16e332019-09-08 05:02:45 +0000338 ls`1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].`,
339 ls`Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]`,
340 ls`BBC has seen a loss of 10% of their users for every extra second of page load [Source: WPO Stats]`,
341 ls`By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]`,
342 ls`Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]`,
343 ls`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]`,
344 ls`75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]`,
345 ls`The average user device costs less than 200 USD. [Source: International Data Corporation]`,
346 ls`53% of all site visits are abandoned if page load takes more than 3 seconds [Source: Google DoubleClick blog]`,
347 ls`19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]`,
348 ls`14 seconds is the average time a mobile web page takes to load on a 4G connection [Source: Google DoubleClick blog]`,
349 ls`70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]`,
350 ls`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]`,
351 ls`As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]`,
352 ls`70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]`,
353 ls`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 +0000354];
355
356/** @const */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000357Audits.StatusView.fastFactRotationInterval = 6000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000358/** @const */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000359Audits.StatusView.minimumTextVisibilityDuration = 3000;