blob: 3ebd460c176f21ffd7d81071fa72541cf989edba [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;
17
Patrick Hulcea087f622018-05-18 00:37:53 +000018 this._inspectedURL = '';
Blink Reformat4c46d092018-04-07 15:32:37 +000019 this._textChangedAt = 0;
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000020 this._fastFactsQueued = Audits.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();
Patrick Hulce8d387f12018-05-29 18:54:54 +000026 this._dialog.setDimmed(true);
27 this._dialog.setCloseOnEscape(false);
Patrick Hulcea087f622018-05-18 00:37:53 +000028 this._dialog.setOutsideClickCallback(event => event.consume(true));
29 this._render();
Blink Reformat4c46d092018-04-07 15:32:37 +000030 }
31
Patrick Hulcea087f622018-05-18 00:37:53 +000032 _render() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000033 const dialogRoot = UI.createShadowRootWithCoreStyles(this._dialog.contentElement, 'audits/auditsDialog.css');
34 const auditsViewElement = dialogRoot.createChild('div', 'audits-view vbox');
Blink Reformat4c46d092018-04-07 15:32:37 +000035
Patrick Hulcea087f622018-05-18 00:37:53 +000036 const cancelButton = UI.createTextButton(ls`Cancel`, this._cancel.bind(this));
37 const fragment = UI.Fragment.build`
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000038 <div class="audits-view vbox">
Patrick Hulcea087f622018-05-18 00:37:53 +000039 <h2 $="status-header">Auditing your web page\u2026</h2>
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000040 <div class="audits-status vbox" $="status-view">
41 <div class="audits-progress-wrapper" $="progress-wrapper">
42 <div class="audits-progress-bar" $="progress-bar"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000043 </div>
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000044 <div class="audits-status-text" $="status-text"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000045 </div>
46 ${cancelButton}
47 </div>
48 `;
Blink Reformat4c46d092018-04-07 15:32:37 +000049
Patrick Hulcea087f622018-05-18 00:37:53 +000050 auditsViewElement.appendChild(fragment.element());
51 auditsViewElement.tabIndex = 0;
52
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');
John Emau463280d2019-07-19 00:37:40 +000058 UI.ARIAUtils.markAsStatus(this._statusText);
Patrick Hulcea087f622018-05-18 00:37:53 +000059
60 this._dialog.setDefaultFocusedElement(cancelButton);
61 this._dialog.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
62 this._dialog.setMaxContentSize(new UI.Size(500, 400));
Blink Reformat4c46d092018-04-07 15:32:37 +000063 }
64
Patrick Hulcea087f622018-05-18 00:37:53 +000065 _reset() {
Blink Reformat4c46d092018-04-07 15:32:37 +000066 this._resetProgressBarClasses();
67 clearTimeout(this._scheduledFastFactTimeout);
68
69 this._textChangedAt = 0;
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000070 this._fastFactsQueued = Audits.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000071 this._currentPhase = null;
72 this._scheduledTextChangeTimeout = null;
73 this._scheduledFastFactTimeout = null;
74 }
75
76 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000077 * @param {!Element} dialogRenderElement
Blink Reformat4c46d092018-04-07 15:32:37 +000078 */
Patrick Hulcea087f622018-05-18 00:37:53 +000079 show(dialogRenderElement) {
80 this._reset();
81 this.updateStatus(ls`Loading\u2026`);
Blink Reformat4c46d092018-04-07 15:32:37 +000082
Patrick Hulcea087f622018-05-18 00:37:53 +000083 const parsedURL = this._inspectedURL.asParsedURL();
84 const pageHost = parsedURL && parsedURL.host;
85 const statusHeader = pageHost ? ls`Auditing ${pageHost}` : ls`Auditing your web page`;
86 this._statusHeader.textContent = `${statusHeader}\u2026`;
87 this._dialog.show(dialogRenderElement);
88 }
89
90 hide() {
91 if (this._dialog.isShowing())
92 this._dialog.hide();
93 }
94
95 /**
96 * @param {string=} url
97 */
98 setInspectedURL(url = '') {
99 this._inspectedURL = url;
Blink Reformat4c46d092018-04-07 15:32:37 +0000100 }
101
102 /**
103 * @param {?string} message
104 */
105 updateStatus(message) {
106 if (!message || !this._statusText)
107 return;
108
109 if (message.startsWith('Cancel')) {
110 this._commitTextChange(Common.UIString('Cancelling\u2026'));
111 clearTimeout(this._scheduledFastFactTimeout);
112 return;
113 }
114
115 const nextPhase = this._getPhaseForMessage(message);
116 if (!nextPhase && !this._currentPhase) {
117 this._commitTextChange(Common.UIString('Lighthouse is warming up\u2026'));
118 clearTimeout(this._scheduledFastFactTimeout);
119 } else if (nextPhase && (!this._currentPhase || this._currentPhase.order < nextPhase.order)) {
120 this._currentPhase = nextPhase;
121 this._scheduleTextChange(this._getMessageForPhase(nextPhase));
122 this._scheduleFastFactCheck();
123 this._resetProgressBarClasses();
124 this._progressBar.classList.add(nextPhase.progressBarClass);
125 }
126 }
127
Patrick Hulcea087f622018-05-18 00:37:53 +0000128 _cancel() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000129 this._controller.dispatchEventToListeners(Audits.Events.RequestAuditCancel);
Patrick Hulcea087f622018-05-18 00:37:53 +0000130 }
131
Blink Reformat4c46d092018-04-07 15:32:37 +0000132 /**
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000133 * @param {!Audits.StatusView.StatusPhases} phase
Blink Reformat4c46d092018-04-07 15:32:37 +0000134 * @return {string}
135 */
136 _getMessageForPhase(phase) {
137 if (phase.message)
138 return Common.UIString(phase.message);
139
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000140 const deviceType = Audits.RuntimeSettings.find(item => item.setting.name === 'audits.device_type').setting.get();
141 const throttling = Audits.RuntimeSettings.find(item => item.setting.name === 'audits.throttling').setting.get();
142 const match = Audits.StatusView.LoadingMessages.find(item => {
Blink Reformat4c46d092018-04-07 15:32:37 +0000143 return item.deviceType === deviceType && item.throttling === throttling;
144 });
145
146 return match ? ls`${match.message}` : ls`Lighthouse is loading your page`;
147 }
148
149 /**
150 * @param {string} message
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000151 * @return {?Audits.StatusView.StatusPhases}
Blink Reformat4c46d092018-04-07 15:32:37 +0000152 */
153 _getPhaseForMessage(message) {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000154 return Audits.StatusView.StatusPhases.find(phase => message.startsWith(phase.statusMessagePrefix));
Blink Reformat4c46d092018-04-07 15:32:37 +0000155 }
156
157 _resetProgressBarClasses() {
158 if (!this._progressBar)
159 return;
160
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000161 this._progressBar.className = 'audits-progress-bar';
Blink Reformat4c46d092018-04-07 15:32:37 +0000162 }
163
164 _scheduleFastFactCheck() {
165 if (!this._currentPhase || this._scheduledFastFactTimeout)
166 return;
167
168 this._scheduledFastFactTimeout = setTimeout(() => {
169 this._updateFastFactIfNecessary();
170 this._scheduledFastFactTimeout = null;
171
172 this._scheduleFastFactCheck();
173 }, 100);
174 }
175
176 _updateFastFactIfNecessary() {
177 const now = performance.now();
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000178 if (now - this._textChangedAt < Audits.StatusView.fastFactRotationInterval)
Blink Reformat4c46d092018-04-07 15:32:37 +0000179 return;
180 if (!this._fastFactsQueued.length)
181 return;
182
183 const fastFactIndex = Math.floor(Math.random() * this._fastFactsQueued.length);
184 this._scheduleTextChange(ls`\ud83d\udca1 ${this._fastFactsQueued[fastFactIndex]}`);
185 this._fastFactsQueued.splice(fastFactIndex, 1);
186 }
187
188 /**
189 * @param {string} text
190 */
191 _commitTextChange(text) {
192 if (!this._statusText)
193 return;
194 this._textChangedAt = performance.now();
195 this._statusText.textContent = text;
196 }
197
198 /**
199 * @param {string} text
200 */
201 _scheduleTextChange(text) {
202 if (this._scheduledTextChangeTimeout)
203 clearTimeout(this._scheduledTextChangeTimeout);
204
205 const msSinceLastChange = performance.now() - this._textChangedAt;
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000206 const msToTextChange = Audits.StatusView.minimumTextVisibilityDuration - msSinceLastChange;
Blink Reformat4c46d092018-04-07 15:32:37 +0000207
208 this._scheduledTextChangeTimeout = setTimeout(() => {
209 this._commitTextChange(text);
210 }, Math.max(msToTextChange, 0));
211 }
212
213 /**
214 * @param {!Error} err
Blink Reformat4c46d092018-04-07 15:32:37 +0000215 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000216 renderBugReport(err) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000217 console.error(err);
218 clearTimeout(this._scheduledFastFactTimeout);
219 clearTimeout(this._scheduledTextChangeTimeout);
220 this._resetProgressBarClasses();
221 this._progressBar.classList.add('errored');
222
223 this._commitTextChange('');
Paul Irishf2659072019-03-24 19:08:52 +0000224 this._statusText.createChild('p').createTextChild(Common.UIString('Ah, sorry! We ran into an error.'));
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000225 if (Audits.StatusView.KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000226 const message = Common.UIString(
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000227 '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 +0000228 this._statusText.createChild('p').createTextChild(message);
229 } else {
Paul Irishf2659072019-03-24 19:08:52 +0000230 this._renderBugReportBody(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000231 }
232 }
233
234 /**
235 * @param {!Error} err
236 * @param {string} auditURL
237 */
Paul Irishf2659072019-03-24 19:08:52 +0000238 _renderBugReportBody(err, auditURL) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000239 const issueBody = `
Paul Irishf2659072019-03-24 19:08:52 +0000240${err.message}
Blink Reformat4c46d092018-04-07 15:32:37 +0000241\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000242Channel: DevTools
243Initial URL: ${auditURL}
244Chrome Version: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
245Stack Trace: ${err.stack}
Blink Reformat4c46d092018-04-07 15:32:37 +0000246\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000247`;
248 this._statusText.createChild('p').createTextChild(
249 ls`If this issue is reproducible, please report it at the Lighthouse GitHub repo.`);
250 this._statusText.createChild('code', 'monospace').createTextChild(issueBody.trim());
Blink Reformat4c46d092018-04-07 15:32:37 +0000251 }
252};
253
254
255/** @type {!Array.<!RegExp>} */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000256Audits.StatusView.KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000257 /PARSING_PROBLEM/,
258 /DOCUMENT_REQUEST/,
259 /READ_FAILED/,
260 /TRACING_ALREADY_STARTED/,
261 /^You must provide a url to the runner/,
262 /^You probably have multiple tabs open/,
263];
264
265/** @typedef {{message: string, progressBarClass: string, order: number}} */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000266Audits.StatusView.StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000267 {
268 id: 'loading',
269 progressBarClass: 'loading',
270 statusMessagePrefix: 'Loading page',
271 order: 10,
272 },
273 {
274 id: 'gathering',
275 progressBarClass: 'gathering',
276 message: 'Lighthouse is gathering information about the page to compute your score.',
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000277 statusMessagePrefix: 'Gathering',
Blink Reformat4c46d092018-04-07 15:32:37 +0000278 order: 20,
279 },
280 {
281 id: 'auditing',
282 progressBarClass: 'auditing',
Paul Irish5726a182018-08-30 17:04:23 +0000283 message: 'Almost there! Lighthouse is now generating your report.',
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000284 statusMessagePrefix: 'Auditing',
Blink Reformat4c46d092018-04-07 15:32:37 +0000285 order: 30,
286 }
287];
288
289/** @typedef {{message: string, deviceType: string, throttling: string}} */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000290Audits.StatusView.LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000291 {
292 deviceType: 'mobile',
293 throttling: 'on',
294 message: 'Lighthouse is loading your page with throttling to measure performance on a mobile device on 3G.',
295 },
296 {
297 deviceType: 'desktop',
298 throttling: 'on',
299 message: 'Lighthouse is loading your page with throttling to measure performance on a slow desktop on 3G.',
300 },
301 {
302 deviceType: 'mobile',
303 throttling: 'off',
304 message: 'Lighthouse is loading your page with mobile emulation.',
305 },
306 {
307 deviceType: 'desktop',
308 throttling: 'off',
309 message: 'Lighthouse is loading your page.',
310 },
311];
312
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000313Audits.StatusView.FastFacts = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000314 '1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].',
315 'Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]',
316 'BBC has seen a loss of 10% of their users for every extra second of page load [Source: WPO Stats]',
317 'By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]',
318 'Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]',
319 '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]',
320 '75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]',
321 'The average user device costs less than 200 USD. [Source: International Data Corporation]',
322 '53% of all site visits are abandoned if page load takes more than 3 seconds [Source: Google DoubleClick blog]',
323 '19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]',
324 '14 seconds is the average time a mobile web page takes to load on a 4G connection [Source: Google DoubleClick blog]',
325 '70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]',
326 '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]',
327 'As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]',
328 '70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]',
329 'Lighthouse only simulates mobile performance; to measure performance on a real device, try WebPageTest.org [Source: Lighthouse team]',
330];
331
332/** @const */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000333Audits.StatusView.fastFactRotationInterval = 6000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000334/** @const */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000335Audits.StatusView.minimumTextVisibilityDuration = 3000;