blob: 700acb6edfa3295702b61d754246f7f9560db63c [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
Jan Schefflerc5a400f2021-01-22 17:41:47 +01005/* eslint-disable rulesdir/no_underscored_properties */
6
Tim van der Lippe7a077eb2021-03-23 18:02:11 +00007import * as Components from '../../components/components.js';
Tim van der Lippe76961572021-04-06 11:48:07 +01008import * as Common from '../../core/common/common.js';
Tim van der Lippee0247312021-04-01 15:25:30 +01009import * as Host from '../../core/host/host.js';
Tim van der Lippebb352e62021-04-01 18:57:28 +010010import * as i18n from '../../core/i18n/i18n.js';
Tim van der Lippeaa1ed7a2021-03-31 15:38:27 +010011import * as Platform from '../../core/platform/platform.js';
Tim van der Lipped8caac42021-03-31 15:40:44 +010012import * as Root from '../../core/root/root.js';
Tim van der Lippee00b92f2021-03-31 17:52:17 +010013import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippe99aeaf32021-04-09 11:33:34 +010014import * as Workspace from '../../models/workspace/workspace.js';
Tim van der Lippe7a077eb2021-03-23 18:02:11 +000015import * as ThemeSupport from '../../theme_support/theme_support.js';
16import * as Timeline from '../../timeline/timeline.js';
Tim van der Lippeaa61faf2021-04-07 16:32:07 +010017import * as UI from '../../ui/legacy/legacy.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000018
Tim van der Lippe5f62c6f2021-02-25 16:39:26 +000019import type * as ReportRenderer from './LighthouseReporterTypes.js';
Tim van der Lippe1e10f852020-10-30 14:35:01 +000020
Simon Zünd6f95e842021-03-01 08:41:55 +010021const UIStrings = {
vidorteg06840022020-11-20 21:18:03 -080022 /**
23 *@description Label for view trace button when simulated throttling is enabled
24 */
25 viewOriginalTrace: 'View Original Trace',
26 /**
27 *@description Text of the timeline button in Lighthouse Report Renderer
28 */
29 viewTrace: 'View Trace',
30 /**
31 *@description Help text for 'View Trace' button
32 */
33 thePerformanceMetricsAboveAre:
34 '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.',
35};
Tim van der Lippe7a077eb2021-03-23 18:02:11 +000036const str_ = i18n.i18n.registerUIStrings('panels/lighthouse/LighthouseReportRenderer.ts', UIStrings);
vidorteg06840022020-11-20 21:18:03 -080037const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Paul Lewiscf2ef222019-11-22 14:55:35 +000038const MaxLengthForLinks = 40;
39
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000040// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Tim van der Lippe1e10f852020-10-30 14:35:01 +000041export class LighthouseReportRenderer extends self.ReportRenderer {
Jan Schefflerc5a400f2021-01-22 17:41:47 +010042 constructor(dom: DOM) {
Tim van der Lippe4df32c92020-11-06 12:35:05 +000043 super(dom);
44 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +010045
46 static addViewTraceButton(el: Element, artifacts?: ReportRenderer.RunnerResultArtifacts): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000047 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000048 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000049 }
Patrick Hulcea087f622018-05-18 00:37:53 +000050
Adam Rainef46d1582020-08-17 20:24:32 +000051 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000052 const container = el.querySelector('.lh-audit-group');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000053 if (!container) {
54 return;
55 }
Paul Irishbf204682020-05-13 16:11:37 -070056 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
57 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
58 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000059 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000060 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000061
Paul Irish8f1e33d2018-05-31 02:29:50 +000062 const defaultPassTrace = artifacts.traces.defaultPass;
vidorteg06840022020-11-20 21:18:03 -080063 const label = simulated ? i18nString(UIStrings.viewOriginalTrace) : i18nString(UIStrings.viewTrace);
Adam Rainef46d1582020-08-17 20:24:32 +000064 const timelineButton = UI.UIUtils.createTextButton(label, onViewTraceClick, 'view-trace');
65 if (simulated) {
Tim van der Lippe70842f32020-11-23 16:56:57 +000066 UI.Tooltip.Tooltip.install(timelineButton, i18nString(UIStrings.thePerformanceMetricsAboveAre));
Adam Rainef46d1582020-08-17 20:24:32 +000067 }
Paul Irishbf204682020-05-13 16:11:37 -070068 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000069
Jan Schefflerc5a400f2021-01-22 17:41:47 +010070 async function onViewTraceClick(): Promise<void> {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000071 Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010072 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000073 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000074 }
Patrick Hulcea087f622018-05-18 00:37:53 +000075 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000076
Jan Schefflerc5a400f2021-01-22 17:41:47 +010077 static async linkifyNodeDetails(el: Element): Promise<void> {
Paul Lewisdaac1062020-03-05 14:37:10 +000078 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000079 if (!mainTarget) {
80 return;
81 }
Paul Lewisdaac1062020-03-05 14:37:10 +000082 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000083 if (!domModel) {
84 return;
85 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000086
87 for (const origElement of el.getElementsByClassName('lh-node')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +010088 const origHTMLElement = origElement as HTMLElement;
89 const detailsItem = origHTMLElement.dataset as unknown as ReportRenderer.NodeDetailsJSON;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000090 if (!detailsItem.path) {
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
94 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
95
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000096 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000097 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000098 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000099 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000100 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +0000101 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000102 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000103
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000104 const element = await Common.Linkifier.Linkifier.linkify(
105 node, {tooltip: detailsItem.snippet, preventKeyboardFocus: undefined});
Tim van der Lippe70842f32020-11-23 16:56:57 +0000106 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Connor Clark49872c02020-12-16 13:39:28 -0600107
108 const screenshotElement = origHTMLElement.querySelector('.lh-element-screenshot');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000109 origHTMLElement.textContent = '';
Connor Clark49872c02020-12-16 13:39:28 -0600110 if (screenshotElement) {
111 origHTMLElement.append(screenshotElement);
112 }
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000113 origHTMLElement.appendChild(element);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000114 }
115 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000116
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100117 static async linkifySourceLocationDetails(el: Element): Promise<void> {
Connor Clark0403a422019-11-18 18:03:18 -0800118 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100119 const origHTMLElement = origElement as HTMLElement;
120 const detailsItem = origHTMLElement.dataset as ReportRenderer.SourceLocationDetailsJSON;
Connor Clark0403a422019-11-18 18:03:18 -0800121 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
122 continue;
123 }
124 const url = detailsItem.sourceUrl;
125 const line = Number(detailsItem.sourceLine);
126 const column = Number(detailsItem.sourceColumn);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000127 const element = await Components.Linkifier.Linkifier.linkifyURL(url, {
128 lineNumber: line,
129 columnNumber: column,
Philip Pfaffe068b01d2021-03-22 10:46:26 +0100130 inlineFrameIndex: 0,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000131 maxLength: MaxLengthForLinks,
132 bypassURLTrimming: undefined,
133 className: undefined,
134 preventClick: undefined,
135 tabStop: undefined,
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100136 text: undefined,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000137 });
Tim van der Lippe70842f32020-11-23 16:56:57 +0000138 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000139 origHTMLElement.textContent = '';
140 origHTMLElement.appendChild(element);
Connor Clark0403a422019-11-18 18:03:18 -0800141 }
142 }
143
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100144 static handleDarkMode(el: Element): void {
Paul Lewisca569a52020-09-09 17:11:51 +0100145 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000146 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000147 }
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
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000151// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Tim van der Lippe1e10f852020-10-30 14:35:01 +0000152export class LighthouseReportUIFeatures extends self.ReportUIFeatures {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100153 _beforePrint: (() => void)|null;
154 _afterPrint: (() => void)|null;
155
156 constructor(dom: DOM) {
Connor Clark99508362019-08-20 19:52:23 +0000157 super(dom);
158 this._beforePrint = null;
159 this._afterPrint = null;
160 }
161
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100162 setBeforePrint(beforePrint: (() => void)|null): void {
Connor Clark99508362019-08-20 19:52:23 +0000163 this._beforePrint = beforePrint;
164 }
165
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100166 setAfterPrint(afterPrint: (() => void)|null): void {
Connor Clark99508362019-08-20 19:52:23 +0000167 this._afterPrint = afterPrint;
168 }
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();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000175 // @ts-ignore 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 Schefflerc5a400f2021-01-22 17:41:47 +0100182 async _saveFile(blob: Blob|File): Promise<void> {
183 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Paul Lewisdaac1062020-03-05 14:37:10 +0000184 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000185 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100186 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Simon Zünd2c704cd2020-06-04 11:08:35 +0200187 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000188 const ext = blob.type.match('json') ? '.json' : '.html';
189 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
190 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100191 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000192 }
193
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100194 async _print(): Promise<void> {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000195 const document = this.getDocument();
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100196 const clonedReport = (document.querySelector('.lh-root') as HTMLElement).cloneNode(true);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000197 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000198 if (!printWindow) {
199 return;
200 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000201 const style = printWindow.document.createElement('style');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000202 style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css') || '';
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000203 printWindow.document.head.appendChild(style);
204 printWindow.document.body.replaceWith(clonedReport);
205 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100206 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport as HTMLElement);
Connor Clark99508362019-08-20 19:52:23 +0000207
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000208 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000209 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000210 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000211 printWindow.focus();
212 printWindow.print();
213 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000214 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000215 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000216 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000217 }
218
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100219 getDocument(): Document {
220 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000221 return this._document;
222 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100223 resetUIState(): void {
224 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000225 this._resetUIState();
226 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000227}