blob: ea6562f953786b6ecd0077d12d6de767931eecf8 [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 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000054
Paul Irish8f1e33d2018-05-31 02:29:50 +000055 const defaultPassTrace = artifacts.traces.defaultPass;
Connor Clark4cef9322021-05-18 02:04:00 -070056 const text = simulated ? i18nString(UIStrings.viewOriginalTrace) : i18nString(UIStrings.viewTrace);
57 const timelineButton = reportUIFeatures.addButton({
58 text,
59 onClick: onViewTraceClick,
60 });
Connor Clark4a7d8342021-07-19 13:38:37 -070061 if (timelineButton) {
62 timelineButton.classList.add('lh-button--trace');
63 if (simulated) {
64 UI.Tooltip.Tooltip.install(timelineButton, i18nString(UIStrings.thePerformanceMetricsAboveAre));
65 }
Adam Rainef46d1582020-08-17 20:24:32 +000066 }
Paul Irish8f1e33d2018-05-31 02:29:50 +000067
Jan Schefflerc5a400f2021-01-22 17:41:47 +010068 async function onViewTraceClick(): Promise<void> {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000069 Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010070 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000071 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000072 }
Patrick Hulcea087f622018-05-18 00:37:53 +000073 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000074
Jan Schefflerc5a400f2021-01-22 17:41:47 +010075 static async linkifyNodeDetails(el: Element): Promise<void> {
Sigurd Schneiderb9f6c792021-05-31 12:57:24 +020076 const mainTarget = SDK.TargetManager.TargetManager.instance().mainTarget();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000077 if (!mainTarget) {
78 return;
79 }
Paul Lewisdaac1062020-03-05 14:37:10 +000080 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000081 if (!domModel) {
82 return;
83 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000084
85 for (const origElement of el.getElementsByClassName('lh-node')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +010086 const origHTMLElement = origElement as HTMLElement;
Connor Clark4a7d8342021-07-19 13:38:37 -070087 const detailsItem = origHTMLElement.dataset as unknown as NodeDetailsJSON;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000088 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000089 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000090 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000091
92 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
93
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000094 if (!nodeId) {
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 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000098 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000099 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000100 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000101
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000102 const element = await Common.Linkifier.Linkifier.linkify(
103 node, {tooltip: detailsItem.snippet, preventKeyboardFocus: undefined});
Tim van der Lippe70842f32020-11-23 16:56:57 +0000104 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Connor Clark49872c02020-12-16 13:39:28 -0600105
106 const screenshotElement = origHTMLElement.querySelector('.lh-element-screenshot');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000107 origHTMLElement.textContent = '';
Connor Clark49872c02020-12-16 13:39:28 -0600108 if (screenshotElement) {
109 origHTMLElement.append(screenshotElement);
110 }
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000111 origHTMLElement.appendChild(element);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000112 }
113 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000114
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100115 static async linkifySourceLocationDetails(el: Element): Promise<void> {
Connor Clark0403a422019-11-18 18:03:18 -0800116 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100117 const origHTMLElement = origElement as HTMLElement;
Connor Clark4a7d8342021-07-19 13:38:37 -0700118 const detailsItem = origHTMLElement.dataset as SourceLocationDetailsJSON;
Connor Clark0403a422019-11-18 18:03:18 -0800119 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
120 continue;
121 }
122 const url = detailsItem.sourceUrl;
123 const line = Number(detailsItem.sourceLine);
124 const column = Number(detailsItem.sourceColumn);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000125 const element = await Components.Linkifier.Linkifier.linkifyURL(url, {
126 lineNumber: line,
127 columnNumber: column,
Brandon Walderman666b40a2021-10-19 13:29:05 -0700128 showColumnNumber: false,
Philip Pfaffe068b01d2021-03-22 10:46:26 +0100129 inlineFrameIndex: 0,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000130 maxLength: MaxLengthForLinks,
131 bypassURLTrimming: undefined,
132 className: undefined,
133 preventClick: undefined,
134 tabStop: undefined,
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100135 text: undefined,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000136 });
Tim van der Lippe70842f32020-11-23 16:56:57 +0000137 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000138 origHTMLElement.textContent = '';
139 origHTMLElement.appendChild(element);
Connor Clark0403a422019-11-18 18:03:18 -0800140 }
141 }
142
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100143 static handleDarkMode(el: Element): void {
Paul Lewisca569a52020-09-09 17:11:51 +0100144 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
Connor Clark649a9792021-09-08 12:31:28 -0700145 el.classList.add('lh-dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000146 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000147 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000148}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000149
Adam Raineda506bc2021-08-20 11:54:17 -0400150// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Connor Clark4a7d8342021-07-19 13:38:37 -0700151export class LighthouseReportUIFeatures extends LighthouseReport.ReportUIFeatures {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200152 private beforePrint: (() => void)|null;
153 private afterPrint: (() => void)|null;
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100154
Connor Clark4a7d8342021-07-19 13:38:37 -0700155 constructor(dom: LighthouseReport.DOM) {
Connor Clark99508362019-08-20 19:52:23 +0000156 super(dom);
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200157 this.beforePrint = null;
158 this.afterPrint = null;
Connor Clarkdb4c9682021-09-24 12:51:09 -0700159 this._topbar._print = this._print.bind(this);
Connor Clark99508362019-08-20 19:52:23 +0000160 }
161
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100162 setBeforePrint(beforePrint: (() => void)|null): void {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200163 this.beforePrint = beforePrint;
Connor Clark99508362019-08-20 19:52:23 +0000164 }
165
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100166 setAfterPrint(afterPrint: (() => void)|null): void {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200167 this.afterPrint = afterPrint;
Connor Clark99508362019-08-20 19:52:23 +0000168 }
169
170 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000171 * Returns the html that recreates this report.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000172 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100173 getReportHtml(): string {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000174 this.resetUIState();
Connor Clark4a7d8342021-07-19 13:38:37 -0700175 // @ts-expect-error https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000176 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
177 }
178
179 /**
180 * Downloads a file (blob) using the system dialog prompt.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000181 */
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200182 // This implements the interface ReportUIFeatures from lighthouse
183 // which follows a different naming convention.
184 // eslint-disable-next-line rulesdir/no_underscored_properties, @typescript-eslint/naming-convention
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100185 async _saveFile(blob: Blob|File): Promise<void> {
Paul Lewisdaac1062020-03-05 14:37:10 +0000186 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000187 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200188 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000189 const ext = blob.type.match('json') ? '.json' : '.html';
190 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
191 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100192 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000193 }
194
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200195 // This implements the interface ReportUIFeatures from lighthouse
196 // which follows a different naming convention.
197 // eslint-disable-next-line rulesdir/no_underscored_properties, @typescript-eslint/naming-convention
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100198 async _print(): Promise<void> {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000199 const document = this.getDocument();
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100200 const clonedReport = (document.querySelector('.lh-root') as HTMLElement).cloneNode(true);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000201 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000202 if (!printWindow) {
203 return;
204 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000205 const style = printWindow.document.createElement('style');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000206 style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css') || '';
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000207 printWindow.document.head.appendChild(style);
208 printWindow.document.body.replaceWith(clonedReport);
209 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100210 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport as HTMLElement);
Connor Clark99508362019-08-20 19:52:23 +0000211
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200212 if (this.beforePrint) {
213 this.beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000214 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000215 printWindow.focus();
216 printWindow.print();
217 printWindow.close();
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200218 if (this.afterPrint) {
219 this.afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000220 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000221 }
222
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100223 getDocument(): Document {
Connor Clark782933d2021-11-08 16:16:34 -0800224 return this._dom.document();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000225 }
Connor Clark782933d2021-11-08 16:16:34 -0800226
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100227 resetUIState(): void {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000228 this._resetUIState();
229 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000230}