chrome-devtools-mcp 1.10.1 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -20
- package/build/src/BrowserManager.js +155 -27
- package/build/src/McpContext.js +598 -200
- package/build/src/McpPage.js +513 -131
- package/build/src/McpResponse.js +291 -116
- package/build/src/McpWorker.js +66 -0
- package/build/src/SlimMcpResponse.js +5 -1
- package/build/src/TextSnapshot.js +40 -20
- package/build/src/ToolHandler.js +306 -147
- package/build/src/bin/chrome-devtools-mcp-main.js +13 -10
- package/build/src/bin/chrome-devtools-mcp.js +1 -1
- package/build/src/bin/chrome-devtools.js +34 -44
- package/build/src/collectors/PageCollector.js +13 -9
- package/build/src/collectors/ServiceWorkerCollector.js +20 -13
- package/build/src/collectors/constants.js +7 -0
- package/build/src/config/ConfigLocator.js +70 -0
- package/build/src/config/ConfigParser.js +159 -0
- package/build/src/config/ConfigWatcher.js +94 -0
- package/build/src/config/ServerConfig.js +251 -0
- package/build/src/config/browser-options.js +15 -17
- package/build/src/config/category-options.js +15 -30
- package/build/src/config/cli-commands.js +165 -5
- package/build/src/config/cli-options.js +441 -58
- package/build/src/config/mcp-options.js +212 -264
- package/build/src/config/puppeteer-options.js +67 -0
- package/build/src/config/tool-options.js +60 -0
- package/build/src/daemon/client.js +84 -44
- package/build/src/daemon/daemon.js +59 -45
- package/build/src/daemon/utils.js +46 -12
- package/build/src/devtools/DevToolsCommentBridge.js +71 -11
- package/build/src/devtools/DevtoolsUtils.js +52 -15
- package/build/src/devtools/McpHostBindingAdapter.js +0 -1
- package/build/src/formatters/CommentFormatter.js +4 -13
- package/build/src/formatters/ConsoleFormatter.js +29 -2
- package/build/src/formatters/CssFormatter.js +3 -2
- package/build/src/formatters/HeapSnapshotFormatter.js +111 -57
- package/build/src/formatters/IssueFormatter.js +33 -32
- package/build/src/formatters/NetworkFormatter.js +62 -22
- package/build/src/formatters/RemoteObjectFormatter.js +231 -0
- package/build/src/formatters/SnapshotFormatter.js +44 -7
- package/build/src/index.js +285 -88
- package/build/src/processors/ChunkedTraceParser.js +11 -11
- package/build/src/processors/HeapSnapshotManager.js +455 -156
- package/build/src/processors/PerformanceTrace.js +9 -9
- package/build/src/telemetry/ClearcutLogger.js +1 -1
- package/build/src/telemetry/transformation.js +7 -11
- package/build/src/telemetry/watchdog/main.js +1 -1
- package/build/src/third_party/THIRD_PARTY_NOTICES +399 -689
- package/build/src/third_party/bundled-packages.json +7 -8
- package/build/src/third_party/cli.js +9239 -0
- package/build/src/third_party/devtools-formatter-worker.js +4299 -2517
- package/build/src/third_party/devtools-heap-snapshot-worker.js +1298 -22
- package/build/src/third_party/index.js +132003 -146852
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantRead.md +0 -1
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantReadOptOut.md +5 -0
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantSet.md +0 -1
- package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantSetOptOut.md +5 -0
- package/build/src/third_party/issue-descriptions/deprecation.md +1 -1
- package/build/src/third_party/issue-descriptions/genericGeolocationPromptWithoutUserGesture.md +5 -0
- package/build/src/third_party/issue-descriptions/sriValidationFailedIntegrityMismatch.md +2 -0
- package/build/src/third_party/lighthouse-devtools-mcp-bundle.js +54576 -52174
- package/build/src/tools/ToolDefinition.js +102 -12
- package/build/src/tools/comments.js +62 -4
- package/build/src/tools/console.js +6 -4
- package/build/src/tools/css.js +5 -2
- package/build/src/tools/emulation.js +22 -8
- package/build/src/tools/extensions.js +5 -0
- package/build/src/tools/input.js +29 -9
- package/build/src/tools/lighthouse.js +136 -12
- package/build/src/tools/memory.js +166 -59
- package/build/src/tools/network.js +5 -4
- package/build/src/tools/pages.js +52 -7
- package/build/src/tools/performance.js +28 -7
- package/build/src/tools/pwa.js +7 -0
- package/build/src/tools/screencast.js +74 -33
- package/build/src/tools/screenshot.js +52 -21
- package/build/src/tools/script.js +125 -40
- package/build/src/tools/slim/tools.js +27 -13
- package/build/src/tools/snapshot.js +13 -3
- package/build/src/tools/thirdPartyDeveloper.js +9 -5
- package/build/src/tools/tools.js +35 -22
- package/build/src/tools/webmcp.js +11 -6
- package/build/src/types.js +11 -0
- package/build/src/utils/TaskRunner.js +136 -0
- package/build/src/utils/WaitForHelper.js +15 -2
- package/build/src/utils/bytes.js +2 -12
- package/build/src/utils/check-for-updates.js +2 -2
- package/build/src/utils/disclaimers.js +27 -0
- package/build/src/utils/errorHandling.js +20 -0
- package/build/src/utils/keyboard.js +14 -2
- package/build/src/utils/logger.js +84 -5
- package/build/src/utils/resourceType.js +27 -0
- package/build/src/utils/text.js +44 -0
- package/build/src/utils/timeout.js +14 -0
- package/build/src/utils/url.js +140 -3
- package/build/src/version.js +1 -1
- package/package.json +16 -11
- package/skills/a11y-debugging/SKILL.md +7 -12
- package/skills/chrome-devtools/SKILL.md +2 -2
- package/skills/chrome-devtools-cli/SKILL.md +18 -15
- package/skills/cookie-debugging/SKILL.md +8 -5
- package/skills/debug-optimize-lcp/SKILL.md +1 -1
- package/skills/troubleshooting/SKILL.md +5 -6
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
import { ISSUE_UTILS } from '../devtools/issueDescriptions.js';
|
|
7
7
|
import { DevTools } from '../third_party/index.js';
|
|
8
8
|
import { logger } from '../utils/logger.js';
|
|
9
|
+
import { substitutePlaceholders } from '../utils/text.js';
|
|
10
|
+
const parsedDescriptionCache = new WeakMap();
|
|
9
11
|
export class IssueFormatter {
|
|
10
12
|
#issue;
|
|
11
13
|
#options;
|
|
@@ -85,7 +87,7 @@ export class IssueFormatter {
|
|
|
85
87
|
delete data.frameId;
|
|
86
88
|
affectedResources.push({
|
|
87
89
|
uid,
|
|
88
|
-
data
|
|
90
|
+
data,
|
|
89
91
|
request,
|
|
90
92
|
});
|
|
91
93
|
}
|
|
@@ -94,53 +96,52 @@ export class IssueFormatter {
|
|
|
94
96
|
isValid() {
|
|
95
97
|
return this.#getTitle() !== undefined;
|
|
96
98
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
#getParsedDescription() {
|
|
100
|
+
const cached = parsedDescriptionCache.get(this.#issue);
|
|
101
|
+
if (cached) {
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
99
104
|
const markdownDescription = this.#issue.getDescription();
|
|
100
105
|
const filename = markdownDescription?.file;
|
|
101
106
|
if (!filename) {
|
|
102
107
|
logger?.(`no description found for issue:` + this.#issue.code());
|
|
103
|
-
|
|
108
|
+
const empty = {};
|
|
109
|
+
parsedDescriptionCache.set(this.#issue, empty);
|
|
110
|
+
return empty;
|
|
104
111
|
}
|
|
105
|
-
// We already have the description logic in #getDescription, but title extraction is separate
|
|
106
|
-
// We can reuse the logic or cache it.
|
|
107
|
-
// Ideally we should process markdown once.
|
|
108
112
|
const rawMarkdown = ISSUE_UTILS.getIssueDescription(filename);
|
|
109
113
|
if (!rawMarkdown) {
|
|
110
114
|
logger?.(`no markdown ${filename} found for issue:` + this.#issue.code());
|
|
111
|
-
|
|
115
|
+
const empty = {};
|
|
116
|
+
parsedDescriptionCache.set(this.#issue, empty);
|
|
117
|
+
return empty;
|
|
112
118
|
}
|
|
113
119
|
try {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
const description = DevTools.MarkdownIssueDescription.createIssueDescriptionFromRawMarkdown(rawMarkdown, {
|
|
121
|
+
file: filename,
|
|
122
|
+
links: markdownDescription?.links || [],
|
|
123
|
+
substitutions: markdownDescription?.substitutions,
|
|
124
|
+
});
|
|
125
|
+
const parsed = {
|
|
126
|
+
title: description.title,
|
|
127
|
+
description: substitutePlaceholders(rawMarkdown, markdownDescription?.substitutions),
|
|
128
|
+
};
|
|
129
|
+
parsedDescriptionCache.set(this.#issue, parsed);
|
|
130
|
+
return parsed;
|
|
122
131
|
}
|
|
123
132
|
catch {
|
|
124
133
|
logger?.('error parsing markdown for issue ' + this.#issue.code());
|
|
125
|
-
|
|
134
|
+
const empty = {};
|
|
135
|
+
parsedDescriptionCache.set(this.#issue, empty);
|
|
136
|
+
return empty;
|
|
126
137
|
}
|
|
127
138
|
}
|
|
139
|
+
// Helper to extract title
|
|
140
|
+
#getTitle() {
|
|
141
|
+
return this.#getParsedDescription().title;
|
|
142
|
+
}
|
|
128
143
|
#getDescription() {
|
|
129
|
-
|
|
130
|
-
const filename = markdownDescription?.file;
|
|
131
|
-
if (!filename) {
|
|
132
|
-
return undefined;
|
|
133
|
-
}
|
|
134
|
-
const rawMarkdown = ISSUE_UTILS.getIssueDescription(filename);
|
|
135
|
-
if (!rawMarkdown) {
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
try {
|
|
139
|
-
return DevTools.MarkdownIssueDescription.substitutePlaceholders(rawMarkdown, markdownDescription?.substitutions);
|
|
140
|
-
}
|
|
141
|
-
catch {
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
+
return this.#getParsedDescription().description;
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
147
|
function convertIssueConciseToString(issue) {
|
|
@@ -5,8 +5,25 @@
|
|
|
5
5
|
* */
|
|
6
6
|
import { isUtf8 } from 'node:buffer';
|
|
7
7
|
import { DevTools, } from '../third_party/index.js';
|
|
8
|
+
import { truncateText, truncateUrl } from '../utils/text.js';
|
|
8
9
|
const BODY_CONTEXT_SIZE_LIMIT = 10000;
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* How long to wait for a response body. Puppeteer reads a body only after the
|
|
12
|
+
* response finished loading, with no timeout of its own, and a response that
|
|
13
|
+
* never finishes (e.g. a `Cache-Control: no-store` response the page did not
|
|
14
|
+
* read) would otherwise hold the tool mutex forever.
|
|
15
|
+
*/
|
|
16
|
+
export const RESPONSE_BODY_TIMEOUT_MS = 5000;
|
|
17
|
+
class ResponseBodyTimeoutError extends Error {
|
|
18
|
+
timeout;
|
|
19
|
+
constructor(timeout) {
|
|
20
|
+
super(`Response body timed out after ${timeout} ms`);
|
|
21
|
+
this.timeout = timeout;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function responseBodyTimeoutMessage(error) {
|
|
25
|
+
return `<Response body not available: the response did not finish loading within ${error.timeout} ms>`;
|
|
26
|
+
}
|
|
10
27
|
export class NetworkFormatter {
|
|
11
28
|
#request;
|
|
12
29
|
#options;
|
|
@@ -49,13 +66,11 @@ export class NetworkFormatter {
|
|
|
49
66
|
this.#requestBody = requestBodyNotAvailableMessage;
|
|
50
67
|
}
|
|
51
68
|
}
|
|
69
|
+
else if (data) {
|
|
70
|
+
this.#requestBody = getSizeLimitedString(data, BODY_CONTEXT_SIZE_LIMIT);
|
|
71
|
+
}
|
|
52
72
|
else {
|
|
53
|
-
|
|
54
|
-
this.#requestBody = getSizeLimitedString(data, BODY_CONTEXT_SIZE_LIMIT);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
this.#requestBody = requestBodyNotAvailableMessage;
|
|
58
|
-
}
|
|
73
|
+
this.#requestBody = requestBodyNotAvailableMessage;
|
|
59
74
|
}
|
|
60
75
|
}
|
|
61
76
|
// Load Response Body
|
|
@@ -63,20 +78,23 @@ export class NetworkFormatter {
|
|
|
63
78
|
if (response) {
|
|
64
79
|
const responseBodyNotAvailableMessage = '<Response body not available anymore>';
|
|
65
80
|
if (this.#options.responseFilePath) {
|
|
81
|
+
let buffer;
|
|
66
82
|
try {
|
|
67
|
-
|
|
83
|
+
buffer = await this.#readResponseBody(response);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
this.#responseBody =
|
|
87
|
+
error instanceof ResponseBodyTimeoutError
|
|
88
|
+
? responseBodyTimeoutMessage(error)
|
|
89
|
+
: responseBodyNotAvailableMessage;
|
|
90
|
+
}
|
|
91
|
+
if (buffer) {
|
|
68
92
|
if (!this.#options.saveFile) {
|
|
69
93
|
throw new Error('Unable to save the response body to a file: no saveFile callback was configured.');
|
|
70
94
|
}
|
|
71
95
|
const result = await this.#options.saveFile(buffer, this.#options.responseFilePath, '.network-response');
|
|
72
96
|
this.#responseBodyFilePath = result.filename;
|
|
73
97
|
}
|
|
74
|
-
catch {
|
|
75
|
-
// Flatten error handling for buffer() failure and save failure
|
|
76
|
-
}
|
|
77
|
-
if (!this.#responseBodyFilePath) {
|
|
78
|
-
this.#responseBody = responseBodyNotAvailableMessage;
|
|
79
|
-
}
|
|
80
98
|
}
|
|
81
99
|
else {
|
|
82
100
|
this.#responseBody = await this.#getFormattedResponseBody(response, BODY_CONTEXT_SIZE_LIMIT);
|
|
@@ -109,7 +127,12 @@ export class NetworkFormatter {
|
|
|
109
127
|
}, {});
|
|
110
128
|
}
|
|
111
129
|
toJSONDetailed() {
|
|
112
|
-
|
|
130
|
+
// Puppeteer shares one redirect chain array between all hops of a
|
|
131
|
+
// redirect, so earlier hops also list themselves and the hops after them.
|
|
132
|
+
// Keep only the requests that redirected to this one.
|
|
133
|
+
const fullChain = this.#request.redirectChain();
|
|
134
|
+
const ownIndex = fullChain.indexOf(this.#request);
|
|
135
|
+
const redirectChain = ownIndex === -1 ? fullChain : fullChain.slice(0, ownIndex);
|
|
113
136
|
const formattedRedirectChain = redirectChain.reverse().map(request => {
|
|
114
137
|
const id = this.#options.requestIdResolver
|
|
115
138
|
? this.#options.requestIdResolver(request)
|
|
@@ -157,7 +180,7 @@ export class NetworkFormatter {
|
|
|
157
180
|
}
|
|
158
181
|
async #getFormattedResponseBody(httpResponse, sizeLimit = BODY_CONTEXT_SIZE_LIMIT) {
|
|
159
182
|
try {
|
|
160
|
-
const responseBuffer = await httpResponse
|
|
183
|
+
const responseBuffer = await this.#readResponseBody(httpResponse);
|
|
161
184
|
if (isUtf8(responseBuffer)) {
|
|
162
185
|
const responseAsTest = responseBuffer.toString('utf-8');
|
|
163
186
|
if (responseAsTest.length === 0) {
|
|
@@ -167,21 +190,38 @@ export class NetworkFormatter {
|
|
|
167
190
|
}
|
|
168
191
|
return '<binary data>';
|
|
169
192
|
}
|
|
170
|
-
catch {
|
|
193
|
+
catch (error) {
|
|
194
|
+
if (error instanceof ResponseBodyTimeoutError) {
|
|
195
|
+
return responseBodyTimeoutMessage(error);
|
|
196
|
+
}
|
|
171
197
|
return '<not available anymore>';
|
|
172
198
|
}
|
|
173
199
|
}
|
|
200
|
+
async #readResponseBody(httpResponse) {
|
|
201
|
+
const timeout = this.#options.responseBodyTimeout ?? RESPONSE_BODY_TIMEOUT_MS;
|
|
202
|
+
let timeoutId;
|
|
203
|
+
try {
|
|
204
|
+
return await Promise.race([
|
|
205
|
+
httpResponse.buffer(),
|
|
206
|
+
new Promise((_, reject) => {
|
|
207
|
+
timeoutId = setTimeout(() => {
|
|
208
|
+
reject(new ResponseBodyTimeoutError(timeout));
|
|
209
|
+
}, timeout);
|
|
210
|
+
}),
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
clearTimeout(timeoutId);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
174
217
|
}
|
|
175
218
|
function getSizeLimitedString(text, sizeLimit) {
|
|
176
|
-
|
|
177
|
-
return text.substring(0, sizeLimit) + '... <truncated>';
|
|
178
|
-
}
|
|
179
|
-
return text;
|
|
219
|
+
return truncateText(text, sizeLimit, { ellipsis: '... <truncated>' });
|
|
180
220
|
}
|
|
181
221
|
function convertNetworkRequestConciseToString(data) {
|
|
182
222
|
// Long URLs (e.g., data: URLs) bloat the concise list output. The full URL
|
|
183
223
|
// remains available via the detailed view and the structured content.
|
|
184
|
-
const url =
|
|
224
|
+
const url = truncateUrl(data.url);
|
|
185
225
|
return `reqid=${data.requestId} ${data.method} ${url} [${data.status}]${data.selectedInDevToolsUI ? ` [selected in the DevTools Network panel]` : ''}`;
|
|
186
226
|
}
|
|
187
227
|
function formatHeaders(headers) {
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Plain-text port of devtools-frontend's RemoteObjectPreviewFormatter
|
|
8
|
+
* (front_end/ui/legacy/components/object_ui/RemoteObjectPreviewFormatter.ts)
|
|
9
|
+
* at revision f7e7785de52d6f5955d6a9c5956c2fe2d520d091, the
|
|
10
|
+
* third_party/devtools-frontend revision this port was written against. Compare
|
|
11
|
+
* with that file when syncing. It formats the preview that CDP already attaches
|
|
12
|
+
* to console arguments, so nothing is evaluated in the page.
|
|
13
|
+
*
|
|
14
|
+
* Intentional differences from upstream:
|
|
15
|
+
* - Returns plain text instead of Lit templates, so there are no CSS classes,
|
|
16
|
+
* tooltips or localized strings ('empty' and 'empty × N' are English).
|
|
17
|
+
* - No nodeDescription markup: DOM nodes print their description as is instead
|
|
18
|
+
* of splitting it into tag, id and class spans (renderNodeTitle), and trusted
|
|
19
|
+
* types print their description instead of `ClassName "value"`.
|
|
20
|
+
* - 'date' and 'node' are added to the subtypes that print their description
|
|
21
|
+
* instead of a property preview.
|
|
22
|
+
* - The `includeNullOrUndefined` option is dropped; null and undefined
|
|
23
|
+
* properties are always listed.
|
|
24
|
+
* - Top-level strings print as their raw value (as the console does for string
|
|
25
|
+
* arguments), and unserializable primitives (NaN, -0, 1n) print from
|
|
26
|
+
* `unserializableValue`.
|
|
27
|
+
* - The 400-character description limit that upstream passes to
|
|
28
|
+
* trimEndWithMaxLength() inline is named MAX_DESCRIPTION_LENGTH.
|
|
29
|
+
*/
|
|
30
|
+
import { DevTools } from '../third_party/index.js';
|
|
31
|
+
const { StringUtilities } = DevTools.Platform;
|
|
32
|
+
const MAX_DESCRIPTION_LENGTH = 400;
|
|
33
|
+
const PROMISE_STATE = '[[PromiseState]]';
|
|
34
|
+
const PROMISE_RESULT = '[[PromiseResult]]';
|
|
35
|
+
const GENERATOR_STATE = '[[GeneratorState]]';
|
|
36
|
+
const PRIMITIVE_VALUE = '[[PrimitiveValue]]';
|
|
37
|
+
const WEAK_REF_TARGET = '[[WeakRefTarget]]';
|
|
38
|
+
// Subtypes whose description is shown instead of a property preview.
|
|
39
|
+
const SUBTYPES_WITHOUT_VALUE_PREVIEW = new Set([
|
|
40
|
+
'arraybuffer',
|
|
41
|
+
'dataview',
|
|
42
|
+
'date',
|
|
43
|
+
'error',
|
|
44
|
+
'internal#entry',
|
|
45
|
+
'null',
|
|
46
|
+
'node',
|
|
47
|
+
'regexp',
|
|
48
|
+
'trustedtype',
|
|
49
|
+
'webassemblymemory',
|
|
50
|
+
]);
|
|
51
|
+
function sortValue(property) {
|
|
52
|
+
switch (property.name) {
|
|
53
|
+
case PROMISE_STATE:
|
|
54
|
+
return 1;
|
|
55
|
+
case PROMISE_RESULT:
|
|
56
|
+
return 2;
|
|
57
|
+
case GENERATOR_STATE:
|
|
58
|
+
case PRIMITIVE_VALUE:
|
|
59
|
+
case WEAK_REF_TARGET:
|
|
60
|
+
return 3;
|
|
61
|
+
}
|
|
62
|
+
if (property.type !== 'function' && !property.name.startsWith('#')) {
|
|
63
|
+
return 4;
|
|
64
|
+
}
|
|
65
|
+
return 5;
|
|
66
|
+
}
|
|
67
|
+
function formatName(name) {
|
|
68
|
+
return /^\s|\s$|^$|\n/.test(name) ? `"${name.replace(/\n/g, '↵')}"` : name;
|
|
69
|
+
}
|
|
70
|
+
function abbreviateClassName(description) {
|
|
71
|
+
const parts = description.split('.');
|
|
72
|
+
if (parts.length === 1 && parts[0] === 'Object') {
|
|
73
|
+
return '{…}';
|
|
74
|
+
}
|
|
75
|
+
return parts
|
|
76
|
+
.map((part, i) => i < parts.length - 1 ? StringUtilities.trimMiddle(part, 3) : part)
|
|
77
|
+
.join('.');
|
|
78
|
+
}
|
|
79
|
+
function formatPropertyValue(type, subtype, description = '') {
|
|
80
|
+
switch (type) {
|
|
81
|
+
case 'accessor':
|
|
82
|
+
return '(...)';
|
|
83
|
+
case 'function':
|
|
84
|
+
return 'ƒ';
|
|
85
|
+
case 'string':
|
|
86
|
+
return StringUtilities.formatAsJSLiteral(description);
|
|
87
|
+
case 'object':
|
|
88
|
+
return subtype ? description : abbreviateClassName(description);
|
|
89
|
+
default:
|
|
90
|
+
return description;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function arrayLength(preview) {
|
|
94
|
+
const match = preview.description?.match(/[[(](\d+)[\])]$/);
|
|
95
|
+
return match ? Number(match[1]) : 0;
|
|
96
|
+
}
|
|
97
|
+
function formatArrayItems(preview) {
|
|
98
|
+
const length = arrayLength(preview);
|
|
99
|
+
const toIndex = (name) => {
|
|
100
|
+
const index = Number(name) >>> 0;
|
|
101
|
+
return String(index) === name && index < length ? index : -1;
|
|
102
|
+
};
|
|
103
|
+
const indexed = preview.properties
|
|
104
|
+
.filter(p => toIndex(p.name) !== -1)
|
|
105
|
+
.sort((a, b) => toIndex(a.name) - toIndex(b.name));
|
|
106
|
+
const others = preview.properties
|
|
107
|
+
.filter(p => toIndex(p.name) === -1)
|
|
108
|
+
.sort((a, b) => sortValue(a) - sortValue(b));
|
|
109
|
+
// Gaps can be shown when all properties are guaranteed to be in the preview.
|
|
110
|
+
const canShowGaps = !preview.overflow;
|
|
111
|
+
const gap = (count) => count === 1 ? 'empty' : `empty × ${count}`;
|
|
112
|
+
const items = [];
|
|
113
|
+
let previous = -1;
|
|
114
|
+
for (const property of indexed) {
|
|
115
|
+
const index = toIndex(property.name);
|
|
116
|
+
const value = formatPropertyValue(property.type, property.subtype, property.value);
|
|
117
|
+
if (canShowGaps) {
|
|
118
|
+
if (index - previous > 1) {
|
|
119
|
+
items.push(gap(index - previous - 1));
|
|
120
|
+
}
|
|
121
|
+
items.push(value);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
items.push(index === items.length ? value : `${property.name}: ${value}`);
|
|
125
|
+
}
|
|
126
|
+
previous = index;
|
|
127
|
+
}
|
|
128
|
+
if (canShowGaps && length - previous > 1) {
|
|
129
|
+
items.push(gap(length - previous - 1));
|
|
130
|
+
}
|
|
131
|
+
for (const property of others) {
|
|
132
|
+
items.push(`${formatName(property.name)}: ${formatPropertyValue(property.type, property.subtype, property.value)}`);
|
|
133
|
+
}
|
|
134
|
+
return items;
|
|
135
|
+
}
|
|
136
|
+
function formatObjectItems(preview) {
|
|
137
|
+
const properties = preview.properties
|
|
138
|
+
.filter(p => p.type !== 'accessor')
|
|
139
|
+
.sort((a, b) => sortValue(a) - sortValue(b));
|
|
140
|
+
const items = [];
|
|
141
|
+
for (let i = 0; i < properties.length; i++) {
|
|
142
|
+
const property = properties[i];
|
|
143
|
+
const value = formatPropertyValue(property.type, property.subtype, property.value);
|
|
144
|
+
if (preview.subtype === 'promise' && property.name === PROMISE_STATE) {
|
|
145
|
+
const result = properties.at(i + 1);
|
|
146
|
+
if (result?.name === PROMISE_RESULT) {
|
|
147
|
+
i++;
|
|
148
|
+
}
|
|
149
|
+
items.push(property.value !== 'pending' && result?.name === PROMISE_RESULT
|
|
150
|
+
? `<${property.value}>: ${formatPropertyValue(result.type, result.subtype, result.value)}`
|
|
151
|
+
: `<${property.value}>`);
|
|
152
|
+
}
|
|
153
|
+
else if (preview.subtype === 'generator' &&
|
|
154
|
+
property.name === GENERATOR_STATE) {
|
|
155
|
+
items.push(`<${property.value}>`);
|
|
156
|
+
}
|
|
157
|
+
else if (property.name === PRIMITIVE_VALUE) {
|
|
158
|
+
items.push(value);
|
|
159
|
+
}
|
|
160
|
+
else if (property.name === WEAK_REF_TARGET) {
|
|
161
|
+
items.push(property.type === 'undefined' ? '<cleared>' : value);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
items.push(`${formatName(property.name)}: ${value}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return items;
|
|
168
|
+
}
|
|
169
|
+
function formatEntryItems(preview) {
|
|
170
|
+
return (preview.entries ?? []).map(({ key, value }) => {
|
|
171
|
+
const formattedValue = formatPropertyValue(value.type, value.subtype, value.description);
|
|
172
|
+
return key
|
|
173
|
+
? `${formatPropertyValue(key.type, key.subtype, key.description)} => ${formattedValue}`
|
|
174
|
+
: formattedValue;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Formats a `Runtime.ObjectPreview` the way the DevTools console previews it,
|
|
179
|
+
* for example `Map(1) {1 => 2}`, `(3) [1, 2, 3]` or `{a: 1, self: {…}}`.
|
|
180
|
+
*/
|
|
181
|
+
function formatObjectPreview(preview) {
|
|
182
|
+
const description = preview.description ?? '';
|
|
183
|
+
if (preview.type !== 'object' ||
|
|
184
|
+
(preview.subtype && SUBTYPES_WITHOUT_VALUE_PREVIEW.has(preview.subtype))) {
|
|
185
|
+
return formatPropertyValue(preview.type, preview.subtype, description);
|
|
186
|
+
}
|
|
187
|
+
const isArray = preview.subtype === 'array' || preview.subtype === 'typedarray';
|
|
188
|
+
let prefix = '';
|
|
189
|
+
if (isArray) {
|
|
190
|
+
const length = arrayLength(preview);
|
|
191
|
+
const lengthText = length > 1 ? `(${length})` : '';
|
|
192
|
+
const name = description.replace(/[[(]\d+[\])]$/, '');
|
|
193
|
+
prefix = name === 'Array' ? lengthText : `${name}${lengthText}`;
|
|
194
|
+
}
|
|
195
|
+
else if (description !== 'Object') {
|
|
196
|
+
prefix = description;
|
|
197
|
+
}
|
|
198
|
+
const items = preview.entries
|
|
199
|
+
? formatEntryItems(preview)
|
|
200
|
+
: isArray
|
|
201
|
+
? formatArrayItems(preview)
|
|
202
|
+
: formatObjectItems(preview);
|
|
203
|
+
if (preview.overflow) {
|
|
204
|
+
items.push('…');
|
|
205
|
+
}
|
|
206
|
+
const body = isArray ? `[${items.join(', ')}]` : `{${items.join(', ')}}`;
|
|
207
|
+
return prefix ? `${prefix} ${body}` : body;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Formats a console argument from the description and preview that CDP sends
|
|
211
|
+
* with it.
|
|
212
|
+
*/
|
|
213
|
+
export function formatRemoteObject(remoteObject) {
|
|
214
|
+
if (remoteObject.preview &&
|
|
215
|
+
remoteObject.type === 'object' &&
|
|
216
|
+
remoteObject.subtype !== 'node' &&
|
|
217
|
+
remoteObject.subtype !== 'trustedtype') {
|
|
218
|
+
return formatObjectPreview(remoteObject.preview);
|
|
219
|
+
}
|
|
220
|
+
if (remoteObject.type === 'string') {
|
|
221
|
+
return String(remoteObject.value ?? '');
|
|
222
|
+
}
|
|
223
|
+
if (remoteObject.unserializableValue !== undefined) {
|
|
224
|
+
return remoteObject.unserializableValue;
|
|
225
|
+
}
|
|
226
|
+
if (remoteObject.description !== undefined) {
|
|
227
|
+
return StringUtilities.trimEndWithMaxLength(remoteObject.description, MAX_DESCRIPTION_LENGTH);
|
|
228
|
+
}
|
|
229
|
+
return String(remoteObject.value);
|
|
230
|
+
}
|
|
231
|
+
//# sourceMappingURL=RemoteObjectFormatter.js.map
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
import { truncateUrl } from '../utils/text.js';
|
|
6
7
|
export class SnapshotFormatter {
|
|
7
8
|
#snapshot;
|
|
8
9
|
constructor(snapshot) {
|
|
@@ -29,16 +30,33 @@ Get a verbose snapshot to include all elements if you are interested in the sele
|
|
|
29
30
|
const attributes = this.#getAttributes(node);
|
|
30
31
|
const line = ' '.repeat(depth * 2) +
|
|
31
32
|
attributes.join(' ') +
|
|
32
|
-
(node.id === this.#snapshot.selectedElementUid
|
|
33
|
+
(node.id && node.id === this.#snapshot.selectedElementUid
|
|
33
34
|
? ' [selected in the DevTools Elements panel]'
|
|
34
35
|
: '') +
|
|
35
36
|
'\n';
|
|
36
37
|
chunks.push(line);
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
if (!this.#hasRedundantTextChildren(node)) {
|
|
39
|
+
for (const child of node.children) {
|
|
40
|
+
chunks.push(this.#formatNode(child, depth + 1));
|
|
41
|
+
}
|
|
39
42
|
}
|
|
40
43
|
return chunks.join('');
|
|
41
44
|
}
|
|
45
|
+
#hasRedundantTextChildren(node) {
|
|
46
|
+
if (this.#snapshot.verbose || !node.name || node.children.length === 0) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
let text = '';
|
|
50
|
+
for (const child of node.children) {
|
|
51
|
+
if (child.role !== 'StaticText' ||
|
|
52
|
+
child.children.length !== 0 ||
|
|
53
|
+
child.id === this.#snapshot.selectedElementUid) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
text += child.name ?? '';
|
|
57
|
+
}
|
|
58
|
+
return stripWhitespace(text) === stripWhitespace(node.name);
|
|
59
|
+
}
|
|
42
60
|
#nodeToJSON(node) {
|
|
43
61
|
const rawAttrs = this.#getAttributesMap(node);
|
|
44
62
|
const children = node.children.map(child => this.#nodeToJSON(child));
|
|
@@ -49,8 +67,12 @@ Get a verbose snapshot to include all elements if you are interested in the sele
|
|
|
49
67
|
return result;
|
|
50
68
|
}
|
|
51
69
|
#getAttributes(serializedAXNodeRoot) {
|
|
52
|
-
const attributes = [
|
|
53
|
-
if (serializedAXNodeRoot.
|
|
70
|
+
const attributes = [];
|
|
71
|
+
if (serializedAXNodeRoot.id) {
|
|
72
|
+
attributes.push(`uid=${serializedAXNodeRoot.id}`);
|
|
73
|
+
}
|
|
74
|
+
if (serializedAXNodeRoot.role &&
|
|
75
|
+
serializedAXNodeRoot.role !== 'StaticText') {
|
|
54
76
|
attributes.push(serializedAXNodeRoot.role === 'none'
|
|
55
77
|
? 'ignored'
|
|
56
78
|
: serializedAXNodeRoot.role);
|
|
@@ -60,15 +82,21 @@ Get a verbose snapshot to include all elements if you are interested in the sele
|
|
|
60
82
|
}
|
|
61
83
|
const simpleAttrs = this.#getAttributesMap(serializedAXNodeRoot,
|
|
62
84
|
/* excludeSpecial */ true);
|
|
85
|
+
const isOption = serializedAXNodeRoot.role === 'option';
|
|
63
86
|
for (const attr of Object.keys(serializedAXNodeRoot).sort()) {
|
|
64
87
|
if (excludedAttributes.has(attr)) {
|
|
65
88
|
continue;
|
|
66
89
|
}
|
|
67
90
|
const mapped = booleanPropertyMap[attr];
|
|
68
|
-
if (mapped &&
|
|
91
|
+
if (mapped &&
|
|
92
|
+
simpleAttrs[mapped] &&
|
|
93
|
+
!(isOption && mapped === 'selectable')) {
|
|
69
94
|
attributes.push(mapped);
|
|
70
95
|
}
|
|
71
96
|
const val = simpleAttrs[attr];
|
|
97
|
+
if (isOption && attr === 'value' && val === serializedAXNodeRoot.name) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
72
100
|
if (val === true) {
|
|
73
101
|
attributes.push(attr);
|
|
74
102
|
}
|
|
@@ -81,7 +109,9 @@ Get a verbose snapshot to include all elements if you are interested in the sele
|
|
|
81
109
|
#getAttributesMap(node, excludeSpecial = false) {
|
|
82
110
|
const result = {};
|
|
83
111
|
if (!excludeSpecial) {
|
|
84
|
-
|
|
112
|
+
if (node.id) {
|
|
113
|
+
result.id = node.id;
|
|
114
|
+
}
|
|
85
115
|
if (node.role) {
|
|
86
116
|
result.role = node.role;
|
|
87
117
|
}
|
|
@@ -110,6 +140,10 @@ Get a verbose snapshot to include all elements if you are interested in the sele
|
|
|
110
140
|
result[attr] = true;
|
|
111
141
|
}
|
|
112
142
|
}
|
|
143
|
+
else if (attr === 'url' && typeof value === 'string') {
|
|
144
|
+
// Keep other URLs whole: the snapshot is their only full copy.
|
|
145
|
+
result[attr] = value.startsWith('data:') ? truncateUrl(value) : value;
|
|
146
|
+
}
|
|
113
147
|
else if (typeof value === 'string' || typeof value === 'number') {
|
|
114
148
|
result[attr] = value;
|
|
115
149
|
}
|
|
@@ -132,4 +166,7 @@ const excludedAttributes = new Set([
|
|
|
132
166
|
'backendNodeId',
|
|
133
167
|
'loaderId',
|
|
134
168
|
]);
|
|
169
|
+
function stripWhitespace(text) {
|
|
170
|
+
return text.replace(/\s+/g, '');
|
|
171
|
+
}
|
|
135
172
|
//# sourceMappingURL=SnapshotFormatter.js.map
|