blob: fb90e4cecbcfb5744111673342258113930a1dea [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
Tim van der Lippe76961572021-04-06 11:48:07 +01005import * as Common from '../../core/common/common.js';
Tim van der Lippee0247312021-04-01 15:25:30 +01006import * as Host from '../../core/host/host.js';
Tim van der Lippebb352e62021-04-01 18:57:28 +01007import * as i18n from '../../core/i18n/i18n.js';
Tim van der Lippeaa1ed7a2021-03-31 15:38:27 +01008import * as Platform from '../../core/platform/platform.js';
Tim van der Lipped8caac42021-03-31 15:40:44 +01009import * as Root from '../../core/root/root.js';
Tim van der Lippee00b92f2021-03-31 17:52:17 +010010import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippe99aeaf32021-04-09 11:33:34 +010011import * as Workspace from '../../models/workspace/workspace.js';
Connor Clark4a7d8342021-07-19 13:38:37 -070012import * as LighthouseReport from '../../third_party/lighthouse/report/report.js';
Tim van der Lippe339ad262021-04-21 13:23:36 +010013import * as Components from '../../ui/legacy/components/utils/utils.js';
Tim van der Lippeaa61faf2021-04-07 16:32:07 +010014import * as UI from '../../ui/legacy/legacy.js';
Tim van der Lippefddcf402021-04-19 14:00:29 +010015import * as ThemeSupport from '../../ui/legacy/theme_support/theme_support.js';
Tim van der Lippe01e1c462021-04-19 16:04:03 +010016import * as Timeline from '../timeline/timeline.js';
Connor Clark4a7d8342021-07-19 13:38:37 -070017import type {RunnerResultArtifacts, NodeDetailsJSON, SourceLocationDetailsJSON} from './LighthouseReporterTypes.js';
Tim van der Lippe1e10f852020-10-30 14:35:01 +000018
Simon Zünd6f95e842021-03-01 08:41:55 +010019const UIStrings = {
vidorteg06840022020-11-20 21:18:03 -080020 /**
21 *@description Label for view trace button when simulated throttling is enabled
22 */
23 viewOriginalTrace: 'View Original Trace',
24 /**
25 *@description Text of the timeline button in Lighthouse Report Renderer
26 */
27 viewTrace: 'View Trace',
28 /**
29 *@description Help text for 'View Trace' button
30 */
31 thePerformanceMetricsAboveAre:
32 '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.',
33};
Tim van der Lippe7a077eb2021-03-23 18:02:11 +000034const str_ = i18n.i18n.registerUIStrings('panels/lighthouse/LighthouseReportRenderer.ts', UIStrings);
vidorteg06840022020-11-20 21:18:03 -080035const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Paul Lewiscf2ef222019-11-22 14:55:35 +000036const MaxLengthForLinks = 40;
37
Connor Clark4a7d8342021-07-19 13:38:37 -070038export class LighthouseReportRenderer extends LighthouseReport.ReportRenderer {
39 constructor(dom: LighthouseReport.DOM) {
Tim van der Lippe4df32c92020-11-06 12:35:05 +000040 super(dom);
41 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +010042
Connor Clark4cef9322021-05-18 02:04:00 -070043 static addViewTraceButton(
Connor Clark4a7d8342021-07-19 13:38:37 -070044 el: Element, reportUIFeatures: LighthouseReport.ReportUIFeatures, artifacts?: RunnerResultArtifacts): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000045 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000046 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000047 }
Patrick Hulcea087f622018-05-18 00:37:53 +000048
Adam Rainef46d1582020-08-17 20:24:32 +000049 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000050 const container = el.querySelector('.lh-audit-group');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000051 if (!container) {
52 return;
53 }
Paul Irishbf204682020-05-13 16:11:37 -070054 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
55 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
56 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000057 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000058 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000059
Paul Irish8f1e33d2018-05-31 02:29:50 +000060 const defaultPassTrace = artifacts.traces.defaultPass;
Connor Clark4cef9322021-05-18 02:04:00 -070061 const text = simulated ? i18nString(UIStrings.viewOriginalTrace) : i18nString(UIStrings.viewTrace);
62 const timelineButton = reportUIFeatures.addButton({
63 text,
64 onClick: onViewTraceClick,
65 });
Connor Clark4a7d8342021-07-19 13:38:37 -070066 if (timelineButton) {
67 timelineButton.classList.add('lh-button--trace');
68 if (simulated) {
69 UI.Tooltip.Tooltip.install(timelineButton, i18nString(UIStrings.thePerformanceMetricsAboveAre));
70 }
Adam Rainef46d1582020-08-17 20:24:32 +000071 }
Paul Irish8f1e33d2018-05-31 02:29:50 +000072
Jan Schefflerc5a400f2021-01-22 17:41:47 +010073 async function onViewTraceClick(): Promise<void> {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000074 Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010075 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000076 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000077 }
Patrick Hulcea087f622018-05-18 00:37:53 +000078 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000079
Jan Schefflerc5a400f2021-01-22 17:41:47 +010080 static async linkifyNodeDetails(el: Element): Promise<void> {
Sigurd Schneiderb9f6c792021-05-31 12:57:24 +020081 const mainTarget = SDK.TargetManager.TargetManager.instance().mainTarget();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000082 if (!mainTarget) {
83 return;
84 }
Paul Lewisdaac1062020-03-05 14:37:10 +000085 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000086 if (!domModel) {
87 return;
88 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000089
90 for (const origElement of el.getElementsByClassName('lh-node')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +010091 const origHTMLElement = origElement as HTMLElement;
Connor Clark4a7d8342021-07-19 13:38:37 -070092 const detailsItem = origHTMLElement.dataset as unknown as NodeDetailsJSON;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000093 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000094 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000095 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000096
97 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
98
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000099 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +0000100 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000101 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000102 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000103 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +0000104 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000105 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000106
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000107 const element = await Common.Linkifier.Linkifier.linkify(
108 node, {tooltip: detailsItem.snippet, preventKeyboardFocus: undefined});
Tim van der Lippe70842f32020-11-23 16:56:57 +0000109 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Connor Clark49872c02020-12-16 13:39:28 -0600110
111 const screenshotElement = origHTMLElement.querySelector('.lh-element-screenshot');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000112 origHTMLElement.textContent = '';
Connor Clark49872c02020-12-16 13:39:28 -0600113 if (screenshotElement) {
114 origHTMLElement.append(screenshotElement);
115 }
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000116 origHTMLElement.appendChild(element);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000117 }
118 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000119
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100120 static async linkifySourceLocationDetails(el: Element): Promise<void> {
Connor Clark0403a422019-11-18 18:03:18 -0800121 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100122 const origHTMLElement = origElement as HTMLElement;
Connor Clark4a7d8342021-07-19 13:38:37 -0700123 const detailsItem = origHTMLElement.dataset as SourceLocationDetailsJSON;
Connor Clark0403a422019-11-18 18:03:18 -0800124 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
125 continue;
126 }
127 const url = detailsItem.sourceUrl;
128 const line = Number(detailsItem.sourceLine);
129 const column = Number(detailsItem.sourceColumn);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000130 const element = await Components.Linkifier.Linkifier.linkifyURL(url, {
131 lineNumber: line,
132 columnNumber: column,
Philip Pfaffe068b01d2021-03-22 10:46:26 +0100133 inlineFrameIndex: 0,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000134 maxLength: MaxLengthForLinks,
135 bypassURLTrimming: undefined,
136 className: undefined,
137 preventClick: undefined,
138 tabStop: undefined,
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100139 text: undefined,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000140 });
Tim van der Lippe70842f32020-11-23 16:56:57 +0000141 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000142 origHTMLElement.textContent = '';
143 origHTMLElement.appendChild(element);
Connor Clark0403a422019-11-18 18:03:18 -0800144 }
145 }
146
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100147 static handleDarkMode(el: Element): void {
Paul Lewisca569a52020-09-09 17:11:51 +0100148 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000149 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000150 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000151 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000152}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000153
Connor Clark4a7d8342021-07-19 13:38:37 -0700154// @ts-expect-error https://github.com/GoogleChrome/lighthouse/issues/11628
155export class LighthouseReportUIFeatures extends LighthouseReport.ReportUIFeatures {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200156 private beforePrint: (() => void)|null;
157 private afterPrint: (() => void)|null;
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100158
Connor Clark4a7d8342021-07-19 13:38:37 -0700159 constructor(dom: LighthouseReport.DOM) {
Connor Clark99508362019-08-20 19:52:23 +0000160 super(dom);
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200161 this.beforePrint = null;
162 this.afterPrint = null;
Connor Clark99508362019-08-20 19:52:23 +0000163 }
164
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100165 setBeforePrint(beforePrint: (() => void)|null): void {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200166 this.beforePrint = beforePrint;
Connor Clark99508362019-08-20 19:52:23 +0000167 }
168
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100169 setAfterPrint(afterPrint: (() => void)|null): void {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200170 this.afterPrint = afterPrint;
Connor Clark99508362019-08-20 19:52:23 +0000171 }
172
173 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000174 * Returns the html that recreates this report.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000175 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100176 getReportHtml(): string {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000177 this.resetUIState();
Connor Clark4a7d8342021-07-19 13:38:37 -0700178 // @ts-expect-error https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000179 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
180 }
181
182 /**
183 * Downloads a file (blob) using the system dialog prompt.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000184 */
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200185 // This implements the interface ReportUIFeatures from lighthouse
186 // which follows a different naming convention.
187 // eslint-disable-next-line rulesdir/no_underscored_properties, @typescript-eslint/naming-convention
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100188 async _saveFile(blob: Blob|File): Promise<void> {
Paul Lewisdaac1062020-03-05 14:37:10 +0000189 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000190 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200191 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000192 const ext = blob.type.match('json') ? '.json' : '.html';
193 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
194 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100195 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000196 }
197
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200198 // This implements the interface ReportUIFeatures from lighthouse
199 // which follows a different naming convention.
200 // eslint-disable-next-line rulesdir/no_underscored_properties, @typescript-eslint/naming-convention
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100201 async _print(): Promise<void> {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000202 const document = this.getDocument();
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100203 const clonedReport = (document.querySelector('.lh-root') as HTMLElement).cloneNode(true);
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`.
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100213 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport as HTMLElement);
Connor Clark99508362019-08-20 19:52:23 +0000214
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200215 if (this.beforePrint) {
216 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();
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200221 if (this.afterPrint) {
222 this.afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000223 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000224 }
225
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100226 getDocument(): Document {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000227 return this._document;
228 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100229 resetUIState(): void {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000230 this._resetUIState();
231 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000232}