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