blob: 82ac3796620653139f14b8b26de88aa95c2796ce [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 Lippe76961572021-04-06 11:48:07 +01007import * as Common from '../../core/common/common.js';
Tim van der Lippee0247312021-04-01 15:25:30 +01008import * as Host from '../../core/host/host.js';
Tim van der Lippebb352e62021-04-01 18:57:28 +01009import * as i18n from '../../core/i18n/i18n.js';
Tim van der Lippeaa1ed7a2021-03-31 15:38:27 +010010import * as Platform from '../../core/platform/platform.js';
Tim van der Lipped8caac42021-03-31 15:40:44 +010011import * as Root from '../../core/root/root.js';
Tim van der Lippee00b92f2021-03-31 17:52:17 +010012import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippe99aeaf32021-04-09 11:33:34 +010013import * as Workspace from '../../models/workspace/workspace.js';
Tim van der Lippe339ad262021-04-21 13:23:36 +010014import * as Components from '../../ui/legacy/components/utils/utils.js';
Tim van der Lippeaa61faf2021-04-07 16:32:07 +010015import * as UI from '../../ui/legacy/legacy.js';
Tim van der Lippefddcf402021-04-19 14:00:29 +010016import * as ThemeSupport from '../../ui/legacy/theme_support/theme_support.js';
Tim van der Lippe01e1c462021-04-19 16:04:03 +010017import * as Timeline from '../timeline/timeline.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
Connor Clark4cef9322021-05-18 02:04:00 -070046 static addViewTraceButton(
47 el: Element, reportUIFeatures: ReportRenderer.ReportUIFeatures,
48 artifacts?: ReportRenderer.RunnerResultArtifacts): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000049 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000050 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000051 }
Patrick Hulcea087f622018-05-18 00:37:53 +000052
Adam Rainef46d1582020-08-17 20:24:32 +000053 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000054 const container = el.querySelector('.lh-audit-group');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000055 if (!container) {
56 return;
57 }
Paul Irishbf204682020-05-13 16:11:37 -070058 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
59 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
60 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000061 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000062 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000063
Paul Irish8f1e33d2018-05-31 02:29:50 +000064 const defaultPassTrace = artifacts.traces.defaultPass;
Connor Clark4cef9322021-05-18 02:04:00 -070065 const text = simulated ? i18nString(UIStrings.viewOriginalTrace) : i18nString(UIStrings.viewTrace);
66 const timelineButton = reportUIFeatures.addButton({
67 text,
68 onClick: onViewTraceClick,
69 });
70 timelineButton.classList.add('lh-button--trace');
Adam Rainef46d1582020-08-17 20:24:32 +000071 if (simulated) {
Tim van der Lippe70842f32020-11-23 16:56:57 +000072 UI.Tooltip.Tooltip.install(timelineButton, i18nString(UIStrings.thePerformanceMetricsAboveAre));
Adam Rainef46d1582020-08-17 20:24:32 +000073 }
Paul Irishbf204682020-05-13 16:11:37 -070074 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000075
Jan Schefflerc5a400f2021-01-22 17:41:47 +010076 async function onViewTraceClick(): Promise<void> {
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000077 Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010078 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000079 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000080 }
Patrick Hulcea087f622018-05-18 00:37:53 +000081 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000082
Jan Schefflerc5a400f2021-01-22 17:41:47 +010083 static async linkifyNodeDetails(el: Element): Promise<void> {
Sigurd Schneiderb9f6c792021-05-31 12:57:24 +020084 const mainTarget = SDK.TargetManager.TargetManager.instance().mainTarget();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000085 if (!mainTarget) {
86 return;
87 }
Paul Lewisdaac1062020-03-05 14:37:10 +000088 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +000089 if (!domModel) {
90 return;
91 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000092
93 for (const origElement of el.getElementsByClassName('lh-node')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +010094 const origHTMLElement = origElement as HTMLElement;
95 const detailsItem = origHTMLElement.dataset as unknown as ReportRenderer.NodeDetailsJSON;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000096 if (!detailsItem.path) {
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
100 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
101
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000102 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +0000103 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000104 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000105 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000106 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +0000107 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000108 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000109
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000110 const element = await Common.Linkifier.Linkifier.linkify(
111 node, {tooltip: detailsItem.snippet, preventKeyboardFocus: undefined});
Tim van der Lippe70842f32020-11-23 16:56:57 +0000112 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Connor Clark49872c02020-12-16 13:39:28 -0600113
114 const screenshotElement = origHTMLElement.querySelector('.lh-element-screenshot');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000115 origHTMLElement.textContent = '';
Connor Clark49872c02020-12-16 13:39:28 -0600116 if (screenshotElement) {
117 origHTMLElement.append(screenshotElement);
118 }
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000119 origHTMLElement.appendChild(element);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +0000120 }
121 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000122
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100123 static async linkifySourceLocationDetails(el: Element): Promise<void> {
Connor Clark0403a422019-11-18 18:03:18 -0800124 for (const origElement of el.getElementsByClassName('lh-source-location')) {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100125 const origHTMLElement = origElement as HTMLElement;
126 const detailsItem = origHTMLElement.dataset as ReportRenderer.SourceLocationDetailsJSON;
Connor Clark0403a422019-11-18 18:03:18 -0800127 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
128 continue;
129 }
130 const url = detailsItem.sourceUrl;
131 const line = Number(detailsItem.sourceLine);
132 const column = Number(detailsItem.sourceColumn);
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000133 const element = await Components.Linkifier.Linkifier.linkifyURL(url, {
134 lineNumber: line,
135 columnNumber: column,
Philip Pfaffe068b01d2021-03-22 10:46:26 +0100136 inlineFrameIndex: 0,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000137 maxLength: MaxLengthForLinks,
138 bypassURLTrimming: undefined,
139 className: undefined,
140 preventClick: undefined,
141 tabStop: undefined,
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100142 text: undefined,
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000143 });
Tim van der Lippe70842f32020-11-23 16:56:57 +0000144 UI.Tooltip.Tooltip.install(origHTMLElement, '');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000145 origHTMLElement.textContent = '';
146 origHTMLElement.appendChild(element);
Connor Clark0403a422019-11-18 18:03:18 -0800147 }
148 }
149
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100150 static handleDarkMode(el: Element): void {
Paul Lewisca569a52020-09-09 17:11:51 +0100151 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000152 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000153 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000154 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000155}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000156
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000157// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Tim van der Lippe1e10f852020-10-30 14:35:01 +0000158export class LighthouseReportUIFeatures extends self.ReportUIFeatures {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100159 _beforePrint: (() => void)|null;
160 _afterPrint: (() => void)|null;
161
162 constructor(dom: DOM) {
Connor Clark99508362019-08-20 19:52:23 +0000163 super(dom);
164 this._beforePrint = null;
165 this._afterPrint = null;
166 }
167
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100168 setBeforePrint(beforePrint: (() => void)|null): void {
Connor Clark99508362019-08-20 19:52:23 +0000169 this._beforePrint = beforePrint;
170 }
171
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100172 setAfterPrint(afterPrint: (() => void)|null): void {
Connor Clark99508362019-08-20 19:52:23 +0000173 this._afterPrint = afterPrint;
174 }
175
176 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000177 * Returns the html that recreates this report.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000178 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100179 getReportHtml(): string {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000180 this.resetUIState();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000181 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000182 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
183 }
184
185 /**
186 * Downloads a file (blob) using the system dialog prompt.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000187 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100188 async _saveFile(blob: Blob|File): Promise<void> {
189 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Paul Lewisdaac1062020-03-05 14:37:10 +0000190 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000191 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100192 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Simon Zünd2c704cd2020-06-04 11:08:35 +0200193 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000194 const ext = blob.type.match('json') ? '.json' : '.html';
195 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
196 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100197 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000198 }
199
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100200 async _print(): Promise<void> {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000201 const document = this.getDocument();
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100202 const clonedReport = (document.querySelector('.lh-root') as HTMLElement).cloneNode(true);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000203 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000204 if (!printWindow) {
205 return;
206 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000207 const style = printWindow.document.createElement('style');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000208 style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css') || '';
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000209 printWindow.document.head.appendChild(style);
210 printWindow.document.body.replaceWith(clonedReport);
211 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100212 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport as HTMLElement);
Connor Clark99508362019-08-20 19:52:23 +0000213
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000214 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000215 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000216 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000217 printWindow.focus();
218 printWindow.print();
219 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000220 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000221 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000222 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000223 }
224
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100225 getDocument(): Document {
226 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000227 return this._document;
228 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100229 resetUIState(): void {
230 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000231 this._resetUIState();
232 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000233}