@mcp-b/chrome-devtools-mcp 0.12.0-beta.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/LICENSE +202 -0
- package/README.md +554 -0
- package/build/src/DevToolsConnectionAdapter.js +69 -0
- package/build/src/DevtoolsUtils.js +206 -0
- package/build/src/McpContext.js +499 -0
- package/build/src/McpResponse.js +396 -0
- package/build/src/Mutex.js +37 -0
- package/build/src/PageCollector.js +283 -0
- package/build/src/WaitForHelper.js +139 -0
- package/build/src/browser.js +134 -0
- package/build/src/cli.js +213 -0
- package/build/src/formatters/consoleFormatter.js +121 -0
- package/build/src/formatters/networkFormatter.js +77 -0
- package/build/src/formatters/snapshotFormatter.js +73 -0
- package/build/src/index.js +21 -0
- package/build/src/issue-descriptions.js +39 -0
- package/build/src/logger.js +27 -0
- package/build/src/main.js +130 -0
- package/build/src/polyfill.js +7 -0
- package/build/src/third_party/index.js +16 -0
- package/build/src/tools/ToolDefinition.js +20 -0
- package/build/src/tools/categories.js +24 -0
- package/build/src/tools/console.js +85 -0
- package/build/src/tools/emulation.js +87 -0
- package/build/src/tools/input.js +268 -0
- package/build/src/tools/network.js +106 -0
- package/build/src/tools/pages.js +237 -0
- package/build/src/tools/performance.js +147 -0
- package/build/src/tools/screenshot.js +84 -0
- package/build/src/tools/script.js +71 -0
- package/build/src/tools/snapshot.js +52 -0
- package/build/src/tools/tools.js +31 -0
- package/build/src/tools/webmcp.js +233 -0
- package/build/src/trace-processing/parse.js +84 -0
- package/build/src/transports/WebMCPBridgeScript.js +196 -0
- package/build/src/transports/WebMCPClientTransport.js +276 -0
- package/build/src/transports/index.js +7 -0
- package/build/src/utils/keyboard.js +296 -0
- package/build/src/utils/pagination.js +49 -0
- package/build/src/utils/types.js +6 -0
- package/package.json +87 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { createIssuesFromProtocolIssue, IssueAggregator, } from '../node_modules/chrome-devtools-frontend/mcp/mcp.js';
|
|
7
|
+
import { FakeIssuesManager } from './DevtoolsUtils.js';
|
|
8
|
+
import { logger } from './logger.js';
|
|
9
|
+
function createIdGenerator() {
|
|
10
|
+
let i = 1;
|
|
11
|
+
return () => {
|
|
12
|
+
if (i === Number.MAX_SAFE_INTEGER) {
|
|
13
|
+
i = 0;
|
|
14
|
+
}
|
|
15
|
+
return i++;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export const stableIdSymbol = Symbol('stableIdSymbol');
|
|
19
|
+
export class PageCollector {
|
|
20
|
+
#browser;
|
|
21
|
+
#listenersInitializer;
|
|
22
|
+
#listeners = new WeakMap();
|
|
23
|
+
#maxNavigationSaved = 3;
|
|
24
|
+
/**
|
|
25
|
+
* This maps a Page to a list of navigations with a sub-list
|
|
26
|
+
* of all collected resources.
|
|
27
|
+
* The newer navigations come first.
|
|
28
|
+
*/
|
|
29
|
+
storage = new WeakMap();
|
|
30
|
+
constructor(browser, listeners) {
|
|
31
|
+
this.#browser = browser;
|
|
32
|
+
this.#listenersInitializer = listeners;
|
|
33
|
+
}
|
|
34
|
+
async init(pages) {
|
|
35
|
+
for (const page of pages) {
|
|
36
|
+
this.addPage(page);
|
|
37
|
+
}
|
|
38
|
+
this.#browser.on('targetcreated', this.#onTargetCreated);
|
|
39
|
+
this.#browser.on('targetdestroyed', this.#onTargetDestroyed);
|
|
40
|
+
}
|
|
41
|
+
dispose() {
|
|
42
|
+
this.#browser.off('targetcreated', this.#onTargetCreated);
|
|
43
|
+
this.#browser.off('targetdestroyed', this.#onTargetDestroyed);
|
|
44
|
+
}
|
|
45
|
+
#onTargetCreated = async (target) => {
|
|
46
|
+
const page = await target.page();
|
|
47
|
+
if (!page) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.addPage(page);
|
|
51
|
+
};
|
|
52
|
+
#onTargetDestroyed = async (target) => {
|
|
53
|
+
const page = await target.page();
|
|
54
|
+
if (!page) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this.cleanupPageDestroyed(page);
|
|
58
|
+
};
|
|
59
|
+
addPage(page) {
|
|
60
|
+
this.#initializePage(page);
|
|
61
|
+
}
|
|
62
|
+
#initializePage(page) {
|
|
63
|
+
if (this.storage.has(page)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const idGenerator = createIdGenerator();
|
|
67
|
+
const storedLists = [[]];
|
|
68
|
+
this.storage.set(page, storedLists);
|
|
69
|
+
const listeners = this.#listenersInitializer(value => {
|
|
70
|
+
const withId = value;
|
|
71
|
+
withId[stableIdSymbol] = idGenerator();
|
|
72
|
+
const navigations = this.storage.get(page) ?? [[]];
|
|
73
|
+
navigations[0].push(withId);
|
|
74
|
+
});
|
|
75
|
+
listeners['framenavigated'] = (frame) => {
|
|
76
|
+
// Only split the storage on main frame navigation
|
|
77
|
+
if (frame !== page.mainFrame()) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.splitAfterNavigation(page);
|
|
81
|
+
};
|
|
82
|
+
for (const [name, listener] of Object.entries(listeners)) {
|
|
83
|
+
page.on(name, listener);
|
|
84
|
+
}
|
|
85
|
+
this.#listeners.set(page, listeners);
|
|
86
|
+
}
|
|
87
|
+
splitAfterNavigation(page) {
|
|
88
|
+
const navigations = this.storage.get(page);
|
|
89
|
+
if (!navigations) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// Add the latest navigation first
|
|
93
|
+
navigations.unshift([]);
|
|
94
|
+
navigations.splice(this.#maxNavigationSaved);
|
|
95
|
+
}
|
|
96
|
+
cleanupPageDestroyed(page) {
|
|
97
|
+
const listeners = this.#listeners.get(page);
|
|
98
|
+
if (listeners) {
|
|
99
|
+
for (const [name, listener] of Object.entries(listeners)) {
|
|
100
|
+
page.off(name, listener);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
this.storage.delete(page);
|
|
104
|
+
}
|
|
105
|
+
getData(page, includePreservedData) {
|
|
106
|
+
const navigations = this.storage.get(page);
|
|
107
|
+
if (!navigations) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
if (!includePreservedData) {
|
|
111
|
+
return navigations[0];
|
|
112
|
+
}
|
|
113
|
+
const data = [];
|
|
114
|
+
for (let index = this.#maxNavigationSaved; index >= 0; index--) {
|
|
115
|
+
if (navigations[index]) {
|
|
116
|
+
data.push(...navigations[index]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
getIdForResource(resource) {
|
|
122
|
+
return resource[stableIdSymbol] ?? -1;
|
|
123
|
+
}
|
|
124
|
+
getById(page, stableId) {
|
|
125
|
+
const navigations = this.storage.get(page);
|
|
126
|
+
if (!navigations) {
|
|
127
|
+
throw new Error('No requests found for selected page');
|
|
128
|
+
}
|
|
129
|
+
const item = this.find(page, item => item[stableIdSymbol] === stableId);
|
|
130
|
+
if (item) {
|
|
131
|
+
return item;
|
|
132
|
+
}
|
|
133
|
+
throw new Error('Request not found for selected page');
|
|
134
|
+
}
|
|
135
|
+
find(page, filter) {
|
|
136
|
+
const navigations = this.storage.get(page);
|
|
137
|
+
if (!navigations) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
for (const navigation of navigations) {
|
|
141
|
+
const item = navigation.find(filter);
|
|
142
|
+
if (item) {
|
|
143
|
+
return item;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
export class ConsoleCollector extends PageCollector {
|
|
150
|
+
#subscribedPages = new WeakMap();
|
|
151
|
+
addPage(page) {
|
|
152
|
+
super.addPage(page);
|
|
153
|
+
if (!this.#subscribedPages.has(page)) {
|
|
154
|
+
const subscriber = new PageIssueSubscriber(page);
|
|
155
|
+
this.#subscribedPages.set(page, subscriber);
|
|
156
|
+
void subscriber.subscribe();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
cleanupPageDestroyed(page) {
|
|
160
|
+
super.cleanupPageDestroyed(page);
|
|
161
|
+
this.#subscribedPages.get(page)?.unsubscribe();
|
|
162
|
+
this.#subscribedPages.delete(page);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
class PageIssueSubscriber {
|
|
166
|
+
#issueManager = new FakeIssuesManager();
|
|
167
|
+
#issueAggregator = new IssueAggregator(this.#issueManager);
|
|
168
|
+
#seenKeys = new Set();
|
|
169
|
+
#seenIssues = new Set();
|
|
170
|
+
#page;
|
|
171
|
+
#session;
|
|
172
|
+
constructor(page) {
|
|
173
|
+
this.#page = page;
|
|
174
|
+
// @ts-expect-error use existing CDP client (internal Puppeteer API).
|
|
175
|
+
this.#session = this.#page._client();
|
|
176
|
+
}
|
|
177
|
+
#resetIssueAggregator() {
|
|
178
|
+
this.#issueManager = new FakeIssuesManager();
|
|
179
|
+
if (this.#issueAggregator) {
|
|
180
|
+
this.#issueAggregator.removeEventListener("AggregatedIssueUpdated" /* IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED */, this.#onAggregatedissue);
|
|
181
|
+
}
|
|
182
|
+
this.#issueAggregator = new IssueAggregator(this.#issueManager);
|
|
183
|
+
this.#issueAggregator.addEventListener("AggregatedIssueUpdated" /* IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED */, this.#onAggregatedissue);
|
|
184
|
+
}
|
|
185
|
+
async subscribe() {
|
|
186
|
+
this.#resetIssueAggregator();
|
|
187
|
+
this.#page.on('framenavigated', this.#onFrameNavigated);
|
|
188
|
+
this.#session.on('Audits.issueAdded', this.#onIssueAdded);
|
|
189
|
+
try {
|
|
190
|
+
await this.#session.send('Audits.enable');
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
logger('Error subscribing to issues', error);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
unsubscribe() {
|
|
197
|
+
this.#seenKeys.clear();
|
|
198
|
+
this.#seenIssues.clear();
|
|
199
|
+
this.#page.off('framenavigated', this.#onFrameNavigated);
|
|
200
|
+
this.#session.off('Audits.issueAdded', this.#onIssueAdded);
|
|
201
|
+
if (this.#issueAggregator) {
|
|
202
|
+
this.#issueAggregator.removeEventListener("AggregatedIssueUpdated" /* IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED */, this.#onAggregatedissue);
|
|
203
|
+
}
|
|
204
|
+
void this.#session.send('Audits.disable').catch(() => {
|
|
205
|
+
// might fail.
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
#onAggregatedissue = (event) => {
|
|
209
|
+
if (this.#seenIssues.has(event.data)) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
this.#seenIssues.add(event.data);
|
|
213
|
+
this.#page.emit('issue', event.data);
|
|
214
|
+
};
|
|
215
|
+
// On navigation, we reset issue aggregation.
|
|
216
|
+
#onFrameNavigated = (frame) => {
|
|
217
|
+
// Only split the storage on main frame navigation
|
|
218
|
+
if (frame !== frame.page().mainFrame()) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
this.#seenKeys.clear();
|
|
222
|
+
this.#seenIssues.clear();
|
|
223
|
+
this.#resetIssueAggregator();
|
|
224
|
+
};
|
|
225
|
+
#onIssueAdded = (data) => {
|
|
226
|
+
try {
|
|
227
|
+
const inspectorIssue = data.issue;
|
|
228
|
+
// @ts-expect-error Types of protocol from Puppeteer and CDP are
|
|
229
|
+
// incomparable for InspectorIssueCode, one is union, other is enum.
|
|
230
|
+
const issue = createIssuesFromProtocolIssue(null, inspectorIssue)[0];
|
|
231
|
+
if (!issue) {
|
|
232
|
+
logger('No issue mapping for for the issue: ', inspectorIssue.code);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const primaryKey = issue.primaryKey();
|
|
236
|
+
if (this.#seenKeys.has(primaryKey)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
this.#seenKeys.add(primaryKey);
|
|
240
|
+
this.#issueManager.dispatchEventToListeners("IssueAdded" /* IssuesManagerEvents.ISSUE_ADDED */, {
|
|
241
|
+
issue,
|
|
242
|
+
// @ts-expect-error We don't care that issues model is null
|
|
243
|
+
issuesModel: null,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
logger('Error creating a new issue', error);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
export class NetworkCollector extends PageCollector {
|
|
252
|
+
constructor(browser, listeners = collect => {
|
|
253
|
+
return {
|
|
254
|
+
request: req => {
|
|
255
|
+
collect(req);
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
}) {
|
|
259
|
+
super(browser, listeners);
|
|
260
|
+
}
|
|
261
|
+
splitAfterNavigation(page) {
|
|
262
|
+
const navigations = this.storage.get(page) ?? [];
|
|
263
|
+
if (!navigations) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const requests = navigations[0];
|
|
267
|
+
const lastRequestIdx = requests.findLastIndex(request => {
|
|
268
|
+
return request.frame() === page.mainFrame()
|
|
269
|
+
? request.isNavigationRequest()
|
|
270
|
+
: false;
|
|
271
|
+
});
|
|
272
|
+
// Keep all requests since the last navigation request including that
|
|
273
|
+
// navigation request itself.
|
|
274
|
+
// Keep the reference
|
|
275
|
+
if (lastRequestIdx !== -1) {
|
|
276
|
+
const fromCurrentNavigation = requests.splice(lastRequestIdx);
|
|
277
|
+
navigations.unshift(fromCurrentNavigation);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
navigations.unshift([]);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { logger } from './logger.js';
|
|
7
|
+
export class WaitForHelper {
|
|
8
|
+
#abortController = new AbortController();
|
|
9
|
+
#page;
|
|
10
|
+
#stableDomTimeout;
|
|
11
|
+
#stableDomFor;
|
|
12
|
+
#expectNavigationIn;
|
|
13
|
+
#navigationTimeout;
|
|
14
|
+
constructor(page, cpuTimeoutMultiplier, networkTimeoutMultiplier) {
|
|
15
|
+
this.#stableDomTimeout = 3000 * cpuTimeoutMultiplier;
|
|
16
|
+
this.#stableDomFor = 100 * cpuTimeoutMultiplier;
|
|
17
|
+
this.#expectNavigationIn = 100 * cpuTimeoutMultiplier;
|
|
18
|
+
this.#navigationTimeout = 3000 * networkTimeoutMultiplier;
|
|
19
|
+
this.#page = page;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A wrapper that executes a action and waits for
|
|
23
|
+
* a potential navigation, after which it waits
|
|
24
|
+
* for the DOM to be stable before returning.
|
|
25
|
+
*/
|
|
26
|
+
async waitForStableDom() {
|
|
27
|
+
const stableDomObserver = await this.#page.evaluateHandle(timeout => {
|
|
28
|
+
let timeoutId;
|
|
29
|
+
function callback() {
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
timeoutId = setTimeout(() => {
|
|
32
|
+
domObserver.resolver.resolve();
|
|
33
|
+
domObserver.observer.disconnect();
|
|
34
|
+
}, timeout);
|
|
35
|
+
}
|
|
36
|
+
const domObserver = {
|
|
37
|
+
resolver: Promise.withResolvers(),
|
|
38
|
+
observer: new MutationObserver(callback),
|
|
39
|
+
};
|
|
40
|
+
// It's possible that the DOM is not gonna change so we
|
|
41
|
+
// need to start the timeout initially.
|
|
42
|
+
callback();
|
|
43
|
+
domObserver.observer.observe(document.body, {
|
|
44
|
+
childList: true,
|
|
45
|
+
subtree: true,
|
|
46
|
+
attributes: true,
|
|
47
|
+
});
|
|
48
|
+
return domObserver;
|
|
49
|
+
}, this.#stableDomFor);
|
|
50
|
+
this.#abortController.signal.addEventListener('abort', async () => {
|
|
51
|
+
try {
|
|
52
|
+
await stableDomObserver.evaluate(observer => {
|
|
53
|
+
observer.observer.disconnect();
|
|
54
|
+
observer.resolver.resolve();
|
|
55
|
+
});
|
|
56
|
+
await stableDomObserver.dispose();
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// Ignored cleanup errors
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return Promise.race([
|
|
63
|
+
stableDomObserver.evaluate(async (observer) => {
|
|
64
|
+
return await observer.resolver.promise;
|
|
65
|
+
}),
|
|
66
|
+
this.timeout(this.#stableDomTimeout).then(() => {
|
|
67
|
+
throw new Error('Timeout');
|
|
68
|
+
}),
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
async waitForNavigationStarted() {
|
|
72
|
+
// Currently Puppeteer does not have API
|
|
73
|
+
// For when a navigation is about to start
|
|
74
|
+
const navigationStartedPromise = new Promise(resolve => {
|
|
75
|
+
const listener = (event) => {
|
|
76
|
+
if ([
|
|
77
|
+
'historySameDocument',
|
|
78
|
+
'historyDifferentDocument',
|
|
79
|
+
'sameDocument',
|
|
80
|
+
].includes(event.navigationType)) {
|
|
81
|
+
resolve(false);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
resolve(true);
|
|
85
|
+
};
|
|
86
|
+
this.#page._client().on('Page.frameStartedNavigating', listener);
|
|
87
|
+
this.#abortController.signal.addEventListener('abort', () => {
|
|
88
|
+
resolve(false);
|
|
89
|
+
this.#page._client().off('Page.frameStartedNavigating', listener);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return await Promise.race([
|
|
93
|
+
navigationStartedPromise,
|
|
94
|
+
this.timeout(this.#expectNavigationIn).then(() => false),
|
|
95
|
+
]);
|
|
96
|
+
}
|
|
97
|
+
timeout(time) {
|
|
98
|
+
return new Promise(res => {
|
|
99
|
+
const id = setTimeout(res, time);
|
|
100
|
+
this.#abortController.signal.addEventListener('abort', () => {
|
|
101
|
+
res();
|
|
102
|
+
clearTimeout(id);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async waitForEventsAfterAction(action) {
|
|
107
|
+
const navigationFinished = this.waitForNavigationStarted()
|
|
108
|
+
.then(navigationStated => {
|
|
109
|
+
if (navigationStated) {
|
|
110
|
+
return this.#page.waitForNavigation({
|
|
111
|
+
timeout: this.#navigationTimeout,
|
|
112
|
+
signal: this.#abortController.signal,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
})
|
|
117
|
+
.catch(error => logger(error));
|
|
118
|
+
try {
|
|
119
|
+
await action();
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
// Clear up pending promises
|
|
123
|
+
this.#abortController.abort();
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
await navigationFinished;
|
|
128
|
+
// Wait for stable dom after navigation so we execute in
|
|
129
|
+
// the correct context
|
|
130
|
+
await this.waitForStableDom();
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
logger(error);
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
this.#abortController.abort();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { logger } from './logger.js';
|
|
10
|
+
import { puppeteer } from './third_party/index.js';
|
|
11
|
+
let browser;
|
|
12
|
+
function makeTargetFilter() {
|
|
13
|
+
const ignoredPrefixes = new Set([
|
|
14
|
+
'chrome://',
|
|
15
|
+
'chrome-extension://',
|
|
16
|
+
'chrome-untrusted://',
|
|
17
|
+
]);
|
|
18
|
+
return function targetFilter(target) {
|
|
19
|
+
if (target.url() === 'chrome://newtab/') {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
// Could be the only page opened in the browser.
|
|
23
|
+
if (target.url().startsWith('chrome://inspect')) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
for (const prefix of ignoredPrefixes) {
|
|
27
|
+
if (target.url().startsWith(prefix)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export async function ensureBrowserConnected(options) {
|
|
35
|
+
if (browser?.connected) {
|
|
36
|
+
return browser;
|
|
37
|
+
}
|
|
38
|
+
const connectOptions = {
|
|
39
|
+
targetFilter: makeTargetFilter(),
|
|
40
|
+
defaultViewport: null,
|
|
41
|
+
handleDevToolsAsPage: true,
|
|
42
|
+
};
|
|
43
|
+
if (options.wsEndpoint) {
|
|
44
|
+
connectOptions.browserWSEndpoint = options.wsEndpoint;
|
|
45
|
+
if (options.wsHeaders) {
|
|
46
|
+
connectOptions.headers = options.wsHeaders;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else if (options.browserURL) {
|
|
50
|
+
connectOptions.browserURL = options.browserURL;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
throw new Error('Either browserURL or wsEndpoint must be provided');
|
|
54
|
+
}
|
|
55
|
+
logger('Connecting Puppeteer to ', JSON.stringify(connectOptions));
|
|
56
|
+
browser = await puppeteer.connect(connectOptions);
|
|
57
|
+
logger('Connected Puppeteer');
|
|
58
|
+
return browser;
|
|
59
|
+
}
|
|
60
|
+
export async function launch(options) {
|
|
61
|
+
const { channel, executablePath, headless, isolated } = options;
|
|
62
|
+
const profileDirName = channel && channel !== 'stable'
|
|
63
|
+
? `chrome-profile-${channel}`
|
|
64
|
+
: 'chrome-profile';
|
|
65
|
+
let userDataDir = options.userDataDir;
|
|
66
|
+
if (!isolated && !userDataDir) {
|
|
67
|
+
userDataDir = path.join(os.homedir(), '.cache', 'chrome-devtools-mcp', profileDirName);
|
|
68
|
+
await fs.promises.mkdir(userDataDir, {
|
|
69
|
+
recursive: true,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const args = [
|
|
73
|
+
...(options.args ?? []),
|
|
74
|
+
'--hide-crash-restore-bubble',
|
|
75
|
+
];
|
|
76
|
+
if (headless) {
|
|
77
|
+
args.push('--screen-info={3840x2160}');
|
|
78
|
+
}
|
|
79
|
+
let puppeteerChannel;
|
|
80
|
+
if (options.devtools) {
|
|
81
|
+
args.push('--auto-open-devtools-for-tabs');
|
|
82
|
+
}
|
|
83
|
+
if (!executablePath) {
|
|
84
|
+
puppeteerChannel =
|
|
85
|
+
channel && channel !== 'stable'
|
|
86
|
+
? `chrome-${channel}`
|
|
87
|
+
: 'chrome';
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const browser = await puppeteer.launch({
|
|
91
|
+
channel: puppeteerChannel,
|
|
92
|
+
targetFilter: makeTargetFilter(),
|
|
93
|
+
executablePath,
|
|
94
|
+
defaultViewport: null,
|
|
95
|
+
userDataDir,
|
|
96
|
+
pipe: true,
|
|
97
|
+
headless,
|
|
98
|
+
args,
|
|
99
|
+
acceptInsecureCerts: options.acceptInsecureCerts,
|
|
100
|
+
handleDevToolsAsPage: true,
|
|
101
|
+
});
|
|
102
|
+
if (options.logFile) {
|
|
103
|
+
// FIXME: we are probably subscribing too late to catch startup logs. We
|
|
104
|
+
// should expose the process earlier or expose the getRecentLogs() getter.
|
|
105
|
+
browser.process()?.stderr?.pipe(options.logFile);
|
|
106
|
+
browser.process()?.stdout?.pipe(options.logFile);
|
|
107
|
+
}
|
|
108
|
+
if (options.viewport) {
|
|
109
|
+
const [page] = await browser.pages();
|
|
110
|
+
// @ts-expect-error internal API for now.
|
|
111
|
+
await page?.resize({
|
|
112
|
+
contentWidth: options.viewport.width,
|
|
113
|
+
contentHeight: options.viewport.height,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return browser;
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (userDataDir &&
|
|
120
|
+
error.message.includes('The browser is already running')) {
|
|
121
|
+
throw new Error(`The browser is already running for ${userDataDir}. Use --isolated to run multiple browser instances.`, {
|
|
122
|
+
cause: error,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export async function ensureBrowserLaunched(options) {
|
|
129
|
+
if (browser?.connected) {
|
|
130
|
+
return browser;
|
|
131
|
+
}
|
|
132
|
+
browser = await launch(options);
|
|
133
|
+
return browser;
|
|
134
|
+
}
|