blob: ba3e7f8e372ba8cb3718a4d5455dee1982fc7441 [file] [log] [blame]
Patrick Hulcea087f622018-05-18 00:37:53 +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 Lewisdaac1062020-03-05 14:37:10 +00005import * as Common from '../common/common.js';
6import * as Components from '../components/components.js';
Tim van der Lippefe3cdc92020-11-06 15:23:13 +00007import * as Host from '../host/host.js';
Simon Zünd2c704cd2020-06-04 11:08:35 +02008import * as Platform from '../platform/platform.js';
Tim van der Lippe5df64b22020-09-11 13:04:24 +01009import * as Root from '../root/root.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000010import * as SDK from '../sdk/sdk.js';
Paul Lewisca569a52020-09-09 17:11:51 +010011import * as ThemeSupport from '../theme_support/theme_support.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000012import * as Timeline from '../timeline/timeline.js';
13import * as UI from '../ui/ui.js';
Tim van der Lippe696c9262020-08-26 15:39:32 +010014import * as Workspace from '../workspace/workspace.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000015
Tim van der Lippe1e10f852020-10-30 14:35:01 +000016import * as ReportRenderer from './LighthouseReporterTypes.js'; // eslint-disable-line no-unused-vars
17
Paul Lewiscf2ef222019-11-22 14:55:35 +000018const MaxLengthForLinks = 40;
19
Patrick Hulcea087f622018-05-18 00:37:53 +000020/**
21 * @override
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000022 * @extends {ReportRenderer.ReportRenderer}
Patrick Hulcea087f622018-05-18 00:37:53 +000023 */
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000024// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Tim van der Lippe1e10f852020-10-30 14:35:01 +000025export class LighthouseReportRenderer extends self.ReportRenderer {
Patrick Hulcea087f622018-05-18 00:37:53 +000026 /**
Tim van der Lippe4df32c92020-11-06 12:35:05 +000027 * @param {!DOM} dom
28 */
29 constructor(dom) {
30 super(dom);
31 }
32 /**
Paul Irish8f1e33d2018-05-31 02:29:50 +000033 * @param {!Element} el Parent element to render the report into.
34 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +000035 */
Paul Irish8f1e33d2018-05-31 02:29:50 +000036 static addViewTraceButton(el, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000037 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000038 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000039 }
Patrick Hulcea087f622018-05-18 00:37:53 +000040
Adam Rainef46d1582020-08-17 20:24:32 +000041 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000042 const container = el.querySelector('.lh-audit-group');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000043 if (!container) {
44 return;
45 }
Paul Irishbf204682020-05-13 16:11:37 -070046 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
47 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
48 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000049 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000050 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000051
Paul Irish8f1e33d2018-05-31 02:29:50 +000052 const defaultPassTrace = artifacts.traces.defaultPass;
Adam Rainef46d1582020-08-17 20:24:32 +000053 const label = simulated ? Common.UIString.UIString('View Original Trace') : Common.UIString.UIString('View Trace');
54 const timelineButton = UI.UIUtils.createTextButton(label, onViewTraceClick, 'view-trace');
55 if (simulated) {
56 timelineButton.title = Common.UIString.UIString(
57 'The performance metrics above are simulated and won\'t match the timings found in this trace. Disable simulated throttling in "Lighthouse Settings" if you want the timings to match.');
58 }
Paul Irishbf204682020-05-13 16:11:37 -070059 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000060
61 async function onViewTraceClick() {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000062 Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010063 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000064 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000065 }
Patrick Hulcea087f622018-05-18 00:37:53 +000066 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000067
68 /**
69 * @param {!Element} el
70 */
71 static async linkifyNodeDetails(el) {
Paul Lewisdaac1062020-03-05 14:37:10 +000072 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000073 if (!mainTarget) {
74 return;
75 }
Paul Lewisdaac1062020-03-05 14:37:10 +000076 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000077 if (!domModel) {
78 return;
79 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000080
81 for (const origElement of el.getElementsByClassName('lh-node')) {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000082 const origHTMLElement = /** @type {!HTMLElement} */ (origElement);
83 const detailsItem = /** @type {!ReportRenderer.NodeDetailsJSON} */ (origHTMLElement.dataset);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000084 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000085 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000086 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000087
88 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
89
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000090 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000091 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000092 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000093 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000094 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000095 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000096 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000097
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000098 const element = await Common.Linkifier.Linkifier.linkify(
99 node, {tooltip: detailsItem.snippet, preventKeyboardFocus: undefined});
100 origHTMLElement.title = '';
101 origHTMLElement.textContent = '';
102 origHTMLElement.appendChild(element);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000103 }
104 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000105
106 /**
107 * @param {!Element} el
108 */
Connor Clark0403a422019-11-18 18:03:18 -0800109 static async linkifySourceLocationDetails(el) {
110 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000111 const origHTMLElement = /** @type {!HTMLElement} */ (origElement);
112 const detailsItem = /** @type {!ReportRenderer.SourceLocationDetailsJSON} */ (origHTMLElement.dataset);
Connor Clark0403a422019-11-18 18:03:18 -0800113 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
114 continue;
115 }
116 const url = detailsItem.sourceUrl;
117 const line = Number(detailsItem.sourceLine);
118 const column = Number(detailsItem.sourceColumn);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000119 const element = await Components.Linkifier.Linkifier.linkifyURL(url, {
120 lineNumber: line,
121 columnNumber: column,
122 maxLength: MaxLengthForLinks,
123 bypassURLTrimming: undefined,
124 className: undefined,
125 preventClick: undefined,
126 tabStop: undefined,
127 text: undefined
128 });
129 origHTMLElement.title = '';
130 origHTMLElement.textContent = '';
131 origHTMLElement.appendChild(element);
Connor Clark0403a422019-11-18 18:03:18 -0800132 }
133 }
134
135 /**
136 * @param {!Element} el
137 */
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000138 static handleDarkMode(el) {
Paul Lewisca569a52020-09-09 17:11:51 +0100139 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000140 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000141 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000142 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000143}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000144
145/**
146 * @override
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000147 * @extends {ReportRenderer.ReportUIFeatures}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000148 */
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000149// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Tim van der Lippe1e10f852020-10-30 14:35:01 +0000150export class LighthouseReportUIFeatures extends self.ReportUIFeatures {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000151 /**
Connor Clark99508362019-08-20 19:52:23 +0000152 * @param {!DOM} dom
153 */
154 constructor(dom) {
155 super(dom);
156 this._beforePrint = null;
157 this._afterPrint = null;
158 }
159
160 /**
Tim van der Lippe4df32c92020-11-06 12:35:05 +0000161 * @override
162 * @param {?function():void} beforePrint
Connor Clark99508362019-08-20 19:52:23 +0000163 */
164 setBeforePrint(beforePrint) {
165 this._beforePrint = beforePrint;
166 }
167
168 /**
Tim van der Lippe4df32c92020-11-06 12:35:05 +0000169 * @override
170 * @param {?function():void} afterPrint
Connor Clark99508362019-08-20 19:52:23 +0000171 */
172 setAfterPrint(afterPrint) {
173 this._afterPrint = afterPrint;
174 }
175
176 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000177 * Returns the html that recreates this report.
178 * @return {string}
179 * @protected
180 */
181 getReportHtml() {
182 this.resetUIState();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000183 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000184 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
185 }
186
187 /**
188 * Downloads a file (blob) using the system dialog prompt.
189 * @param {!Blob|!File} blob The file to save.
190 */
191 async _saveFile(blob) {
Paul Lewisdaac1062020-03-05 14:37:10 +0000192 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000193 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200194 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000195 const ext = blob.type.match('json') ? '.json' : '.html';
196 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
197 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100198 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000199 }
200
201 async _print() {
202 const document = this.getDocument();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000203 const clonedReport = /** @type {!HTMLElement} */ (document.querySelector('.lh-root')).cloneNode(true /* deep */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000204 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000205 if (!printWindow) {
206 return;
207 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000208 const style = printWindow.document.createElement('style');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000209 style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css') || '';
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000210 printWindow.document.head.appendChild(style);
211 printWindow.document.body.replaceWith(clonedReport);
212 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000213 await LighthouseReportRenderer.linkifyNodeDetails(/** @type {!HTMLElement} */ (clonedReport));
Connor Clark99508362019-08-20 19:52:23 +0000214
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000215 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000216 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000217 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000218 printWindow.focus();
219 printWindow.print();
220 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000221 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000222 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000223 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000224 }
225
226 /**
227 * @suppress {visibility}
228 * @return {!Document}
229 */
230 getDocument() {
231 return this._document;
232 }
233
234 /**
235 * @suppress {visibility}
236 */
237 resetUIState() {
238 this._resetUIState();
239 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000240}