Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 1 | // 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 Lippe | 132b4ba | 2020-09-02 16:00:48 +0100 | [diff] [blame] | 5 | // @ts-nocheck |
| 6 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
| 7 | |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 8 | import * as Common from '../common/common.js'; |
| 9 | import * as Components from '../components/components.js'; |
| 10 | import * as HostModule from '../host/host.js'; |
Simon Zünd | 2c704cd | 2020-06-04 11:08:35 +0200 | [diff] [blame] | 11 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 5df64b2 | 2020-09-11 13:04:24 +0100 | [diff] [blame] | 12 | import * as Root from '../root/root.js'; |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 13 | import * as SDK from '../sdk/sdk.js'; |
Paul Lewis | ca569a5 | 2020-09-09 17:11:51 +0100 | [diff] [blame] | 14 | import * as ThemeSupport from '../theme_support/theme_support.js'; |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 15 | import * as Timeline from '../timeline/timeline.js'; |
| 16 | import * as UI from '../ui/ui.js'; |
Tim van der Lippe | 696c926 | 2020-08-26 15:39:32 +0100 | [diff] [blame] | 17 | import * as Workspace from '../workspace/workspace.js'; |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 18 | |
Tim van der Lippe | 1e10f85 | 2020-10-30 14:35:01 +0000 | [diff] [blame] | 19 | import * as ReportRenderer from './LighthouseReporterTypes.js'; // eslint-disable-line no-unused-vars |
| 20 | |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 21 | const MaxLengthForLinks = 40; |
| 22 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 23 | /** |
| 24 | * @override |
| 25 | */ |
Tim van der Lippe | 1e10f85 | 2020-10-30 14:35:01 +0000 | [diff] [blame] | 26 | export class LighthouseReportRenderer extends self.ReportRenderer { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 27 | /** |
Tim van der Lippe | 4df32c9 | 2020-11-06 12:35:05 +0000 | [diff] [blame^] | 28 | * @param {!DOM} dom |
| 29 | */ |
| 30 | constructor(dom) { |
| 31 | super(dom); |
| 32 | } |
| 33 | /** |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 34 | * @param {!Element} el Parent element to render the report into. |
| 35 | * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 36 | */ |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 37 | static addViewTraceButton(el, artifacts) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 38 | if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) { |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 39 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 40 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 41 | |
Adam Raine | f46d158 | 2020-08-17 20:24:32 +0000 | [diff] [blame] | 42 | const simulated = artifacts.settings.throttlingMethod === 'simulate'; |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 43 | const container = el.querySelector('.lh-audit-group'); |
Paul Irish | bf20468 | 2020-05-13 16:11:37 -0700 | [diff] [blame] | 44 | const disclaimerEl = container.querySelector('.lh-metrics__disclaimer'); |
| 45 | // If it was a PWA-only run, we'd have a trace but no perf category to add the button to |
| 46 | if (!disclaimerEl) { |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 47 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 48 | } |
cjamcl@google.com | 21d2d22 | 2019-08-09 01:58:17 +0000 | [diff] [blame] | 49 | |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 50 | const defaultPassTrace = artifacts.traces.defaultPass; |
Adam Raine | f46d158 | 2020-08-17 20:24:32 +0000 | [diff] [blame] | 51 | const label = simulated ? Common.UIString.UIString('View Original Trace') : Common.UIString.UIString('View Trace'); |
| 52 | const timelineButton = UI.UIUtils.createTextButton(label, onViewTraceClick, 'view-trace'); |
| 53 | if (simulated) { |
| 54 | timelineButton.title = Common.UIString.UIString( |
| 55 | '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.'); |
| 56 | } |
Paul Irish | bf20468 | 2020-05-13 16:11:37 -0700 | [diff] [blame] | 57 | container.insertBefore(timelineButton, disclaimerEl.nextSibling); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 58 | |
| 59 | async function onViewTraceClick() { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 60 | HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace); |
Tim van der Lippe | 80d8265 | 2020-08-27 14:53:44 +0100 | [diff] [blame] | 61 | await UI.InspectorView.InspectorView.instance().showPanel('timeline'); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 62 | Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 63 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 64 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * @param {!Element} el |
| 68 | */ |
| 69 | static async linkifyNodeDetails(el) { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 70 | const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget(); |
| 71 | const domModel = mainTarget.model(SDK.DOMModel.DOMModel); |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 72 | |
| 73 | for (const origElement of el.getElementsByClassName('lh-node')) { |
Tim van der Lippe | 1e10f85 | 2020-10-30 14:35:01 +0000 | [diff] [blame] | 74 | /** @type {!ReportRenderer.NodeDetailsJSON} */ |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 75 | const detailsItem = origElement.dataset; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 76 | if (!detailsItem.path) { |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 77 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 78 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 79 | |
| 80 | const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path); |
| 81 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 82 | if (!nodeId) { |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 83 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 84 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 85 | const node = domModel.nodeForId(nodeId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 86 | if (!node) { |
cjamcl@google.com | 5c46b7e | 2018-12-04 14:44:47 +0000 | [diff] [blame] | 87 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 88 | } |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 89 | |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 90 | const element = await Common.Linkifier.Linkifier.linkify(node, {tooltip: detailsItem.snippet}); |
cjamcl@google.com | da0e4c6 | 2018-11-27 23:52:10 +0000 | [diff] [blame] | 91 | origElement.title = ''; |
| 92 | origElement.textContent = ''; |
| 93 | origElement.appendChild(element); |
| 94 | } |
| 95 | } |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * @param {!Element} el |
| 99 | */ |
Connor Clark | 0403a42 | 2019-11-18 18:03:18 -0800 | [diff] [blame] | 100 | static async linkifySourceLocationDetails(el) { |
| 101 | for (const origElement of el.getElementsByClassName('lh-source-location')) { |
Tim van der Lippe | 1e10f85 | 2020-10-30 14:35:01 +0000 | [diff] [blame] | 102 | /** @type {!ReportRenderer.SourceLocationDetailsJSON} */ |
Connor Clark | 0403a42 | 2019-11-18 18:03:18 -0800 | [diff] [blame] | 103 | const detailsItem = origElement.dataset; |
| 104 | if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) { |
| 105 | continue; |
| 106 | } |
| 107 | const url = detailsItem.sourceUrl; |
| 108 | const line = Number(detailsItem.sourceLine); |
| 109 | const column = Number(detailsItem.sourceColumn); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 110 | const element = await Components.Linkifier.Linkifier.linkifyURL( |
| 111 | url, {lineNumber: line, column, maxLength: MaxLengthForLinks}); |
Connor Clark | 0403a42 | 2019-11-18 18:03:18 -0800 | [diff] [blame] | 112 | origElement.title = ''; |
| 113 | origElement.textContent = ''; |
| 114 | origElement.appendChild(element); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param {!Element} el |
| 120 | */ |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 121 | static handleDarkMode(el) { |
Paul Lewis | ca569a5 | 2020-09-09 17:11:51 +0100 | [diff] [blame] | 122 | if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') { |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 123 | el.classList.add('dark'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 124 | } |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 125 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 126 | } |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * @override |
| 130 | */ |
Tim van der Lippe | 1e10f85 | 2020-10-30 14:35:01 +0000 | [diff] [blame] | 131 | export class LighthouseReportUIFeatures extends self.ReportUIFeatures { |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 132 | /** |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 133 | * @param {!DOM} dom |
| 134 | */ |
| 135 | constructor(dom) { |
| 136 | super(dom); |
| 137 | this._beforePrint = null; |
| 138 | this._afterPrint = null; |
| 139 | } |
| 140 | |
| 141 | /** |
Tim van der Lippe | 4df32c9 | 2020-11-06 12:35:05 +0000 | [diff] [blame^] | 142 | * @override |
| 143 | * @param {?function():void} beforePrint |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 144 | */ |
| 145 | setBeforePrint(beforePrint) { |
| 146 | this._beforePrint = beforePrint; |
| 147 | } |
| 148 | |
| 149 | /** |
Tim van der Lippe | 4df32c9 | 2020-11-06 12:35:05 +0000 | [diff] [blame^] | 150 | * @override |
| 151 | * @param {?function():void} afterPrint |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 152 | */ |
| 153 | setAfterPrint(afterPrint) { |
| 154 | this._afterPrint = afterPrint; |
| 155 | } |
| 156 | |
| 157 | /** |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 158 | * Returns the html that recreates this report. |
| 159 | * @return {string} |
| 160 | * @protected |
| 161 | */ |
| 162 | getReportHtml() { |
| 163 | this.resetUIState(); |
| 164 | return Lighthouse.ReportGenerator.generateReportHtml(this.json); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Downloads a file (blob) using the system dialog prompt. |
| 169 | * @param {!Blob|!File} blob The file to save. |
| 170 | */ |
| 171 | async _saveFile(blob) { |
Paul Lewis | daac106 | 2020-03-05 14:37:10 +0000 | [diff] [blame] | 172 | const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain(); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 173 | const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_'); |
Simon Zünd | 2c704cd | 2020-06-04 11:08:35 +0200 | [diff] [blame] | 174 | const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime)); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 175 | const ext = blob.type.match('json') ? '.json' : '.html'; |
| 176 | const basename = `${sanitizedDomain}-${timestamp}${ext}`; |
| 177 | const text = await blob.text(); |
Tim van der Lippe | 696c926 | 2020-08-26 15:39:32 +0100 | [diff] [blame] | 178 | Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | async _print() { |
| 182 | const document = this.getDocument(); |
| 183 | const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */); |
| 184 | const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1'); |
| 185 | const style = printWindow.document.createElement('style'); |
Tim van der Lippe | 5df64b2 | 2020-09-11 13:04:24 +0100 | [diff] [blame] | 186 | style.textContent = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/report.css'); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 187 | printWindow.document.head.appendChild(style); |
| 188 | printWindow.document.body.replaceWith(clonedReport); |
| 189 | // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`. |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame] | 190 | await LighthouseReportRenderer.linkifyNodeDetails(clonedReport); |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 191 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 192 | if (this._beforePrint) { |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 193 | this._beforePrint(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 194 | } |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 195 | printWindow.focus(); |
| 196 | printWindow.print(); |
| 197 | printWindow.close(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 198 | if (this._afterPrint) { |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 199 | this._afterPrint(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 200 | } |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @suppress {visibility} |
| 205 | * @return {!Document} |
| 206 | */ |
| 207 | getDocument() { |
| 208 | return this._document; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @suppress {visibility} |
| 213 | */ |
| 214 | resetUIState() { |
| 215 | this._resetUIState(); |
| 216 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 217 | } |