blob: 997d099cac706577d6e48ee0b4f4266f8a315aef [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
Paul Lewisdaac1062020-03-05 14:37:10 +00007import * as Common from '../common/common.js';
8import * as Components from '../components/components.js';
Tim van der Lippefe3cdc92020-11-06 15:23:13 +00009import * as Host from '../host/host.js';
vidorteg06840022020-11-20 21:18:03 -080010import * as i18n from '../i18n/i18n.js';
Simon Zünd2c704cd2020-06-04 11:08:35 +020011import * as Platform from '../platform/platform.js';
Tim van der Lippe5df64b22020-09-11 13:04:24 +010012import * as Root from '../root/root.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000013import * as SDK from '../sdk/sdk.js';
Paul Lewisca569a52020-09-09 17:11:51 +010014import * as ThemeSupport from '../theme_support/theme_support.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000015import * as Timeline from '../timeline/timeline.js';
16import * as UI from '../ui/ui.js';
Tim van der Lippe696c9262020-08-26 15:39:32 +010017import * as Workspace from '../workspace/workspace.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
vidorteg06840022020-11-20 21:18:03 -080021export const UIStrings = {
22 /**
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};
Jan Schefflerc5a400f2021-01-22 17:41:47 +010036const str_ = i18n.i18n.registerUIStrings('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,
130 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') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000145 el.classList.add('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
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000150// @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Tim van der Lippe1e10f852020-10-30 14:35:01 +0000151export class LighthouseReportUIFeatures extends self.ReportUIFeatures {
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100152 _beforePrint: (() => void)|null;
153 _afterPrint: (() => void)|null;
154
155 constructor(dom: DOM) {
Connor Clark99508362019-08-20 19:52:23 +0000156 super(dom);
157 this._beforePrint = null;
158 this._afterPrint = null;
159 }
160
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100161 setBeforePrint(beforePrint: (() => void)|null): void {
Connor Clark99508362019-08-20 19:52:23 +0000162 this._beforePrint = beforePrint;
163 }
164
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100165 setAfterPrint(afterPrint: (() => void)|null): void {
Connor Clark99508362019-08-20 19:52:23 +0000166 this._afterPrint = afterPrint;
167 }
168
169 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000170 * Returns the html that recreates this report.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000171 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100172 getReportHtml(): string {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000173 this.resetUIState();
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000174 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000175 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
176 }
177
178 /**
179 * Downloads a file (blob) using the system dialog prompt.
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000180 */
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100181 async _saveFile(blob: Blob|File): Promise<void> {
182 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Paul Lewisdaac1062020-03-05 14:37:10 +0000183 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000184 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100185 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
Simon Zünd2c704cd2020-06-04 11:08:35 +0200186 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000187 const ext = blob.type.match('json') ? '.json' : '.html';
188 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
189 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100190 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000191 }
192
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100193 async _print(): Promise<void> {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000194 const document = this.getDocument();
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100195 const clonedReport = (document.querySelector('.lh-root') as HTMLElement).cloneNode(true);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000196 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000197 if (!printWindow) {
198 return;
199 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000200 const style = printWindow.document.createElement('style');
Tim van der Lippefe3cdc92020-11-06 15:23:13 +0000201 style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css') || '';
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000202 printWindow.document.head.appendChild(style);
203 printWindow.document.body.replaceWith(clonedReport);
204 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100205 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport as HTMLElement);
Connor Clark99508362019-08-20 19:52:23 +0000206
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000207 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000208 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000209 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000210 printWindow.focus();
211 printWindow.print();
212 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000213 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000214 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000215 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000216 }
217
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100218 getDocument(): Document {
219 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000220 return this._document;
221 }
Jan Schefflerc5a400f2021-01-22 17:41:47 +0100222 resetUIState(): void {
223 // @ts-ignore https://github.com/GoogleChrome/lighthouse/issues/11628
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000224 this._resetUIState();
225 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000226}