blob: 1211811ef76f95fff72955bea76b7539395940a9 [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 Lippee00b92f2021-03-31 17:52:17 +01009import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippe99aeaf32021-04-09 11:33:34 +010010import * as Workspace from '../../models/workspace/workspace.js';
Connor Clark4a7d8342021-07-19 13:38:37 -070011import * as LighthouseReport from '../../third_party/lighthouse/report/report.js';
Tim van der Lippe339ad262021-04-21 13:23:36 +010012import * as Components from '../../ui/legacy/components/utils/utils.js';
Tim van der Lippeaa61faf2021-04-07 16:32:07 +010013import * as UI from '../../ui/legacy/legacy.js';
Tim van der Lippefddcf402021-04-19 14:00:29 +010014import * as ThemeSupport from '../../ui/legacy/theme_support/theme_support.js';
Tim van der Lippe01e1c462021-04-19 16:04:03 +010015import * as Timeline from '../timeline/timeline.js';
Jack Franklin3a802602022-07-13 08:39:42 +000016import {
17 type RunnerResultArtifacts,
18 type NodeDetailsJSON,
19 type SourceLocationDetailsJSON,
20} from './LighthouseReporterTypes.js';
Tim van der Lippe1e10f852020-10-30 14:35:01 +000021
Simon Zünd6f95e842021-03-01 08:41:55 +010022const UIStrings = {
vidorteg06840022020-11-20 21:18:03 -080023 /**
24 *@description Label for view trace button when simulated throttling is enabled
25 */
26 viewOriginalTrace: 'View Original Trace',
27 /**
28 *@description Text of the timeline button in Lighthouse Report Renderer
29 */
30 viewTrace: 'View Trace',
31 /**
32 *@description Help text for 'View Trace' button
33 */
34 thePerformanceMetricsAboveAre:
35 '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.',
36};
Tim van der Lippe7a077eb2021-03-23 18:02:11 +000037const str_ = i18n.i18n.registerUIStrings('panels/lighthouse/LighthouseReportRenderer.ts', UIStrings);
vidorteg06840022020-11-20 21:18:03 -080038const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Paul Lewiscf2ef222019-11-22 14:55:35 +000039const MaxLengthForLinks = 40;
40
Connor Clark4a7d8342021-07-19 13:38:37 -070041export class LighthouseReportRenderer extends LighthouseReport.ReportRenderer {
42 constructor(dom: LighthouseReport.DOM) {
Tim van der Lippe4df32c92020-11-06 12:35:05 +000043 super(dom);
44 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +010045
Connor Clark4cef9322021-05-18 02:04:00 -070046 static addViewTraceButton(
Connor Clark4a7d8342021-07-19 13:38:37 -070047 el: Element, reportUIFeatures: LighthouseReport.ReportUIFeatures, artifacts?: RunnerResultArtifacts): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000048 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000049 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000050 }
Patrick Hulcea087f622018-05-18 00:37:53 +000051
Adam Rainef46d1582020-08-17 20:24:32 +000052 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000053 const container = el.querySelector('.lh-audit-group');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000054 if (!container) {
55 return;
56 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000057
Paul Irish8f1e33d2018-05-31 02:29:50 +000058 const defaultPassTrace = artifacts.traces.defaultPass;
Connor Clark4cef9322021-05-18 02:04:00 -070059 const text = simulated ? i18nString(UIStrings.viewOriginalTrace) : i18nString(UIStrings.viewTrace);
60 const timelineButton = reportUIFeatures.addButton({
61 text,
62 onClick: onViewTraceClick,
63 });
Connor Clark4a7d8342021-07-19 13:38:37 -070064 if (timelineButton) {
65 timelineButton.classList.add('lh-button--trace');
66 if (simulated) {
67 UI.Tooltip.Tooltip.install(timelineButton, i18nString(UIStrings.thePerformanceMetricsAboveAre));
68 }
Adam Rainef46d1582020-08-17 20:24:32 +000069 }
Paul Irish8f1e33d2018-05-31 02:29:50 +000070
Jan Schefflerc5a400f2021-01-22 17:41:47 +010071 async function onViewTraceClick(): Promise<void> {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000072 Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010073 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000074 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000075 }
Patrick Hulcea087f622018-05-18 00:37:53 +000076 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000077
Jan Schefflerc5a400f2021-01-22 17:41:47 +010078 static async linkifyNodeDetails(el: Element): Promise<void> {
Danil Somsikove1ee1b22022-11-07 18:44:57 +010079 const mainTarget = SDK.TargetManager.TargetManager.instance().mainFrameTarget();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000080 if (!mainTarget) {
81 return;
82 }
Paul Lewisdaac1062020-03-05 14:37:10 +000083 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000084 if (!domModel) {
85 return;
86 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000087
88 for (const origElement of el.getElementsByClassName('lh-node')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +010089 const origHTMLElement = origElement as HTMLElement;
Connor Clark4a7d8342021-07-19 13:38:37 -070090 const detailsItem = origHTMLElement.dataset as unknown as NodeDetailsJSON;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000091 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000092 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000093 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000094
95 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
96
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000097 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000098 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000099 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000100 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000101 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +0000102 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000103 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000104
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000105 const element = await Common.Linkifier.Linkifier.linkify(
106 node, {tooltip: detailsItem.snippet, preventKeyboardFocus: undefined});
Tim van der Lippe70842f32020-11-23 16:56:57 +0000107 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Connor Clark49872c02020-12-16 13:39:28 -0600108
109 const screenshotElement = origHTMLElement.querySelector('.lh-element-screenshot');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000110 origHTMLElement.textContent = '';
Connor Clark49872c02020-12-16 13:39:28 -0600111 if (screenshotElement) {
112 origHTMLElement.append(screenshotElement);
113 }
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000114 origHTMLElement.appendChild(element);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000115 }
116 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000117
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100118 static async linkifySourceLocationDetails(el: Element): Promise<void> {
Connor Clark0403a422019-11-18 18:03:18 -0800119 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100120 const origHTMLElement = origElement as HTMLElement;
Connor Clark4a7d8342021-07-19 13:38:37 -0700121 const detailsItem = origHTMLElement.dataset as SourceLocationDetailsJSON;
Connor Clark0403a422019-11-18 18:03:18 -0800122 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
123 continue;
124 }
125 const url = detailsItem.sourceUrl;
126 const line = Number(detailsItem.sourceLine);
127 const column = Number(detailsItem.sourceColumn);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000128 const element = await Components.Linkifier.Linkifier.linkifyURL(url, {
129 lineNumber: line,
130 columnNumber: column,
Brandon Walderman666b40a2021-10-19 13:29:05 -0700131 showColumnNumber: false,
Philip Pfaffe068b01d2021-03-22 10:46:26 +0100132 inlineFrameIndex: 0,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000133 maxLength: MaxLengthForLinks,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000134 });
Tim van der Lippe70842f32020-11-23 16:56:57 +0000135 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000136 origHTMLElement.textContent = '';
137 origHTMLElement.appendChild(element);
Connor Clark0403a422019-11-18 18:03:18 -0800138 }
139 }
140
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100141 static handleDarkMode(el: Element): void {
Tim van der Lippe4437a7a2022-02-07 16:41:50 +0000142 const updateDarkModeIfNecessary = (): void => {
143 el.classList.toggle('lh-dark', ThemeSupport.ThemeSupport.instance().themeName() === 'dark');
144 };
145 ThemeSupport.ThemeSupport.instance().addEventListener(
146 ThemeSupport.ThemeChangeEvent.eventName, updateDarkModeIfNecessary);
147 updateDarkModeIfNecessary();
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000148 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000149}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000150
Adam Raineda506bc2021-08-20 11:54:17 -0400151// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Connor Clark4a7d8342021-07-19 13:38:37 -0700152export class LighthouseReportUIFeatures extends LighthouseReport.ReportUIFeatures {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200153 private beforePrint: (() => void)|null;
154 private afterPrint: (() => void)|null;
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100155
Connor Clark76abfb32022-11-03 15:50:41 -0700156 constructor(dom: LighthouseReport.DOM, opts: {}) {
157 super(dom, opts);
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200158 this.beforePrint = null;
159 this.afterPrint = null;
Connor Clarkdb4c9682021-09-24 12:51:09 -0700160 this._topbar._print = this._print.bind(this);
Connor Clark99508362019-08-20 19:52:23 +0000161 }
162
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100163 setBeforePrint(beforePrint: (() => void)|null): void {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200164 this.beforePrint = beforePrint;
Connor Clark99508362019-08-20 19:52:23 +0000165 }
166
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100167 setAfterPrint(afterPrint: (() => void)|null): void {
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200168 this.afterPrint = afterPrint;
Connor Clark99508362019-08-20 19:52:23 +0000169 }
170
171 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000172 * Returns the html that recreates this report.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000173 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100174 getReportHtml(): string {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000175 this.resetUIState();
Connor Clark4a7d8342021-07-19 13:38:37 -0700176 // @ts-expect-error https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000177 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
178 }
179
180 /**
181 * Downloads a file (blob) using the system dialog prompt.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000182 */
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200183 // This implements the interface ReportUIFeatures from lighthouse
184 // which follows a different naming convention.
185 // eslint-disable-next-line rulesdir/no_underscored_properties, @typescript-eslint/naming-convention
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100186 async _saveFile(blob: Blob|File): Promise<void> {
Paul Lewisdaac1062020-03-05 14:37:10 +0000187 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000188 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200189 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000190 const ext = blob.type.match('json') ? '.json' : '.html';
Kateryna Prokopenko380fdfa2022-03-16 16:39:32 +0000191 const basename = `${sanitizedDomain}-${timestamp}${ext}` as Platform.DevToolsPath.RawPathString;
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000192 const text = await blob.text();
Tim van der Lippe2d9a95c2022-01-04 16:18:03 +0100193 void Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000194 }
195
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200196 // This implements the interface ReportUIFeatures from lighthouse
197 // which follows a different naming convention.
198 // eslint-disable-next-line rulesdir/no_underscored_properties, @typescript-eslint/naming-convention
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100199 async _print(): Promise<void> {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000200 const document = this.getDocument();
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100201 const clonedReport = (document.querySelector('.lh-root') as HTMLElement).cloneNode(true);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000202 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000203 if (!printWindow) {
204 return;
205 }
Connor Clark93a44542021-11-16 10:24:51 -0800206
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000207 printWindow.document.body.replaceWith(clonedReport);
208 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100209 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport as HTMLElement);
Connor Clark99508362019-08-20 19:52:23 +0000210
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200211 if (this.beforePrint) {
212 this.beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000213 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000214 printWindow.focus();
215 printWindow.print();
216 printWindow.close();
Jan Scheffler9d4136d2021-08-10 12:55:04 +0200217 if (this.afterPrint) {
218 this.afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000219 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000220 }
221
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100222 getDocument(): Document {
Connor Clark782933d2021-11-08 16:16:34 -0800223 return this._dom.document();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000224 }
Connor Clark782933d2021-11-08 16:16:34 -0800225
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100226 resetUIState(): void {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000227 this._resetUIState();
228 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000229}