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